启动后更改 Spring Cloud Config 服务器 uri

Change the Spring Cloud Config server uri after startup

我 bootstrap 我的 springboot 应用程序指向默认配置服务器以在启动时获取配置道具,配置服务器将 spring.cloud.config.uri (不要问我为什么 :D)更改为指向不同的配置服务器...当我调用 /actuator/refresh 端点时,我希望能够将配置服务器 uri 切换到我刚收到的新配置服务器。

我查看了 spring-cloud-config 来源,但那里的 bean 似乎没有用 @RefreshScope 注释。

springboot env /actuator/env 似乎显示收到的新 uri,但配置客户端 bean 似乎仍指向 bootstrap uri。

关于如何实现这一点有什么建议吗?我对使用 springboot 还很陌生。

谢谢!

对于其他尝试执行相同操作的人,我从初始启动中删除了 spring 云配置,并根据需要通过执行 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();
}