@RefreshScope 不工作 - Spring 启动
@RefreshScope not working - Spring Boot
我遵循这里描述的方法:https://github.com/jeroenbellen/blog-manage-and-reload-spring-properties,唯一的区别是,在我的例子中,属性在多个 classes 中使用,所以我将它们全部放在一个实用程序中class CloudConfig
并且我使用 getter 引用它的变量。这是 class 的样子:
@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;
}
}
并且我在其他 class 中使用变量 count
,例如 CloudConfig.getCount()
。我能够在启动时很好地加载属性,但我无法即时动态更新它们。谁能告诉我我做错了什么?如果我没有进行此配置 class,而是完全按照教程中的描述进行操作,一切正常,但我无法将其适应我的用例。谁能告诉我我错过了什么?
请尝试使用 @ConfigurationProperties。
例如
@ConfigurationProperties(prefix="config")
public class CloudConfig {
private Integer count;
public Integer count() {
return this.count;
}
public void setCount(Integer count) {
this.count = count;
}
}
reference doc from spring cloud 状态:
@RefreshScope works (technically) on an @Configuration class, but it
might lead to surprising behaviour: e.g. it does not mean that all the
@Beans defined in that class are themselves @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).
遇到此问题的任何其他人,请确保以下内容:
- 您的控制器带有
@RefreshScope
注释
Spring 引导执行器已添加到您的依赖项中,因为它是实际提供这些端点的模块:
org.springframework.boot
spring-boot-starter-actuator
刷新端点已更新为:
http://{ip_address}:{端口}/actuator/refresh
默认情况下不启用刷新端点。您必须通过添加以下行在 bootstrap.properties 文件中明确启用它:
management.endpoints.web.exposure.include=*
我已经启用了所有端点,而您也可以只启用特定端点。
我遵循这里描述的方法:https://github.com/jeroenbellen/blog-manage-and-reload-spring-properties,唯一的区别是,在我的例子中,属性在多个 classes 中使用,所以我将它们全部放在一个实用程序中class CloudConfig
并且我使用 getter 引用它的变量。这是 class 的样子:
@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;
}
}
并且我在其他 class 中使用变量 count
,例如 CloudConfig.getCount()
。我能够在启动时很好地加载属性,但我无法即时动态更新它们。谁能告诉我我做错了什么?如果我没有进行此配置 class,而是完全按照教程中的描述进行操作,一切正常,但我无法将其适应我的用例。谁能告诉我我错过了什么?
请尝试使用 @ConfigurationProperties。 例如
@ConfigurationProperties(prefix="config")
public class CloudConfig {
private Integer count;
public Integer count() {
return this.count;
}
public void setCount(Integer count) {
this.count = count;
}
}
reference doc from spring cloud 状态:
@RefreshScope works (technically) on an @Configuration class, but it might lead to surprising behaviour: e.g. it does not mean that all the @Beans defined in that class are themselves @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).
遇到此问题的任何其他人,请确保以下内容:
- 您的控制器带有
@RefreshScope
注释
Spring 引导执行器已添加到您的依赖项中,因为它是实际提供这些端点的模块:
org.springframework.boot spring-boot-starter-actuator
刷新端点已更新为:
http://{ip_address}:{端口}/actuator/refresh
默认情况下不启用刷新端点。您必须通过添加以下行在 bootstrap.properties 文件中明确启用它:
management.endpoints.web.exposure.include=*
我已经启用了所有端点,而您也可以只启用特定端点。