How to set local config properties with Spring Config Service

点点圈 提交于 2019-12-02 09:39:37

问题


So I recently learned this awesome config service of Spring Cloud, and after some struggling I'm able to get our distributed application set up, with multiple nodes reading config properties from one config server.

However one problem I don't know how to resolve is even though most props are same across multiple nodes, some props need local version and I don't know how to set local prop together with config service.

For example this is my bootstrap.properties,

spring.cloud.config.uri=${config.server:http://localhost:8888}
spring.application.name=worker

If I add additional props under bootstrap.properties or in a separate application.properties file, I can see them under actuator's env endpoint, like this,

configService:file:///home/me/work/config-test/worker.properties: 
{
    server.timeout: "100"
},

applicationConfig: [classpath:/application.properties]: {
    server.timeout: "50"
},

In my app the server.timeout will always be 100 now, I don't know how can I reference a local value when I use a centralized config service like this?


回答1:


OK, after some more research I found the answer. From https://docs.pivotal.io/spring-cloud-services/config-server/configuration-clients.html

A Spring application can use a Config Server as a property source. Properties from a Config Server will override those defined locally (e.g. via an application.yml in the classpath).

So the right thing to do is to leave any client side property out of the config service. This to me also means for such properties there is no way to have a default that will only be overwritten if a client side value presents, which would be good to have.




回答2:


From the Overriding the Values of Remote Properties section in the official documentation:

The property sources that are added to you application by the bootstrap context are often "remote" (e.g. from a Config Server), and by default they cannot be overridden locally, except on the command line. If you want to allow your applications to override the remote properties with their own System properties or config files, the remote property source has to grant it permission by setting spring.cloud.config.allowOverride=true (it doesn’t work to set this locally).

Once that flag is set there are some finer grained settings to control the location of the remote properties in relation to System properties and the application’s local configuration: spring.cloud.config.overrideNone=true to override with any local property source, and spring.cloud.config.overrideSystemProperties=false if only System properties and env vars should override the remote settings, but not the local config files.

So, you can set the following configurations in the remote application.yml (e.g. remote git repository) to allow local override of remote properties.

spring:
  cloud:
    config:
      allowOverride: true
      overrideNone: true
      overrideSystemProperties: false


来源:https://stackoverflow.com/questions/39261372/how-to-set-local-config-properties-with-spring-config-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!