整合Kong和Consul
一、先裝個(gè)Consul拟枚,參考:Consul 快速入門
注意事項(xiàng):?jiǎn)?dòng)時(shí),可以添加 -dns-port=53
這樣就可以走默認(rèn)的DNS端口了众弓。
二恩溅、然后裝個(gè)Kong,參考:Kong的簡(jiǎn)介和安裝 和 使用Docker安裝Kong
注意事項(xiàng):如果使用docker方式執(zhí)行谓娃,網(wǎng)絡(luò)需要使用host方式脚乡。
附Docker執(zhí)行方式:
[root@local12 ~]# docker run -d --name kong-database \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" postgres:9.6
[root@local12 ~]# docker run --rm \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=192.168.56.112" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:0.13.1-centos kong migrations up
[root@local12 ~]# docker run -d --name kong \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=192.168.56.112" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "DNS_RESOLVER=192.168.56.112" \
-p 8000:8000 -p 8443:8443 -p 8001:8001 -p 8444:8444 \
-v /mnt/kong.conf:/etc/kong/kong.conf \
kong:0.13.1-centos
[root@local12 ~]# docker run -d -p 1337:1337 --name konga pantsel/konga
三、檢查一下效果
kong的info信息
四滨达、配置一個(gè)DNS SRV每窖,后面用來(lái)做輪訓(xùn)
[root@local13 ~]# cat /etc/consul.d/web.json
{"service": {"name": "web", "tags": ["springboot"], "port": 8080}}
# local12同local13
# 查下結(jié)果
[root@local13 ~]# dig @192.168.56.112 web.service.consul SRV
...
;; QUESTION SECTION:
;web.service.consul. IN SRV
;; ANSWER SECTION:
web.service.consul. 0 IN SRV 1 1 8080 agent-two.node.dc1.consul.
web.service.consul. 0 IN SRV 1 1 8080 agent-one.node.dc1.consul.
至此,Kong與Consul就整合完成了弦悉。然后窒典,我們?cè)僬蟂pring Cloud,實(shí)現(xiàn)Kong網(wǎng)關(guān)到APP的DNS輪訓(xùn)稽莉。
Spring Cloud整合Consul
一瀑志、POM文件添加Spring Cloud依賴
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-consul-dependencies</artifactId>
<version>2.0.2.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
...
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
...
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
二、配置YML文件污秆,添加CONSUL相關(guān)內(nèi)容
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
register: true
hostname: 127.0.0.1
tags: version=1.0,author=Dreamson.Ma
healthCheckPath: /actuator/health
healthCheckInterval: 5s
instanceId: ${spring.application.name}:${spring.cloud.client.hostname}:${server.port}
三劈猪、Spring Boot入口添加相關(guān)注解
@EnableDiscoveryClient
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@Import({DynamicDataSourceConfig.class})
public class DreamsonApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DreamsonApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DreamsonApplication.class);
}
}
至此,Spring Cloud整合Consul已經(jīng)完成良拼。運(yùn)行下看看效果吧战得。
spring cloud整合consul
通過(guò)Kong的DNS輪訓(xùn)訪問(wèn)應(yīng)用
這次使用konga(kong的ui管理工具)進(jìn)行配置。
一庸推、配置一個(gè)Service
konga配置kong的服務(wù)
二常侦、配置一個(gè)Route
konga配置kong的路由
三、然后就可以訪問(wèn)了
訪問(wèn)kong的代理
至此贬媒,Kong利用dns輪訓(xùn)訪問(wèn)后端應(yīng)用的配置已經(jīng)完成聋亡。可以自己隨便關(guān)個(gè)應(yīng)用測(cè)試一下际乘。