一、概述

在Spring Cloud(7)配置中心——Config中,服务端配置信息发生改变后,需要手动通知客户端去刷新。如果服务数量众多,则不利于维护。

Spring Cloud Bus可以配合Spring Cloud Config实现配置信息的动态刷新。

Spring Cloud Bus使用轻量级的消息代理,连接起分布式系统的各个节点,使用消息代理来广播状态更新或其他管理指令。 关键点是,消息总线就像是Spring Boot应用程序扩展的分布式执行器,可以作为应用之间的通信渠道。 目前集成了AMQP和Kafka的消息代理。

  • 概念

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

  • 原理

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

二、流程图

spring cloud config 如何刷新配置 spring cloud config bus_服务端

三、代码实现

(一)安装RabbitMQ

1. 安装Erlang

官网下载

2. 安装RabbitMQ

官网下载 进入sbin目录:
D:\dev\RabbitMQ Server\rabbitmq_server-3.8.14\sbin
执行:

rabbitmq-plugins enable rabbitmq_management

启动RabbitMQ:

rabbitmqctl start_app

访问地址:http://localhost:15672/

账号:guest

密码:guest

spring cloud config 如何刷新配置 spring cloud config bus_服务端_02

(二)config服务器模块集成bus

pom.xml添加spring-cloud-starter-bus-amqp依赖:

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

application.yml添加相关配置:

server:
  port: 3333

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机版
      #defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/ym451808578/cloud-config.git  #git仓库地址
          search-paths: #搜索目录
            - cloud-config
          default-label: master   #分支
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"
  endpoint:
    bus-refresh:
      enabled: true

启动项目,访问http://localhost:3333/config-dev.yml:

spring cloud config 如何刷新配置 spring cloud config bus_spring_03

(三)config客户端模块集成bus

pom.xml:

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

bootstrap.yml:

server:
  port: 3334   #另一个客户端端口号3335

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机版
      #defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
spring:
  application:
    name: cloud-config-client 
  cloud:
    config:
      label: master  #分支名称
      name: config  #配置文件名称
      profile: dev   #后缀名称    组合后表示master分支上的config-dev.yml配置文件
      uri: http://localhost:3333   #配置中心地址  即 http://localhost:3333/master/config-dev.yml

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

management:
  endpoints:
    web:
      exposure:
        include: "*"

分别启动2个客户端,访问:http://localhost:3334/config/info

spring cloud config 如何刷新配置 spring cloud config bus_客户端_04

(四)测试消息总线

  1. 修改gitee仓库中的配置文件,version修改为3
  2. 访问config服务端:
    http://localhost:3333/config-dev.yml 服务端配置信息已更新,客户端未更新
  3. 发送命令:
curl -X POST "http://localhost:3333/actuator/bus-refresh"

客户端均得到更新。
若发送:

curl -X POST "http://localhost:3333/actuator/bus-refresh/cloud-config-client:3334"

只通知3334客户端更新。

注:向服务端发送指令

  1. rabbitMQ