关于Springcloud Bus的使用 小结

29 篇文章 1 订阅
9 篇文章 0 订阅

目录

 

1. Springcloud Bus概述

2. RabbitMQ配置

3. Springcloud Bus动态刷新

3.1 全局广播

3.1.1 设计思想

3.1.2 配置消息总线

3.1.3 配置更新测试

3.2 定点通知


1. Springcloud Bus概述

Spring Cloud Bus links the nodes of a distributed system with a lightweight message broker. This broker can then be used to broadcast state changes (such as configuration changes) or other management instructions. A key idea is that the bus is like a distributed actuator for a Spring Boot application that is scaled out. However, it can also be used as a communication channel between apps. This project provides starters for either an AMQP broker or Kafka as the transport.

1) 由于Spring Cloud Config更新配置信息的方式不是很方便,所以接下来总结一下Spring Cloud Bus配合Spring Cloud Config使用实现配置的动态刷新。

在微服务架构的系统中,通常会使用轻量级的消息代理,并让系统中所有微服务实例都接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称之为消息总线。

在总线的各个实例,都可以很方便地广播一些需要让其他连接在该主题的实例都知道的信息。

2) 原理:ConfigClient实例都监听MQ中同一个topic(默认是SpringcloudBus),当一个服务刷新数据的时候,它会把这个消息放到topic,然后其他监听同一topic的服务就能得到通知并更新自身的配置。

2. RabbitMQ配置

1) 先安装erlang下面的otp_win64_23.2.exe。

2) 然后安装rabbitMQ下面的rabbitmq-server-3.8.9.exe。

3) 进入RabbitMQ安装目录下的sbin目录,当前cmd输入:.\rabbitmq-plugins.bat enable rabbitmq_management

4) 启动成功后就能访问(输入账号密码都是guest):localhost:15672/

3. Springcloud Bus动态刷新

以下案例代码的Github地址

四个modules分别是:cloud-eureka-server7001,cloud-config-center-3344,cloud-config-client-3355,cloud-config-client-3366。

3.1 全局广播

3.1.1 设计思想

利用消息总线触发一个服务端ConfigServer,从而刷新所有客户端的配置。

3.1.2 配置消息总线

1) cloud-config-center-3344,cloud-config-client-3355,cloud-config-client-3366添加spring-cloud-starter-bus-amqp。

<!-- 添加消息总线RabbitMQ支持 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>

2) cloud-config-client-3355,cloud-config-client-3366添加rabbitMQ配置。

rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3) cloud-config-center-3344添加rabbitMQ配置。

rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

#rabbitmq相关设置 ,暴露 bus刷新配置的端点
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

3.1.3 配置更新测试

1) 修改Github配置中心的配置文件(修改版本号):

 配置中心cloud-config-center-3344的即时获取到了最新的配置信息(localhost:3344/main/config-dev.yml)。

当前的cloud-config-client-3355和cloud-config-client-3366配置信息还是旧的(localhost:3355/configInfo)。

2) 发送Post请求一次性刷新所有客户端的配置信息。

curl -X POST "http://localhost:3344/actuator/bus-refresh"

这个时候的cloud-config-client-3355和cloud-config-client-3366配置信息都更新了。

 

:如果返回405,记得检查一下步骤3.1.2的相关依赖是否已经引入,以及与其他依赖的版本是否一致。

3.2 定点通知

简单来说就是,不想全部通知而只想更新指定实例的配置信息。

1) 修改Github配置中心的配置文件(修改版本号):

2) 配置中心cloud-config-center-3344的即时获取到了最新的配置信息(localhost:3344/main/config-dev.yml)。

3) 发送Post请求一次性刷新cloud-config-client-3355的配置信息。

curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"

这个时候只有cloud-config-client-3355的配置信息更新了。

1. 关于Springcloud Stream消息驱动可以参考:关于Springcloud Stream消息驱动的使用小结

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值