Spring Cloud Config 配合 Spring Cloud Bus 实现分布式配置自动刷新详解

Spring Cloud Config 配置刷新机制详解

在分布式系统中,配置的集中管理尤为重要。Spring Cloud Config 提供了基于 Git 仓库的集中化配置管理方案,而在配置更新后,如何让服务动态刷新而无需重启呢?这就需要利用 Spring Cloud Config 的配置刷新机制以及 Spring Cloud Bus 的消息传播能力。

本文将详细讲解如何通过 /actuator/bus-refresh 接口,实现各个配置客户端(config-client)的动态刷新。


一、环境搭建

1. 环境准备
  • RabbitMQ:配合 Spring Cloud Bus 使用
  • Config Server:配置中心服务端
  • Config Client:配置中心客户端
2. 引入依赖

要实现配置动态刷新,除了基本的 spring-cloud-starter-config 依赖,还需引入消息总线依赖,如 spring-cloud-starter-bus-amqpspring-cloud-starter-bus-kafka

Config Server 依赖

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

<!-- 使用 RabbitMQ -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

<!-- 或者使用 Kafka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>

Config Client 依赖

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- 使用 RabbitMQ -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

<!-- 或者使用 Kafka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
3. 配置文件

Config Server 配置

server:
  port: 8050
spring:
  cloud:
    config:
      server:
        git:
          uri: https://your-git-repo.git  # Git 仓库地址
          default-label: master  # 拉取的分支
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"  # 暴露 bus-refresh 端点

Config Client 配置

server:
  port: 8001
spring:
  cloud:
    config:
      uri: http://localhost:8050  # 指定 Config Server 地址
  rabbitmq:  # 如果使用 RabbitMQ
    host: localhost
    port: 5672
    username: guest
    password: guest
management:
  endpoints:
    web:
      exposure:
        include: refresh  # 暴露 refresh 端点(手动刷新)

二、配置刷新流程

1. 修改 Git 仓库中的配置

你可以在 Git 仓库中修改 application-dev.yml 等配置文件。此时,客户端还未自动获取到最新的配置。

2. 调用 /actuator/bus-refresh 接口

修改完配置后,调用 Config Server 的 /actuator/bus-refresh 接口触发全局配置刷新。可以通过 curl 或 Postman 发送 POST 请求:

curl -X POST http://localhost:8050/actuator/bus-refresh

如果 config-server 没有检测到新配置,可能会返回 204 No Content,表示刷新事件已触发,但没有新的配置更新。如果有新的配置,Config Server 会通过消息总线广播刷新事件,通知所有客户端拉取最新配置。

3. 客户端刷新配置
  • 自动刷新:每个客户端通过消息总线监听刷新事件,自动从 Config Server 获取最新配置,更新自身配置,无需重启服务。

  • 手动刷新:也可以通过访问 /actuator/refresh 手动刷新客户端配置:

curl -X POST http://localhost:8001/actuator/refresh

返回结果示例:

[
    "config.client.version",
    "修改的配置项"
]

为了让某些 Bean 在配置更新后自动重载,可以使用 @RefreshScope 注解:

@RefreshScope
@RestController
public class ConfigClientController {
    
    

    @Value("${config.property}")
    private String configProperty;

    @GetMapping("/config")
    public String getConfigProperty() {
    
    
        return configProperty;
    }
}

三、常见问题及解决方案

1. 调用 /actuator/bus-refresh/actuator/refresh 时返回 404
  • Config Serveractuator 端点未正确暴露,确保 application.yml 中添加以下配置:
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"
  • Config Client 同样需要正确暴露 refresh 端点:
management:
  endpoints:
    web:
      exposure:
        include: "refresh"

并确保 spring-boot-starter-actuator 依赖已引入:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 客户端未更新配置

可能原因包括:

  • 消息总线(如 RabbitMQ 或 Kafka)未正确配置。
  • Git 仓库中的配置文件没有实际修改。
  • 客户端未使用 @RefreshScope 注解,导致配置未生效。
3. /actuator/bus-refresh 返回 204 No Content

204 表示请求已成功处理,但没有内容返回。配置刷新事件成功触发,但未检测到配置文件的变更。

四、总结

通过 /actuator/bus-refresh 接口,Spring Cloud Config 提供了分布式系统中动态刷新配置的能力,配合 Spring Cloud Bus 进行事件传播,极大简化了配置更新的操作。关键点包括:

  • 使用 spring-cloud-starter-bus 依赖,并正确配置消息总线(RabbitMQ 或 Kafka)。
  • 在服务端和客户端暴露对应的 actuator 端点。
  • 在需要动态刷新的 Bean 上使用 @RefreshScope 注解。

Spring Cloud 的配置刷新机制极大提高了分布式系统的运维效率与系统灵活性。

猜你喜欢

转载自blog.csdn.net/qq_56694800/article/details/143143805