Search code examples
springspring-bootspring-cloudspring-cloud-config

Placeholders resolution for client specific configuration


I am learning about Spring Cloud Config project capabilities. I wondered whether it is possible to use a placeholders for application specific configuration?

For example:

My application.yml file:

server:
  port: 8888

spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config

The config folder contains a my-app.yml file:

key:
  value: ${my.password}

The server started with -Dmy.password=password environment variable. A get request to /my-app/native url returns:

{
    "name": "my-app",
    "profiles": ["native"],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [{
        "name": "classpath:/config/my-app.yml",
        "source": {
            "key.value": "${my.password}"
        }
    }]
}

The env property in placeholder returned to the client without evaluation, there is a way to evaluate the property before sending the response to the client?


Solution

  • You can use the override feature to set key.value's value when starting your server:

    -Dspring.cloud.config.server.overrides.key.value=overrideValue
    

    I think your clients must be started afterwards.

    Spring documentation:

    Property Overrides

    The Config Server has an “overrides” feature that lets the operator provide configuration properties to all applications. The overridden properties cannot be accidentally changed by the application with the normal Spring Boot hooks. To declare overrides, add a map of name-value pairs to spring.cloud.config.server.overrides, as shown in the following example:

    spring:   
      cloud:
        config:
          server:
            overrides:
              foo: bar 
    

    The preceding examples causes all applications that are config clients to read foo=bar, independent of their own configuration.

    ...