SpringCloud Bus消息总线

SpringCloud Bus消息总线简单来说就是springcloud对mq的封装。目前只支持:rabbitmq和kafka。

通常的使用场景:修改了分布式配置中心的配置,通过bus发送消息,所有订阅的服务同时刷新最新配置,也就是实时更新配置文件,不用重启所有微服务。

一、在不使用SpringCloud Bus情况下修改了配置文件,刷新服务配置通常是单台服务器刷新,效率慢。刷新配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
server:
  port: 8081
# 开启服务断点权限
management:
  endpoint:
    web:
      exposure:
        include: "*"

更新git配置,如当前age=21,更新age=88,调用服务刷新接口:http://127.0.0.1:8081/actuator/refrest

调用Controller验证:http://127.0.0.1:8081/getAge 返回age=88,但是,调用http://127.0.0.1:8082/getAge 返回结果仍然是age=21,所以需要bus总线对所有服务或集群同时刷新。

二、使用SpringCloud Bus情况下修改了配置文件,同时刷新所有服务。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
server:
  port: 8081
# 开启bus刷新
management:
  endpoint:
    web:
      exposure:
        include: bus-refresh

更新git配置,如当前age=21,更新age=88,调用服务刷新接口(注意和上面接口不一样):http://127.0.0.1:8081/actuator/bus-refresh

调用Controller验证:http://127.0.0.1:8081/getAge 返回age=88,调用http://127.0.0.1:8082/getAge 返回结果也是age=88,也就是虽然只调用了8081刷新接口,但是8082也同时刷新了最新配置,因为8082消费了mq消息

原理图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值