使用Actuator/Refresh刷新Git配置服务器,无法更新配置值求助
1. 修复依赖拼写错误
你的依赖配置里存在明显拼写错误,spring-boot-starter-actuator后缀多了一个x,这会导致依赖无法正常引入,直接影响Actuator的Refresh功能:
// 错误写法 implementation 'org.springframework.boot:spring-boot-starter-actuator'x // 修正后 implementation 'org.springframework.boot:spring-boot-starter-actuator'
2. 修正Git配置文件语法
你的transfer-prod.properties中maintenance=false new不符合properties文件规范,键值对应为key=value格式,空格会被解析为值的一部分,导致配置无法被正确识别。修改为标准格式:
message=transfer.prod maintenance=false
修改后需确保Git提交成功,保证配置服务器能拉取到最新内容。
3. 调整BOM依赖的引入方式
spring-cloud-bus-dependencies是物料清单(BOM),不能直接用implementation引入,正确方式是在依赖管理中导入:
dependencyManagement { imports { mavenBom 'org.springframework.cloud:spring-cloud-bus-dependencies:4.1.0' } }
如果不需要Spring Cloud Bus功能,直接移除该依赖即可,单独使用Actuator Refresh不需要Bus依赖。
4. 替换过时配置项
endpoints.actuator.enabled=true是Spring Boot旧版本配置,新版本应使用:
management.endpoint.actuator.enabled=true
不过你已配置management.endpoints.web.exposure.include=*,该配置已包含暴露Actuator端点的功能,可直接保留。
5. 验证配置服务器的配置更新状态
调用客户端Refresh接口前,先访问配置服务器端点http://localhost:8888/transfer/prod,确认返回的JSON中包含最新的maintenance值。如果配置服务器未更新,说明Git修改未被拉取,可重启配置服务器,或通过spring.cloud.config.server.git.refresh-rate配置Git刷新间隔。
6. 确认客户端配置加载逻辑
由于你引入了spring-cloud-starter-bootstrap,Spring Cloud 2022+版本默认禁用bootstrap机制,该依赖会重新启用它。当前spring.config.import=optional:configserver:http://localhost:8888配置正确,可额外添加spring.cloud.config.profile=prod明确指定拉取的环境配置。
7. 测试刷新流程
完成以上修正后,按以下步骤验证:
- 修改Git上
transfer-prod.properties的maintenance值并提交。 - 确认配置服务器返回最新配置。
- 调用客户端
POST http://localhost:8081/actuator/refresh接口。 - 调用
GET http://localhost:8081/value查看返回值是否更新,同时检查Refresh接口返回是否包含maintenance。
内容的提问来源于stack exchange,提问作者Armendo Chandra

