十一、SpringCloud Bus 消息总线

概述

本文测试源码:码云

  • Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新
    在这里插入图片描述

Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,
它整合了Java的事件处理机制和消息中间件的功能。
Spring Clud Bus目前支持RabbitMQKafka`。

什么是总线

  • 在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播─些需要让其他连接在该主题上的实例都知道的消息。
    基本原理
  • ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus)。当一个服务刷新i数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。
  • 尚硅谷ActiveMQ教程(MQ消息中间件快速入门)

在这里插入图片描述
在这里插入图片描述

作用

Spring Cloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道

RabbitMQ环境配置

安装Erlang,

下载地址:http://erlang.org/download/otp_win64_21.3.exe

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

安装RabbitMQ,

  1. 下载地址:https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe
    在这里插入图片描述

  2. 进入RabbitMQ安装目录下的sbin目录
    在这里插入图片描述
    开启可视化插件

     rabbitmq-plugins enable rabbitmq_management
    

在这里插入图片描述
在这里插入图片描述

	http://localhost:15672/
	输入账号密码并登录: guest guest
	安装完成

SpringCloud Bus动态刷新全局广播

在这里插入图片描述
配置和上一篇文章SpringCloudConfig配置基本(端口号需要修改)一致

1) 利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置

在这里插入图片描述

2) 利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置(更加推荐)

在这里插入图片描述### 两种方式对比
在这里插入图片描述

配置消息总线

pom文件

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

给cloud-config-center-3344配置中心服务端添加消息总线支持

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri:  https://github.com/hhf19906/springcloud-config.git  #git@github.com:hhf19906/springcloud-config.git
          search-paths:
            - springcloud-config
      label: master

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

eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh' #注意这个

给cloud-config-client-3355客户端添加消息总线支持

cloud-config-center-3366 类似只是修改端口号

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344

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

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*" #经过测试客户端可以不配置 配置中心发来的广播也可以刷新该服务配置 最好还是配置

测试
在这里插入图片描述

  1. 先把3344 3355 3366 都启动了
  2. 3个配置都是version4
  3. 修改github中的配置为 version5
  4. 刷新cloud-config-center-3344 发现配置已经变为version5
  5. 刷新cloud-config-client-3355 和3366 发现配置还是version4 没有更新
  6. http://localhost:3344/actuator/bus-refresh post请求 向服务中心发送刷新 进行统一刷新
  7. 最后 客户端已经同步到 version5的配置
    使用idea的http测试
#消息总线 服务端刷新 配置 信息 客户端统一刷新
POST http://localhost:3344/actuator/bus-refresh
Content-Type: application/json

{}
###

最终效果 向服务中心发送刷新请求 客户端配置已经成功同步
在这里插入图片描述

SpringCloud Bus动态刷新定点通知

在这里插入图片描述

只通知cloud-config-client-3355服务
http://localhost:3344/actuator/bus-refresh/config-client:3355

总结
在这里插入图片描述

原文地址:https://www.cnblogs.com/idcode/p/14551373.html