一、简介
 配合SpringcCloudConfig 实现配置的动态刷新。管理和传播分布式系统间的消息,就像一个分布式执行器,用于广播状态更改和时间推送等,是微服务之间的通信通道。
 在微服务系统中,用轻量级的消息代理构建共用的消息主题,并让系统中所有微服务实例连接上来该主题的消息会被所有的实例监听和消费即消息总线。实例监听MQ中的同一个topic(即SpringCloudBus),当一个服务刷新数据的时候,会把这消息放到Topic主题中,这样其他的监听同一个topic的服务就能得到通知,进而更新自己的配置。
注:当前Bus只支持 RabbitMQ和Kafka
二、消息通知
 通过通知Config配置中心,完成对所有的微服务的通知。
1. 配置中心配置
- pom依赖加入bus - 1 
 2
 3
 4- <dependency> 
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
 </dependency>
- yaml配置中引入 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- spring: 
 rabbitmq:
 host: localhost
 port: 5672
 username: admin
 password: 123456
 
 management:
 endpoints:
 web:
 exposure:
 include: 'bus-refresh'
2. 微服务配置
- pom依赖引入 - 1 
 2
 3
 4- <dependency> 
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
 </dependency>
- yaml配置 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- spring: 
 rabbitmq:
 host: localhost
 port: 5672
 username: admin
 password: 123456
 management:
 endpoints:
 web:
 exposure:
 include: "*"
三、测试
- 启动服务后,在github上修改配置,在配置中心可以得到最新配置数据 ,但微服务配置依然没有更新
- curl -X POST "http://localhost:3344/actuator/bus-refresh"执行POST请求,通过配置中心通知所有微服务
- 此时所有的微服务配置将刷新。
四、定点通知
在执行post请求时,告诉配置中心通知哪些微服务进行响应,这样,值通知部分微服务,而不是所有的微服务的配置都随着更新。
- 命令如下: - curl -X POST "http://localhost:3344/actuator/bus-refresh/{dest}"
- 例子:通知两个。 - curl -X POST "http://localhost:3344/actuator/bus-refresh/{微服务名:微服务端口,微服务名:微服务端口}"

...
...
00:00
                    00:00
                
本文为作者原创文章,未经作者允许不得转载。