9

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?

3 Answers 3

2

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.

0
2

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.

0
0

There was an update in order to accomplish this, in the following merge. 1 I found an implementation for resolvePlaceholders. Which gave me the idea of just creating a new controller which uses the EnvironmentController. This will allow you to resolve configuration, this is a good bootstrap.

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.config.server.environment.EnvironmentController;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(method = RequestMethod.GET, path = "resolved/${spring.cloud.config.server.prefix:}")
public class ReplacedEnvironmentController {

  private EnvironmentController  environmentController;

  @Autowired
  public ReplacedEnvironmentController(EnvironmentRepository repository) {
    environmentController = new EnvironmentController(repository, new ObjectMapper());
  }

  public ReplacedEnvironmentController(EnvironmentRepository repository,
      ObjectMapper objectMapper) {
    environmentController = new EnvironmentController(repository, objectMapper);

  }

  @RequestMapping("/{name}/{profiles:.*[^-].*}")
  public ResponseEntity<String> resolvedDefaultLabel(@PathVariable String name,
      @PathVariable String profiles) throws Exception  {
    return resolvedLabelled(name, profiles, null);
  }

  @RequestMapping("/{name}/{profiles}/{label:.*}")
  public ResponseEntity<String> resolvedLabelled(@PathVariable String name, @PathVariable String profiles,
      @PathVariable String label) throws Exception  {
    return environmentController.labelledJsonProperties(name, profiles, label, true);
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.