简单记录一下,整理架构如下:
一、config-sever配置
1、bootstrap.yml
server:
port: 8001
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/sirlzf/XXX-config-repo.git
username: XXXX
password: XXXX
search-paths: user-service-config,common-config,gateway-config,key-generator-config
bus:
trace:
enabled: true
stream:
kafka:
binder:
brokers: XXXXX:9092
zk-nodes: XXXXX:2181
auto-create-topics: true
management:
security:
enabled: false
eureka:
client:
service-url:
defaultZone: http://XXXXX:8888/eureka/,http://XXXXX:8889/eureka/
instance:
ip-address: XXXXX
prefer-ip-address: true
2、pom.xml
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-bus-kafka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-eureka
3、Application启动类加注解
@EnableDiscoveryClient
@EnableConfigServer
二、config-client
1、bootstrap.yml
server:
port: 7005
spring:
application:
name: user-service
cloud:
config:
profile: dev
discovery:
service-id: config-server
enabled: true
bus:
trace:
enabled: true
stream:
kafka:
binder:
brokers: XXXXXX:9092
zk-nodes: XXXXXXXX:2181
auto-create-topics: true
eureka:
client:
service-url:
defaultZone: http://XXXXXXXXX:8889/eureka/,http://XXXXXXXXXXX:8888/eureka/
2、pom.xml
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-bus-kafka
3、Application启动类注解
@EnableDiscoveryClient
4、Controller自动刷新注解
@RefreshScope
三、通过git的WebHooks配置自动触发/bus/refresh刷新请求
1、WebHooks配置如下:
2、关于/bus/refresh请求说明:
在某种灰度发布的场景中,使用destination参数可以指定只刷某个服务的配置,如下
http://IP:8001/bus/refresh?destination=user-service:application:7005 (POST)
http://47.107.60.69:8001/trace (GET) 查看Spring Cloud Bus事件传播的细节,也可以从里面找到上面destination的值
三、过程中遇到的坑:
在执行http://XXXXX:8001/bus/refresh 进行手动刷新时config server后台一直报如下错误,config client无法显示最新配置
2020-01-07 13:12:14.071 WARN 22995 --- [nio-8001-exec-1] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1578373934071, current=UP, previous=DOWN]
2020-01-07 13:12:14.072 INFO 22995 --- [nio-8001-exec-1] o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed []
2020-01-07 13:12:14.074 INFO 22995 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIG-SERVER/iZw1111111111Z:config-server:8001: registering service...
2020-01-07 13:12:14.081 INFO 22995 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIG-SERVER/iZw1111111111Z:config-server:8001 - registration status: 204
2020-01-07 13:12:43.461 ERROR 22995 --- [ad | producer-2] o.s.k.support.LoggingProducerListener : Exception thrown when sending a message with key='null' and payload='{-1, 2, 11, 99, 111, 110, 116, 101, 110, 116, 84, 121, 112, 101, 0, 0, 0, 12, 34, 116, 101, 120, 116...' to topic springCloudBus:
org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for springCloudBus-0 due to 30012 ms has passed since batch creation plus linger time
经过排查是kafka的server.properties文件中配置的kafka IP地址配置错误,导致config server无法获取消息总线信息(我的kafka服务器与config server不在一个网段),修改配置:
listeners=PLAINTEXT://{内网ip}:9092
advertised.listeners=PLAINTEXT://{外网ip}:9092
重启kafka项目后,刷新成功;
文件被刷新。