如何触发刷新事件?

yog*_*ger 6 spring-boot spring-cloud-config

来自https://spring.io/guides/gs/centralized-configuration/

您可以通过使用 Spring Cloud Config @RefreshScope 注解 MessageRestController 然后触发刷新事件来强制 Bean 刷新其配置(即从配置服务器中提取更新的值)。

我们如何触发这个刷新事件(对于用@ConfigurationProperties和注释的类@RefreshScope)?我们不想使用弹簧执行器/refresh端点。应用程序从配置服务器获取配置需要什么?

小智 9

RefreshEvent您可以使用自动装配来触发ApplicationEventPublisher. Spring Cloud 有一个该事件的监听器RefreshEventListener

@Autowired
private ApplicationEventPublisher eventPublisher;

public void fireRefreshEvent() {
  eventPublisher.publishEvent(new RefreshEvent(this, "RefreshEvent", "Refreshing scope");
}
Run Code Online (Sandbox Code Playgroud)

参考:

第一个参考

第二参考


Gna*_*yam 0

要刷新所有微服务的配置属性,您可以通过消息代理(Spring Cloud Bus)将更改发布到所有微服务。

参考:

https://springbootdev.com/2018/07/17/spring-cloud-config-refreshing-the-config-changes-with-spring-cloud-bus-part-2/