在运行时具有动态引导URI的Spring Cloud Config客户端?

给出配置客户端bootstrap.yml:

spring:
  ...
  cloud:
    config:
      uri: ${SPRING_CONFIG_URI:http://localhost:9000}
      fail-fast: true

需要在运行时设置环境变量SPRING_CONFIG_URI。我们可以再次讨论该代码的气味,但是对于本练习,只有在服务启动时才知道此URI(与获取随机SRV记录有关)。

是否可以通过Spring方法设置/覆盖某些bootstrap.yaml参数?

这是我尝试过的:

@SpringBootApplication
public class Application {
    public static void main( String[] args ) throws Exception
    {
        String configServerPort = DnsHelper.resolveSrv("configserver.local");

        // This doesn't work
        System.setProperty("SPRING_CONFIG_URI",configServerPort);

        // This doesn't work
        // Use reflection to change the in-memory environment variables
        // @see https://stackoverflow.com/a/7201825/1938889
        setEnv( Map.of( "SPRING_CONFIG_URI",configServerPort ) );

        // After the environment variable is set,then start the service
        Application.run( Application.class,args );
    }
    ...
nmhanq 回答:在运行时具有动态引导URI的Spring Cloud Config客户端?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3150744.html

大家都在问