Kong整合Consul - Kong最佳实践

整合Kong和Consul

一、先装个Consul,参考:Consul 快速入门
注意事项:启动时,可以添加 -dns-port=53 这样就可以走默认的DNS端口了。

二、然后装个Kong,参考:Kong的简介和安装使用Docker安装Kong
注意事项:如果使用docker方式执行,网络需要使用host方式。
附Docker执行方式:

[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信息

四、配置一个DNS SRV,后面用来做轮训

[root@local13 ~]# cat /etc/consul.d/web.json 
{"service": {"name": "web", "tags": ["springboot"], "port": 8080}}
# local12同local13

# 查下结果
[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就整合完成了。然后,我们再整合Spring Cloud,实现Kong网关到APP的DNS轮训。

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相关内容

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入口添加相关注解

@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已经完成。运行下看看效果吧。

spring cloud整合consul

通过Kong的DNS轮训访问应用

这次使用konga(kong的ui管理工具)进行配置。

一、配置一个Service


konga配置kong的服务

二、配置一个Route

konga配置kong的路由

三、然后就可以访问了


访问kong的代理

至此,Kong利用dns轮训访问后端应用的配置已经完成。可以自己随便关个应用测试一下。

转载于:https://my.oschina.net/u/1404949/blog/3039504

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值