Spring Cloud Bus 消息总线实现配置自动刷新

why

当微服务太多的时候,服务之间需要建立通信或一个服务的改变需要广播到所有其它服务,这时就需要有一个总线来承担对应的职责。

what

spring cloud bus 是通过轻量消息代理连接各个分布的节点,就类似于单体应用中常说的消息总线。主要作用是在一个或多个应用之间建立通信频道。

how

主要通过AMQP(Advanced Message Queuing Protocol)消息代理作为通道来实现消息通信。

应用场景

  • 配置中心:当git仓库配置修改的时候,能够自动通知到所有服务重新拉取最新配置

消息总线实现所有服务配置刷新

整体架构

开始之前,我们先来看下整体架构:
在这里插入图片描述
这时Spring Cloud Bus 做配置更新的步骤如下:

  1. 在git仓库提交代码,通过WebHook触发post请求给bus/refresh端点
  2. config-server接收到请求并发送给Spring Cloud Bus
  3. Spring Cloud Bus接收到消息后会广播给其它客户端config-client
  4. 其它config-client接收到通知后,会重新拉取config-server中的配置
  5. 最终全部客户端钧获取到最新配置

RabbitMQ的安装

参考:RabbitMQ在Windows环境下部署(简单有效)

动态刷新配置

使用Spring Config Bus需要配合Spring Cloud Config一起使用,结合上一篇文章的示例整合Spring Cloud Bus。Spring Cloud Config Demo(外部集中化配置管理示例)

config-server 添加消息总线支持

在pom.xml中添加依赖:

        <!--使用消息总线刷新配置时添加-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

添加配置文件application-amqp.yml(主要是添加了RabbigMQ配置和暴露了刷新配置的Actuator端点):

server:
  port: 8904
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/yousiqi/springcloud-config.git
          clone-on-start: true # 开启启动时直接从git获取配置
  rabbitmq: #rabbitmq相关配置
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'
config-client 添加消息总线支持

pom.xml中添加依赖:

        <!--使用消息总线刷新配置时添加-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

添加配置文件bootstrap-amqp1.yml

server:
  port: 9004
spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev #启用环境名称
      label: dev #分支名称
      name: config #配置文件名称
      discovery:
        enabled: true
        service-id: config-server
  rabbitmq: #rabbitmq相关配置
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
management:
  endpoints:
    web:
      exposure:
        include: 'refresh'
启动项目
  1. 启动eureka-server

  2. 启动config-server,配置active profiles为amqp在这里插入图片描述

  3. 启动config-client,配置active profiles为amqp1在这里插入图片描述

  4. 登录RabbigMQ控制台可以发现Spring Cloud Bus创建了一个SpringCloudBus的交换机和以 springCloudBus.anonymous开头的队列:

在这里插入图片描述在这里插入图片描述

动态刷新配置演示
  1. 修改git仓库dev分支下的config-dev.yml文件
  2. 调用注册中心的接口刷新配置:POST:http://localhost:8904/actuator/bus-refresh
  3. 调用config-client测试接口验证配置是否更新:http://localhost:9004/configInfo

如果需要刷新指定的实例的配置可以使用一下格式刷新:
http://localhost:8904/actuator/bus-refresh/{destination}
我们这里以刷新运行在9004端口上的config-client为例:
http://localhost:8904/actuator/bus-refresh/config-client:9004

配合WebHook实现全自动刷新配置

WebHook是一个钩子函数,当修改仓库文件或push文件的时候,可以发送请求到WebHook的url,这里我们使用github来配置:在这里插入图片描述
这里是将本地的http://localhost:8904/actuator/bus-refresh地址映射到了公网地址:http://evq7dx.natappfree.cc/actuator/bus-refresh,供WebHook进行调用,否则localhost无法被调用

通过以上的配置后,你再也不需要去手动刷新或启动项目了。当你在git仓库中修改了文件,自然会触发WebHook通知到config-server,然后发送消息到Spring Cloud Bus,Spring Cloud Bus会广播到其它的实例上,最终在不知不觉中完成了所有实例的配置刷新。

Other

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我思知我在

原创不易,多多一键三连

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值