简体   繁体   English

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

[英]Spring Cloud Config- Customizing the Bootstrap context to read config URI from another property Source

So I have the following use case- 所以我有以下用例-

I need to customize the bootstrap context to read the uri where the config Server lies from a particular file and then inject that uri into the property spring.cloud.config.uri to get properties from the configServer. 我需要自定义引导上下文,以从特定文件中读取配置服务器所在的uri,然后将该uri注入属性spring.cloud.config.uri中,以从configServer获取属性。

I have been looking at documentation on Spring cloud commons and have experimented around customizing Bootstrap property sources. 我一直在查看有关Spring Cloud Commons的文档,并尝试过自定义Bootstrap属性源。 This, unfortunately does not work, because this acts as another source of properties for the application context, not a source of properties for the bootstrap context( bootstrap.yml or properties). 不幸的是,这是行不通的,因为这是应用程序上下文的另一个属性源,而不是引导程序上下文的属性源(bootstrap.yml或properties)。

The only solution I have so far, which is probably not the most elegant one is to set the uri property as a system property before we run the spring Application. 到目前为止,我唯一的解决方案(可能不是最优雅的解决方案)是在运行spring Application之前将uri属性设置为系统属性。

So, something like the following. 因此,如下所示。

            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);

          }

And then reference the above property in bootstrap.properties as- 然后在bootstrap.properties中将上述属性引用为-

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

This works and is okay,but i would like to do this still in a more Spring Friendly way. 这可以正常工作,但是我还是想以一种更友好的方式来做到这一点。

Thank you in advance. 先感谢您。

Unfortunately this cant be done. 不幸的是,这不能完成。

You can change the location of bootstrap.yml/properties using config 您可以使用config更改bootstrap.yml / properties的位置

                 spring.cloud.bootstrap.location

But , you cannot add a bean to read properties which will be part of the Bootstrap context, as the Bootstrap Context will be the parent of the most senior ancestor created by the user himself. 但是,您不能添加bean来读取将成为Bootstrap上下文一部分的属性,因为Bootstrap上下文将是用户本人创建的最高级祖先的父级。

I think you can use run arg arguments and give a defult value like this: 我认为您可以使用run arg参数并提供如下的defult值:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM