我引导我的 springboot 应用程序指向默认配置服务器以在启动时获取配置属性,配置服务器更改 spring.cloud.config.uri
(不要问我为什么:D)以指向不同的配置服务器...当我调用 /actuator/refresh
端点时,我希望能够将配置服务器 uri 切换到我刚刚收到的新配置服务器 uri。
我查看了spring-cloud-config源,但那里的 bean 似乎没有用 @RefreshScope
注释。
springboot env /actuator/env
似乎显示收到的新 uri,但配置客户端 bean 似乎仍然指向引导 uri。
关于如何实现这一目标有什么建议吗?我对使用 springboot 还很陌生。
谢谢!
最佳答案
对于尝试执行相同操作的其他人,我从初始启动中删除了 Spring Cloud 配置,并根据需要通过执行 setConfigUri
和 refreshObjects
添加它,这似乎是从配置服务器获取配置。 (不要忘记将 spring-cloud-config 和依赖项添加到 pom.xml)
这就是我所做的,到目前为止它似乎达到了目的。如果出现问题,请随时指出。
@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();
}
关于java - 启动后更改Spring Cloud Config服务器uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51713309/