我引导我的springboot应用程序指向默认的配置服务器,在启动时获取配置支持,配置服务器更改spring.cloud.config.uri (不要问我为什么:D)指向另一个配置服务器.当我调用/actuator/refresh端点时,我希望能够将配置服务器uri切换到我刚刚收到的新端点。
我查看了弹簧云配置源代码,但是那里的bean似乎没有使用@RefreshScope进行注释。
springboot env /actuator/env似乎显示了接收到的新uri,但是配置客户端bean似乎仍然指向引导uri。
对于如何做到这一点,有什么建议吗?我对弹力靴的使用相当陌生。
谢谢!
发布于 2018-08-13 19:09:33
对于其他尝试这样做的人,我从初始启动中删除了spring,并根据需要添加了spring,然后执行setConfigUri,然后执行refreshObjects,这似乎是从配置服务器获取配置。(不要忘记向pom.xml添加spring和依赖项)
这是我所做的,而且似乎达到了目前为止的目的。如果有什么不对劲的地方,请随意插话。
@Autowired
private ConfigurableEnvironment env;
@Autowired
private ConfigurableApplicationContext applicationContext;
@Autowired
private ContextRefresher refresher;
public void setConfigUri(String uri) throws Exception {
MutablePropertySources propertySources = env.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put("spring.cloud.config.uri", uri);
propertySources.addFirst(new MapPropertySource("defaultProperties", map));
applicationContext.setEnvironment(env);
}
public void refreshObjects() throws Exception {
refresher.refresh();
}https://stackoverflow.com/questions/51713309
复制相似问题