Cob*_*117 7 java spring spring-boot spring-cloud
我的理解是,当您使用Spring Cloud的RefreshScope注释时,会注入数据的代理,如果更改了支持信息,代理会自动更新.不幸的是,我需要找到一种方法,以便在刷新时发出警报,以便我的代码可以重新读取刷新范围的bean中的数据.
简单示例:计划任务,其计划存储在Cloud Config中.除非你等到下一次执行任务(可能需要一段时间)或定期轮询配置(这看起来很浪费),否则无法知道配置是否已更改.
Ali*_*ani 12
当EnvironmentChangeEvent您的配置客户端中出现刷新时,如文档所述:
应用程序将以
EnvironmentChangedEvent几种标准方式监听并对变化做出反应(用户ApplicationListener可以@Bean通过正常方式添加附加 s ).
因此,您可以为此事件定义事件侦听器:
public class YourEventListener implements ApplicationListener<EnvironmentChangeEvent> {
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
EnvironmentChangeEvent发生更改时会被触发Environment。就Spring Cloud Config而言,这意味着它在/env调用执行器端点时被触发。
RefreshScopeRefreshedEvent当@RefreshScope启动刷新豆时(例如,/refresh调用执行器端点)触发。
这意味着您需要这样注册ApplicationListener<RefreshScopeRefreshedEvent>:
@Configuration
public class AppConfig {
@EventListener(RefreshScopeRefreshedEvent.class)
public void onRefresh(RefreshScopeRefreshedEvent event) {
// Your code goes here...
}
}
Run Code Online (Sandbox Code Playgroud)