Spring Cloud Config-自定义Bootstrap上下文以从另一个属性Source读取配置URI

时间:2018-04-04 03:11:57

标签: java spring-boot spring-cloud spring-cloud-netflix spring-cloud-config

所以我有以下用例 -

我需要自定义引导上下文以从特定文件中读取配置服务器所在的uri,然后将该URI注入属性spring.cloud.config.uri以从configServer获取属性。

我一直在查看有关Spring云公共文档的文档,并且已经尝试过自定义Bootstrap属性源。遗憾的是,这不起作用,因为它充当应用程序上下文的另一个属性源,而不是引导上下文(bootstrap.yml或属性)的属性源。

我到目前为止唯一的解决方案可能不是最优雅的解决方案是在运行spring应用程序之前将uri属性设置为系统属性。

所以,如下所示。

            public static void main(String args[]) {

            * Read properties from file and set as system properties*
            System.setProperty("uri","http://localhost:8097");
            SpringApplication.run(Application.class,args);

          }

然后将bootstrap.properties中的上述属性引用为 -

          spring.cloud.config.uri=${uri}

这是有效的,但我还是希望以更加春天友好的方式做到这一点。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

不幸的是,这件事无法完成。

您可以使用config

更改bootstrap.yml / properties的位置
                 spring.cloud.bootstrap.location

但是,你不能添加一个bean来读取将成为Bootstrap上下文一部分的属性,因为Bootstrap Context将是用户自己创建的最高级祖先的父级。

答案 1 :(得分:-1)

我认为你可以使用run arg参数并给出这样的defult值:

spring.cloud.config.uri=${config.uri:http://localhost:8097}

java jar xxx.jar --config.uri=http://localhost:8099