在分布式系统中,配置的集中管理尤为重要。Spring Cloud Config 提供了基于 Git 仓库的集中化配置管理方案,而在配置更新后,如何让服务动态刷新而无需重启呢?这就需要利用 Spring Cloud Config 的配置刷新机制以及 Spring Cloud Bus 的消息传播能力。
本文将详细讲解如何通过 /actuator/bus-refresh
接口,实现各个配置客户端(config-client
)的动态刷新。
要实现配置动态刷新,除了基本的 spring-cloud-starter-config
依赖,还需引入消息总线依赖,如 spring-cloud-starter-bus-amqp
或 spring-cloud-starter-bus-kafka
。
Config Server 依赖:
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-kafkaartifactId>
dependency>
Config Client 依赖:
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-kafkaartifactId>
dependency>
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 端点(手动刷新)
你可以在 Git 仓库中修改 application-dev.yml
等配置文件。此时,客户端还未自动获取到最新的配置。
/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 会通过消息总线广播刷新事件,通知所有客户端拉取最新配置。
自动刷新:每个客户端通过消息总线监听刷新事件,自动从 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;
}
}
/actuator/bus-refresh
或 /actuator/refresh
时返回 404
Config Server
的 actuator
端点未正确暴露,确保 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.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
可能原因包括:
@RefreshScope
注解,导致配置未生效。/actuator/bus-refresh
返回 204 No Content
204
表示请求已成功处理,但没有内容返回。配置刷新事件成功触发,但未检测到配置文件的变更。
通过 /actuator/bus-refresh
接口,Spring Cloud Config 提供了分布式系统中动态刷新配置的能力,配合 Spring Cloud Bus 进行事件传播,极大简化了配置更新的操作。关键点包括:
spring-cloud-starter-bus
依赖,并正确配置消息总线(RabbitMQ 或 Kafka)。actuator
端点。@RefreshScope
注解。Spring Cloud 的配置刷新机制极大提高了分布式系统的运维效率与系统灵活性。