Spring Cloud Bus 消息总线
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
即为最后面跟上要定向刷新的实例的 服务名:端口号 即可
评论已关闭