[英]@RefreshScope not working - Spring Boot
I am following the approach described here: https://github.com/jeroenbellen/blog-manage-and-reload-spring-properties , the only difference is that in my case, the properties are being used in multiple classes so I have put them all in one utility class CloudConfig
and I refer to its variables using the getters. 我遵循这里描述的方法: https : //github.com/jeroenbellen/blog-manage-and-reload-spring-properties ,唯一的区别是在我的情况下,属性被用于多个类,所以我有将它们全部放在一个实用程序类
CloudConfig
并使用getter引用它的变量。 This is what the class looks like: 这就是这个类的样子:
@Configuration
@RefreshScope
public class CloudConfig {
static volatile int count; // 20 sec
@Value("${config.count}")
public void setCount(int count) {
this.count = count;
}
public static int getCount() {
return count;
}
}
and I use the variable count
in other classes like CloudConfig.getCount()
. 我在其他类中使用变量
count
,如CloudConfig.getCount()
。 I am able to load the properties on bootup just fine but I am not able to dynamically update them on the fly. 我能够在启动时加载属性,但我无法动态更新它们。 Can anyone tell what I am doing wrong?
谁能说出我做错了什么? If instead of making this config class, I do exactly what the tutorial describes everything works fine but I am having trouble adapting it to my usecase.
如果不是制作这个配置类,我完全按照教程描述的一切正常工作但我无法适应我的用例。 Can anybody tell what I am missing?
任何人都可以告诉我缺少什么吗?
Try using @ConfigurationProperties instead. 请尝试使用@ConfigurationProperties 。 eg
例如
@ConfigurationProperties(prefix="config")
public class CloudConfig {
private Integer count;
public Integer count() {
return this.count;
}
public void setCount(Integer count) {
this.count = count;
}
}
The reference doc from spring cloud states: 来自spring cloud的参考文档指出:
@RefreshScope works (technically) on an @Configuration class, but it might lead to surprising behaviour: eg it does not mean that all the @Beans defined in that class are themselves @RefreshScope.
@RefreshScope(在技术上)在@Configuration类上工作,但它可能会导致令人惊讶的行为:例如,它并不意味着该类中定义的所有@Beans本身都是@RefreshScope。 Specifically, anything that depends on those beans cannot rely on them being updated when a refresh is initiated, unless it is itself in @RefreshScope (in which it will be rebuilt on a refresh and its dependencies re-injected, at which point they will be re-initialized from the refreshed @Configuration).
具体来说,依赖于那些bean的任何东西都不能依赖于它们在启动刷新时被更新,除非它本身位于@RefreshScope中(在刷新时会重建它并重新注入其依赖项,此时它们将被重新注入)从刷新的@Configuration重新初始化。
Anyone else facing this issue, please make sure the followings: 遇到此问题的任何其他人,请确保以下内容:
@RefreshScope
@RefreshScope
注释 Spring boot actuator is added into your dependency, as it is the module which actually provides these endpoints: Spring启动器执行器被添加到您的依赖项中,因为它是实际提供这些端点的模块:
org.springframework.boot spring-boot-starter-actuator
org.springframework.boot spring-boot-starter-actuator
Refresh endpoint has been updated to: 刷新端点已更新为:
http://{ip_address}:{port}/actuator/refresh HTTP:// {IP_ADDRESS}:{端口} /致动器/刷新
Refresh endpoint isn't enabled by default. 默认情况下不启用刷新端点。 You have to enable it explicitly in the bootstrap.properties file by adding the following line:
您必须通过添加以下行在bootstrap.properties文件中显式启用它:
management.endpoints.web.exposure.include=*
I have enabled all the endpoints, while you can just enable the specific endpoints as well. 我已启用所有端点,同时您也可以启用特定端点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.