使用SpringCloud Bus完善Config的动态刷新

SpringCloud Bus概述

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

Bus 目前只支持RabbitMQ和Kafka

什么是总线

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

基本原理

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

使用SpringCloud Bus解决Config的动态刷新问题

在上篇文章SpringCloud Config配置中心的使用中留下了一个问题,如果有多个config的使用者,如何在配置修改的时候通知它们?

设计思路:

  • 利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置
  • 利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置

显然第二种思想会更好,原因有:

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

我们新建模块cloud-config-client3366作为config的第二个使用者,将它和cloud-config-client3355的pom依赖都修改为如下(为config服务器也添加spring-cloud-starter-bus-amqp依赖):

<!-- 添加rabbitmq 总线支持 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

yaml配置:(文件名也要是bootstrap.yml

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master # 分支名称
      name: config # 配置文件名称
      profile: dev # 读取的profile
      uri: http://localhost:3344 #配置中心的地址

  rabbitmq: #rabbitmq相关配置,请为3355也添加这一部分
    port: 5672
    host: localhost
    username: guest
    password: guest

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

启动类和controller的写法和3355一样。

cloud-config-center-3344的yaml配置修改为:

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: http://git.codetool.top/RhettPeng/springcloud-config.git # 仓库地址
          search-paths:
            - springcloud-config
      label: master   # 读取分支
  # 添加rabbitmq相关
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

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

# 暴露bus刷新的端点
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

到此配置成功,只要git仓库中的配置文件修改的时候向 http://localhost:3344/actuator/bus-refresh 发出post请求(可以使用webhook),就能通知所有的配置使用者刷新配置。

查看rabbitmq的exchange:

使用SpringCloud Bus完善Config的动态刷新

补充:定点通知

bus可以指定通知具体一个实例而不是全部。只需要在 http://localhost:3344/actuator/bus-refresh 后面加上 /destination,其中destination为服务ID。之前有介绍可以通过eureka.instance.instance-id指定。

例如post http://localhost:3344/actuator/bus-refresh/config-client:3355 即可只通知3355,而不通知其他使用者。 使用SpringCloud Bus完善Config的动态刷新

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/%e4%bd%bf%e7%94%a8springcloud-bus%e5%ae%8c%e5%96%84config%e7%9a%84%e5%8a%a8%e6%80%81%e5%88%b7%e6%96%b0/

(0)
彭晨涛彭晨涛管理者
上一篇 2020年3月22日 21:03
下一篇 2020年3月23日

相关推荐

发表回复

登录后才能评论