Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cloud config server. Environment variables in properties

I configured Spring Cloud Config server like this:

@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServer {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServer.class, args);
    }
}

I'm using 'native' profile so properties are picked up from the file system:

server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.search-locations: classpath:/global

Now the tricky part is that some properties contain environmental variables. Properties in 'global/application-production.properties' are configured like this:

test=${DOCKER_HOST}

When I start up Config Server - everything works fine. However when I access http://localhost:8888/testapp/production I see this:

{
    name: "testapp",
    profiles: [
        "production"
],
    label: null,
    version: null,
    propertySources: [
        {
            name: "classpath:/global/application-production.properties",
            source: {
                test: "${DOCKER_HOST}"
            }
        }
    ]
}

So value from ENV variable is not replacing ${DOCKER_HOST} put rather returned as is.

But if I access http://localhost:8888/application-production.properties then result is non JSON but rather plain text:

test: tcp://192.168.99.100:2376

Spring documentation says:

The YAML and properties representations have an additional flag (provided as a boolean query parameter resolvePlaceholders) to signal that placeholders in the source documents, in the standard Spring ${…​} form, should be resolved in the output where possible before rendering. This is a useful feature for consumers that don’t know about the Spring placeholder conventions.

For some reason resolvePlaceholders is not working for JSON representation thus server config clients need to be aware of all ENV variables configured on server.

Is it possible to force JSON representation resolvePlaceholders same way as plain text (properties) representation?

like image 829
ykravchenko Avatar asked Aug 03 '16 18:08

ykravchenko


People also ask

How do I set environment specific properties in Spring boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.

What is Spring Cloud config?

From the project documentation: Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments.

How do I get configuration properties from a config server?

A Spring Boot application can fetch configuration properties from a config server thanks to Spring Cloud Config Client. The configuration returned is filtered according to the application name, the active Spring profiles, and, if present, a label identified by the specific Environment Repository.

How to configure Spring Boot config server?

Spring Cloud Config Server 1 3.1 Set up project and dependencies. Our Config Server, on top of Spring Boot, will make use of one central library: spring-cloud-config-server. 2 3.2 Enable config server. The next step is enabling the config server. ... 3 3.3 Define the Environment Repository. ... 4 3.4 Test config server. ...

How does Spring Cloud config server clone the remote repository?

As Spring Cloud Config Server has a clone of the remote git repository after check-outing branch to local repo (e.g fetching properties by label) it will keep this branch forever or till the next server restart (which creates new local repo).


2 Answers

I faced the same issue. After looking into Spring Cloud Config Repository I have found the following commit: Omit system properties and env vars from placeholders in config

It looks like such behavior is not supported.

like image 194
Anastasiia Smirnova Avatar answered Oct 08 '22 13:10

Anastasiia Smirnova


You can try the Property Overrides feature to override properties from git Environment Repository.

To override property foo at runtime, just set a system property or an environment variable spring.cloud.config.server.overrides.foo before starting the config server.

like image 36
gongshw Avatar answered Oct 08 '22 13:10

gongshw