8-Bus消息总线


提示:本文应与上一边文章《7-Config服务配置》一同食用,效果更佳

一、基本概念

1、作用

Spring Cloud Bus将分布式系统的节点与轻量级消息代理链接

​ 这可以用于广播状态更改(例如配置更改)或其他管理指令。一个关键的想法是,Bus就像一个扩展的Spring Boot应用程序的分布式执行器,但也可以用作应用程序之间的通信渠道。当前唯一的实现是使用AMQP代理作为传输,但是相同的基本功能集(还有一些取决于传输)在其他传输的路线图上。

说人话:就是配置一处修改,就会扩散到所有的微服务。《7-Config服务配置》后面提到当配置修改后所有的配置应用端的微服务都要手动执行发送一次刷新的post命令。而消息总线就是只需向配置中心3344发送一次post刷新命令,即可实现所有微服务跟随刷新。

2、什么是总线

就是通过消息队列达到广播的效果
          我们要广播每个消息时,主要放到某个topic中,所有监听的节点都可以获取到

3、设计思想(重点)

  1. 利用消息总线触发一个客户端/bus/refresh从而刷新所有客户端配置
  2. 利用消息总线触发一个服务端 ConfigServer 的/bus/refresh端点从而刷新所有客户端
  • 明显更合适
    1. 打破了微服务职责单一性
    2. 破坏了微服务各节点的对等性
    3. 有一定局限性 ,微服务歉意时网络地址常常发生变化
  • 以下搭建的模块是依据二

二、模块搭建

新建一个cloud-config-client-3366模块 ,与《7-Config服务配置》的3355一致。作为2个配置的使用端。如果执行一次以下命令,2个微服务都刷新,即证明Bus服务总线生效。

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

1、配置中心微服务3344

application.yml

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/rewind922/springcloud-config.git #GitHub上面的git仓库名字
          ####搜索目录
          search-paths:
            - springcloud-config
      ####读取分支
      label: master
  #rabbitmq相关配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

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

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

主启动类

@SpringBootApplication
@EnableConfigServer
public class ConfigCenter3344Main {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenter3344Main.class,args);
    }
}

2、配置使用端3355、3366

bootstrap.yml配置文件(优先于application执行)

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/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: "*"

主启动类

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355
{
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class, args);
    }
}

简单业务

@RestController
@RefreshScope
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return configInfo;
    }
}

3、gitee上的仓库

springcloud-config/config-dev.yml

config:
  info: v1.3

4、测试

访问:

  1. http://localhost:3344/master/config-dev.yml

  2. http://localhost:3355/configInfo

  3. http://localhost:3366/configInfo

当gitee上的配置更改时,3344配置中心会自动随之更改,3355和3366则不会自动更改。

全局通知

当向配置中心发送刷新的post请求后,3355和3366都随之更改

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

局部通知

只向其中一个微服务发送通知,达到精确修改。其他微服务不会修改。

{destination}:微服务名:端口

curl -X POST "http://localhost:配置中心端口号/actuator/bus-refresh/{destination}"

本例中

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

  目录