09 Spring Cloud Bus结合RabbitMQ动态刷新配置

本文详细介绍了如何使用Spring Cloud Bus结合RabbitMQ实现微服务配置的动态刷新,包括在Config Server和Client端添加Bus依赖,配置RabbitMQ,使用@RefreshScope注解,以及通过手动和自动方式刷新配置。手动刷新通过调用/bus-refresh接口完成,自动刷新则借助GitHub的Webhooks功能,当配置文件变更时,自动触发配置中心更新配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Cloud Bus结合RabbitMQ消息队列动态刷新配置架构图:

(1)为配置中心Config Server端添加Spring Cloud Bus依赖

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

(2)为配置中心Config Server端配置RabbitMQ消息中间件

spring:
  application:
    name: MALL-CONFIG
  cloud:
    config:
      server: # Config 服务端配置,增加一个git仓库存放配置文件
        git:
          uri: https://github.com/lihailin9073/config-repo.git
          username: lihailin9073
          password: LHLlhl870104
          basedir: D:\javasource\config # 指定加载的配置文件存放目录
  rabbitmq: # 配置RabbitMQ
    host: 39.98.172.148
    port: 5672
    username: admin
    password: 123456

eureka:
  client:
    service-url:
      defaultZone: http://system:sys123@peer1:8761/eureka/ # 单点Eureka Server时使用,将微服务注册到单个Eureka节点上,适合用于本地开发和测试
      #defaultZone: http://system:sys123@peer1:8761/eureka/,http://system:sys123@peer2:8762/eureka/,http://system:sys123@peer3:8763/eureka/ # 集群Eureka Server时使用,将微服务注册到集群中的多个Eureka节点上,适合用于线上生产环境

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

注意必须暴露监控端点,以便后续调用配置中心的/bus-refresh 接口

 

(3)为配置中心Config Client端添加Spring Cloud Bus依赖

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

(4)在配置中心Config Client端配置RabbitMQ消息中间件

spring:
  application:
    name: ORDER-SERVICE
  cloud:
    config:
      discovery:
        enabled: true
        service-id: MALL-CONFIG # 配置中心的微服务名称
      profile: dev # 加载的配置文件后缀名称,与前面的应用名称共同构成完整的配置文件名称:{application}-{profile}.yml=order-service-dev.yml
  rabbitmq: # 配置RabbitMQ
    host: 39.98.172.148
    port: 5672
    username: admin
    password: 123456

eureka:
  client:
    service-url:
      defaultZone: http://system:sys123@peer1:8761/eureka/ # 单点Eureka Server时使用,将微服务注册到单个Eureka节点上,适合用于本地开发和测试
      #defaultZone: http://system:sys123@peer1:8761/eureka/,http://system:sys123@peer2:8762/eureka/,http://system:sys123@peer3:8763/eureka/ # 集群Eureka Server时使用,将微服务注册到集群中的多个Eureka节点上,适合用于线上生产环境

(5)显示拉取的配置文件的属性

编写一个控制器类,并使用@RefreshScope注解将该类标注为一个动态刷新类,提供一个接口来显示获取到的配置文件中的属性值:

package cn.org.xcore.mall.order.controller;

import cn.org.xcore.mall.order.config.CompanyConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 配置中心客户端测试控制器
 *
 * @author 李海林 手机:13802780104|微信:lihailin9073|Email:767679879@qq.com
 * @copyright 个人开发者李海林版权所有,产品详情及技术服务请登录官网查询[http://www.x-core.org.cn]
 * @create 2019-09-09 11:05
 */
@RefreshScope
@RestController
@RequestMapping("/test")
public class TestConfigController {
    // 通过Config配置中心从Git仓库拉取的配置文件中读取属性的值
    @Value("${developer}")
    private String developer;
    @Value("${email}")
    private String email;
    @Value("${description}")
    private String description;
    @Autowired
    private CompanyConfig companyConfig;
    // 通过Config配置中心从Git仓库拉取的配置文件中读取属性的值

    @GetMapping("/get_git_config")
    public Object getGitConfig() {
        String config = "developer="+this.developer + ", email="+ this.email + ", description="+this.description + ", companyConfig="+ companyConfig.toString();
        return config;
    }
}

注意,必须要将注解@RefreshScope 加在所有使用了Git配置文件中的属性的类头部,否则将无法实现动态刷新。

启动order-service微服务,在浏览器访问该接口: http://localhost:9101/test/get_git_config 

显示结果如下图所示:

其中的developer属性的值为lihailin9073,此时不要重启该微服务,通过下面的第(6)步就可以实现无重启刷新配置信息。

 

(6)无重启手动刷新配置

前往GitHub上的仓库找到该配置文件然后修改developer的值,修改后的Git仓库配置文件如下图所示:

修改完成后,在命令行发送一个post请求到Config Server配置中心的 /bus-refresh 接口,触发配置中心重新拉取Git仓库中的配置文件:

#curl -v -X POST http://localhost:9101/actuator/bus-refresh 

注意,在这整一个操作过程中都没有重启过该微服务,此时再次通过浏览器访问接口查看developer的值:

http://localhost:9101/test/get_git_config

会发现修改后的值已经被加载下来了,显示结果如下图所示:

到此,实现了微服务的无重启刷新配置的功能;实际项目中微服务个数成千上百个,是不可能一个个去重启它们的,因此在开发工作中如果使用了Spring Cloud,那么必然就需要无重启刷新的功能。但是,手动刷新毕竟也不够方便,可以结合Git平台提供的Webhooks功能,实现自动刷新。

 

(7)无重启自动刷新配置

当Config Server配置中心部署在外网时,可以通过GitHub或其它Git平台的Webhooks推送功能实现自动刷新;如果是本地开发和测试,可以使用natapp内网穿透工具(https://natapp.cn)将外网域名映射到本地机器,为Webhooks提供推送接口;以GitHub为例,配置如下所示:

其中的Payload URL指的是配置中心Spring Cloud Config Server部署的服务器域名地址,格式为:http://www.domain.com/monitor。

配置好GitHub上的Webhooks推送以后,还需要为Spring Cloud Config Server端增加spring-cloud-config-monitor依赖,如下所示:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-monitor</artifactId>
</dependency>

添加完依赖后,将配置中心Config Server部署到外网可访问的服务器上,然后解析一个域名过去(如果不解析域名,就将服务器的IP地址填写到GitHub的Webhooks中),测试可正常访问后,在GitHub仓库中修改配置文件,GitHub的Webhooks就会推送通知到 /monitor 接口通知配置中心拉取新的配置文件。

以解析的【http://mall-config.wzliulan.com/monitor】域名为例,打开浏览器访问接口:

http://mall-config.wzliulan.com/test/get_git_config 

这个接口会显示配置文件中的几个属性值,通过它的显示结果可以检查配置是否可以自动刷新。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值