You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

@RefreshScope配置实时刷新不生效,调用Actuator接口后参数未更新求助

解决Spring Cloud配置实时重读后参数未更新的问题

1. 调整配置类的注解与属性可变性

你的配置类同时使用@ConstructorBinding和@RefreshScope会引发冲突:@ConstructorBinding让配置类通过构造函数完成不可变绑定,而@RefreshScope需要代理类能在刷新时更新属性。

修改配置类:

  • 移除@ConstructorBinding注解
  • 将顶层属性connectionProfiles改为可变(var)并设置默认值
@ConfigurationProperties(prefix = "merchant-connection")
@RefreshScope
data class ConnectionsProfileProperties(
    var connectionProfiles: Map<String, ConnectionProperties> = emptyMap()
) {
    // 内部数据类保持原有定义不变
    data class ConnectionProperties(
        val smser: SmserProperties,
        val two: TwoProperties
    ) {
        data class SmserProperties(
            val host: String,
            val port: Int,
            val version: Int,
            val priority: Int,
            val dstHost: Int,
            val srcHost: Int,
            val phoneSrc: String,
            val socketTimeout: Duration
        )

        data class TwoProperties(
            val host: String,
            val port: Int,
            val useProxy: Boolean,
            val proxyHost: String?,
            val proxyPort: Int,
            val clerk: String,
            val password: String,
            val product: String,
            val retAddress: String,
            val version: String,
            val needDict: Int,
            val allVendors: Int,
            val avoidSession: Int,
            val readTimeout: Duration,
            val connectionTimeout: Duration
        )
    }
}

2. 确保依赖配置类的组件也加入刷新范围

如果有Service、Controller等组件注入了ConnectionsProfileProperties,这些组件必须添加@RefreshScope,否则会一直持有旧的配置实例,无法获取更新后的值:

@Service
@RefreshScope
class MerchantService(
    private val properties: ConnectionsProfileProperties
) {
    // 业务逻辑中使用properties的方法
}

3. 检查配置文件加载与刷新支持

  • 若配置不在默认的application.yml中,需在配置类上添加@PropertySource并开启可刷新:
    @Configuration
    @PropertySource(value = ["classpath:your-custom-config.yml"], refreshable = true)
    class CustomConfig {
    }
    
  • 确保spring.cloud.refresh.enabled=true(Spring Cloud环境下默认开启,若手动关闭会导致刷新失效)。

4. 验证刷新流程与日志排查

  • 调用refresh接口后,查看应用日志,确认是否存在配置绑定异常或刷新失败的报错
  • 通过/actuator/env端点查看对应配置路径的值是否已更新,验证配置是否正确加载到环境中

5. 排查版本兼容性问题

部分旧版本的Spring Boot/Spring Cloud存在@RefreshScope与@ConfigurationProperties组合的bug,尝试升级到稳定版本(如Spring Boot 2.7.x+、Spring Cloud 2021.0.x+)

内容的提问来源于stack exchange,提问作者Juzo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 11:06:24