Spring Cloud Config Server环境变量优先级

时间:2023-01-15 08:03:09

I have a question regarding the priority of environment variables when working with spring cloud config server

在使用spring cloud配置服务器时,我对环境变量的优先级有疑问

In my service I have a local properties file application.yml with this content

在我的服务中,我有一个带有此内容的本地属性文件application.yml

foo:
  bar: "some"
  buz: "some"
  joe: "some"

The service is also connected to a config server with a configuration repository that contains a file testservice-api.yml (where testservice-api is the spring application name of the service). The contents of this file is:

该服务还连接到配置服务器,配置存储库包含文件testservice-api.yml(其中testservice-api是服务的spring应用程序名称)。该文件的内容是:

foo:
  bar: "some-specific"

So with this setup the configuration at runtime would result in this:

因此,使用此设置,运行时的配置将导致:

{
    "foo.bar": "some-specific",
    "foo.buz": "some",
    "foo.joe": "some"
}

Now I try to override foo.bar and foo.joe with an environment variable.

现在我尝试用环境变量覆盖foo.bar和foo.joe。

So I start the service with this command:

所以我用这个命令启动服务:

FOO_BAR=some-env FOO_JOE=some-env gradle bootRun

FOO_BAR = some-env FOO_JOE = some-env gradle bootRun

From what I read in this part of the spring boot documentation the environment variables should have priority over the configuration files - also the spring cloud config documentation does not state sth different - so I would expect the result to be:

从我在spring boot文档的这一部分中读到的内容,环境变量应优先于配置文件 - spring cloud配置文档也没有说明不同 - 所以我希望结果如下:

{
    "foo.bar": "some-env",
    "foo.buz": "some",
    "foo.joe": "some-env"
}

But instead I get:

但相反,我得到:

{
    "foo.bar": "some-specific",
    "foo.buz": "some",
    "foo.joe": "some-env"
}

So only the configuration from the local configuration file inside the jar is overridden by the environment variable - the property from the config repo seems to have priority over the environment variable.

因此,只有来自jar内部的本地配置文件的配置被环境变量覆盖 - 来自config repo的属性似乎优先于环境变量。

Is this explainable - Or is this a bug? Any hints in this one?

这可以解释 - 或者这是一个错误?这个提示有什么提示吗?

Please find the example code here:

请在此处找到示例代码:

https://github.com/mduesterhoeft/configserver-test

The README in the repository lists the issue described here as Use Case 3

存储库中的README将此处描述的问题列为用例3

1 个解决方案

#1


7  

define following properties in git repo (as a source for config-server) [for given profile]: spring.cloud.config: overrideSystemProperties: false overrideNone: true

在git repo中定义以下属性(作为config-server的源代码)[对于给定的配置文件]:spring.cloud.config:overrideSystemProperties:false overrideNone:true

keep in mind properties (especially overrideSystemProperties & overrideNone) in bootsrap.yml are overriden by those from config-server by default

请记住,bootsrap.yml中的属性(特别是overrideSystemProperties和overrideNone)默认被config-server覆盖。

#1


7  

define following properties in git repo (as a source for config-server) [for given profile]: spring.cloud.config: overrideSystemProperties: false overrideNone: true

在git repo中定义以下属性(作为config-server的源代码)[对于给定的配置文件]:spring.cloud.config:overrideSystemProperties:false overrideNone:true

keep in mind properties (especially overrideSystemProperties & overrideNone) in bootsrap.yml are overriden by those from config-server by default

请记住,bootsrap.yml中的属性(特别是overrideSystemProperties和overrideNone)默认被config-server覆盖。