数据库连接的属性可以动态刷新吗?@RefreshScope加在哪里呢?

来源:12-3 配置属性动态刷新与回滚(附回滚Bug)

zengchen

2019-09-05

写回答

1回答

大目

2019-09-06

您好,很赞的问题哦!数据库连接等配置,在spring boot里,都是加了@ConfigurationProperties注解的。

因此,这部分的配置值(注意这里的用词)也是可以被刷新的,但刷新后对于使用上有没有效果,这个不一定。


配置刷新相关源码的代码入口:org.springframework.cloud.context.refresh.ContextRefresher#refresh,可以在这个方法里面打个断点调试一下。你会发现:环境变量、ConfigurationProperties、@RefreshScope注解的类的配置值都可以刷新。


最后,来解释一下,上面所谓的“配置值(注意这里的用词)也是可以被刷新的,但刷新后对于使用上有没有效果,这个不一定” 是啥意思:

来做个测试:

以应用user-center为例,创建一个DATA-ID,名为user-center.yaml,值为:

management:
  endpoint:
    health:
      show-details: always


 然后,访问 http://localhost:8081/actuator/health  ,可以看到健康检查的详情信息;

访问 http://localhost:8081/actuator/configprops ,可以看到类似如下的结果:

"management.endpoint.health-org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties": {
          "prefix": "management.endpoint.health",
          "properties": {
            "showDetails": "ALWAYS",
            "roles": [
              
            ]
          }
        },


然后,修改Nacos中的配置,改为:

management:
  endpoint:
    health:
      show-details: never

再次访问 http://localhost:8081/actuator/configprops ,可以看到类似如下的结果:

"management.endpoint.health-org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties": {
          "prefix": "management.endpoint.health",
          "properties": {
            "showDetails": "NEVER",
            "roles": [
              
            ]
          }
        },

也就是说,配置的值是被刷新了的。

然而,当你再次访问 http://localhost:8081/actuator/health  ,你依然可以看到健康检查的详情信息,而不是"NEVER" 的效果!!



最后,回到您的问题本身,数据库连接池大小等等,是可以刷新的。并且spring boot自己就做掉了。你要做的就是点击nacos上的按钮,哈哈。

3
2
大目
回复
WittChen
是的。
2022-10-19
共2条回复

Spring Cloud Alibaba微服务从入门到进阶

面向未来微服务:熟练掌握Spring Cloud Alibaba

3084 学习 · 1324 问题

查看课程