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

标签 java spring spring-boot spring-cloud

我引导我的 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 配置,并根据需要通过执行 setConfigUrirefreshObjects 添加它,这似乎是从配置服务器获取配置。 (不要忘记将 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/

相关文章:

java - 使用 Javassist 修改后检测加载的类

java - 方法不明确 - Java

java - Jaxb2Marshaller 无法以线程安全的方式解码非 XmlRoot 元素

java - 如何将外部属性文件位置添加到部署到 tomcat 6 的 Spring Boot 应用程序?

java - JDBC Template异常表或 View 不存在但实际存在

Java,在其他地方使用数组中的字符串?

java - 在 JLabel 上设置大小会取代 java 中的其他组件

spring - 如何在 Spring 中保护对静态资源的访问

java - gradle依赖文件构建gradle

java - 测试 Spring Boot 应用程序?