监听Spring cloud consul配置中刷新的上下文

问题描述 投票:0回答:2

Spring Cloud consul 配置允许在 consul 中更改属性时动态刷新属性。有没有办法在变化发生时倾听?

@Component
public class ContextRefreshListener {

@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
    System.out.println("refreshed");
}

@EventListener
public void handleContextStart(ContextStartedEvent event) {
    System.out.println("started");
}


@EventListener
public void handleContextRefresh(ApplicationContextEvent event) {
    System.out.println("context");
}

}

我尝试了以上三个事件,但没有运气。有什么办法可以在刷新发生时监听事件吗?

spring spring-cloud spring-cloud-config spring-cloud-consul
2个回答
1
投票

我可以通过以下方式做到这一点

@EventListener
public void handleContextStart(EnvironmentChangeEvent event) {
    System.out.println("changed");
    //Use this for getting the version from consul

}

0
投票

RefreshScopeRefreshedEvent 更好。

© www.soinside.com 2019 - 2024. All rights reserved.