zl程序教程

您现在的位置是:首页 >  其他

当前栏目

消息总线(Spring Cloud Bus)

2023-03-07 09:16:07 时间

前言

Spring Cloud Bus是什么?

  • Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动态刷新
  • Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架, 它整合了Java的事件处理机制和消息中间件的功能。 Spring Clud Bus目前支持RabbitMQKafka

Spring Cloud Bus能干吗?

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

为何被称为总线?

  • 在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。

基本原理

  • ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus)。当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。

使用说明:

环境:先具备良好的RabbitMQ环境

SpringCloud Bus动态刷新全局广播

  1. 给配置中心服务端添加消息总线支持
<!--添加消息总线RabbitMQ支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. bootstrap.yml
server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: git@github.com:6666/springcloud-config.git #GitHub上面的git仓库名字
        ####搜索目录
          search-paths:
            - springcloud-config
      ####读取分支
      label: master


#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka



##rabbitmq相关配置,暴露bus刷新配置的端点
rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'
  1. 客户端多个)添加消息总线支持

pom

<!--添加消息总线RabbitMQ支持-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取
      uri: http://localhost:3344 #配置中心地址k
#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"   # 'refresh'
  1. 启动注册中心,配置中心,客户端, 通过服务端、客户端访问git上的远程配置文件 修改git上的配置文件,cmd 执行: curl -X POST “http://localhost:3344/actuator/bus-refresh” 接着访问多个客户端可以发现都获取到了最新的配置信息,实现了一次修改,广播通知,处处生效。

SpringCloud Bus动态刷新定点通知

假设有两个客户端(端口号为3355 和 3366),修改了远程配置文件,只想要端口号为3355的更新,3366的不更新。指定具体某一个实例生效而不是全部 。

/bus/refresh请求不再发送到具体的服务实例上,而是发给config server并通过destination参数类指定需要更新配置的服务或实例

公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}
destination: spring.application.name + server.port

这里以刷新运行在3355端口上的config-client为例

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

什么都没有就代表执行成功。