java - 启动后更改 Spring Cloud Config 服务器 uri
问题描述
我引导我的springboot应用程序指向默认配置服务器以在启动时获取配置道具,配置服务器将spring.cloud.config.uri
(不要问我为什么:D)更改为指向不同的配置服务器......当我调用/actuator/refresh
端点我希望能够将配置服务器 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();
}
推荐阅读
- c# - 角色控制器卡住
- python - 如何使用json附加到文件中的列表?
- java - 日期到字符串解析
- react-native - React Native 升级版本从 0.61.5+ TypeError 到 0.63:
- java - Spring Boot Oauth2 Client(Reactive) Mutual TLS/SSL token uri
- php - Dompdf 版本 0.8.5 阿拉伯语 RTL 未正确显示
- assembly - 此代码没有错误,但我仍然没有得到正确的答案
- markdown - 使用 markdown 突出显示代码会生成内联代码块
- go - How to save and retrieve Gorm's many2many relationship using Go
- mvvm - SwiftUI - @Binding 到访问 ObservableObject 属性内的值的计算属性会复制变量吗?