Spring Cloud 学习笔记之——10 SpringCloud Bus 消息总线

Spring Cloud 学习笔记之——10 SpringCloud Bus 消息总线,第1张

Spring Cloud 学习笔记之——10 SpringCloud Bus 消息总线

        在SpringCloud Config 中,假如有多个微服务客户端从服务配置中心获取配置信息,一旦远程库中的配置文件发生修改,服务配置中心可以同步更新,但配置信息的修改无法立即同步到每一个微服务上去,我们可以通过向微服务发送 post 请求刷新微服务来使得配置信息的更新,我们希望:1、每一个微服务不用一个个的发送 post 请求,不用一个个的手动刷新,一次通知让每一个微服务都获取到最新的配置信息;2、更进一步的让该修改配置信息的微服务修改信息,不改修改配置信息的微服务不做修改。

        Spring Cloud Bus 是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的时间处理机制和消息中间件的功能,具体分布式自动刷新配置功能,Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新;目前支持两种消息代理:RabbitMQ 和 Kafka

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

 主要功能:

总线

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

基本原理

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

RabbitMQ 的安装(Linux 系统)

先安装 Docker 再运行如下命令:Docker 的安装步骤见 Docker的官方文档
docker run -d --name rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 25672:25672 -p 15671:15671 -p 15672:15672 rabbitmq:management

##############    设置 RabbitMQ 自动启动       #######################
docker update rabbitmq --restart=always

##############    登录RabbitMQ管理端的登录网页        #################
http://服务器IP 地址:15672

默认的用户名称和密码都是 guest

SpringCloud Bus动态刷新全局广播 添加一个新的微服务模块

因为要通知多个微服务,这里需要创建 cloud-config-client-3366 模块

1、POM 文件


        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            com.atyixuan
            cloud-api-commons
            1.0-SNAPSHOT
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

2、bootstrap.yml 配置文件

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"

3、主启动类:com.atyixuan.springcloud.ConfigClientMain3366

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

4、业务类:com.atyixuan.springcloud.controller.ConfigClientController

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${server.port}")
    private String serverPort;

    @Value("${config.info}")
    private String configInfo;

    @GetMapping(value = "/configInfo")
    public String getConfigInfo() {
        return "server port: " + serverPort + "tnn configInfo: " + configInfo;
    }
}
设计思想

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

 2) 利用消息总线触发一个服务端ConfigServer的  /bus/refresh 端点,而刷新所有客户端的配置(更加推荐),相比较于下图,上图不合适的原因如下:

打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新职责;破坏了微服务各节点的对等性;有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改

对配置中心和两个微服务的修改

给cloud-config-center-3344配置中心服务端添加消息总线支持,在其 POM 文件中添加新的依赖:


    org.springframework.cloud
    spring-cloud-starter-bus-amqp

修改 YML 配置文件:

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

  # 使用 rabbitMQ 消息中间件
  rabbitmq:
    host: 服务器 IP 地址
    port: 5672
    username: guest
    password: guest


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

management:
  endpoints:
    web:
      exposure:
        # 这里和上图中的 /bus/refresh 相对应
        include: 'bus-refresh'

给cloud-config-center-3355客户端添加消息总线支持,在其 POM 文件中添加新的依赖:


    org.springframework.cloud
    spring-cloud-starter-bus-amqp

修改 YML 配置文件:

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344
  rabbitmq:
    host: 服务器 ip 地址
    port: 5672
    username: guest
    password: guest


eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

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

 给cloud-config-center-3366客户端添加消息总线支持,在其 POM 文件中添加新的依赖:


    org.springframework.cloud
    spring-cloud-starter-bus-amqp

修改 YML 配置文件:

server:
  port: 3366

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

  rabbitmq:
    host: 服务器 ip 地址
    port: 5672
    username: guest
    password: guest

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

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

测试:

依次启动 

cloud-eureka-server7001
cloud-config-center3344
cloud-config-client-3355 cloud-config-client-3366 浏览器中依次输入:http://config-3344.com:3344/config-dev.yml

 http://localhost:3355/configInfo

 http://localhost:3366/configInfo

 此时修改远程 Git 中的配置信息,将版本号修改为 5

利用 curl 发送 POST 请求刷新 cloud-config-center3344

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

  http://localhost:3355/configInfo浏览器再次访问 http://localhost:3355/configInfo 和 http://localhost:3366/configInfo

结果图分别为:

 两个微服务获取到的配置信息都得到更新,实现了一次修改,处处生效的目的

SpringCloud Bus动态刷新定点通知

        在上一步实现了动态刷新全局广播,我们希望更进一步的只让部分微服务收到通知,一些微服务不收到通知。

 公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}

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

 如果依照上面的微服务,只通知 cloud-config-client-3355 而不通知 cloud-config-client-3366

在刷新服务配置中心 cloud-config-center3344 的时候只刷新 cloud-config-client-3355 即可

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

再次修改 Git 远程库中的配置文件内容

Curl 刷新cloud-config-center3344

  cloud-config-client-3355 收到通知,而cloud-config-client-3366没有收到

整个的流程如下图所示

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5719601.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存