Config配置⾃动更新
实现⼀次通知处处⽣效
在微服务架构中,我们可以结合消息总线(Bus)实现分布式配置的⾃动更新(Spring Cloud Config + Spring Cloud Bus)
消息总线Bus
所谓消息总线Bus,即我们经常会使⽤MQ消息代理构建⼀个共⽤的Topic,通过这个Topic连接各个微服务实例,MQ⼴播的消息会被所有在注册中⼼的微服务实例监听和消费。换⾔之就是通过⼀个主题连接各个微服务,打通脉络。
Spring Cloud Bus(基于MQ的,⽀持RabbitMq/Kafka) 是Spring Cloud中的消息总线⽅案,Spring Cloud Config + Spring Cloud Bus 结合可以实现配置信息的⾃动更新。

Spring Cloud Config + Spring Cloud Bus 实现⾃动刷新
MQ消息代理,这里选择使⽤RabbitMQ(不细说MQ原理,暂可认为是一个存数据的组件),ConfigServer和ConfigClient都添加都消息总线的⽀持以及与RabbitMq的连接信息
- Config Server服务端添加消息总线⽀持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- ConfigServer添加MQ相关配置
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
- 微服务暴露端⼝
management:
endpoints:
web:
exposure:
include: bus-refresh
建议暴露所有的端⼝
management:
endpoints:
web:
exposure:
include: "*"
- 重启各个服务,更改配置之后,向配置中⼼服务端发送post请求http://localhost:9006/actuator/bus-refresh,各个客户端配置即可⾃动刷新
在⼴播模式下实现了⼀次请求,处处更新,如果我只想定向更新呢?
在发起刷新请求的时候http://localhost:9006/actuator/bus-refresh/cloud-eureka-server:8081
即为最后⾯跟上要定向刷新的实例的 **服务名:端⼝号** 即可