Spring cloud config server. Environment variables in properties

谁说胖子不能爱 提交于 2020-01-12 07:40:08

问题


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?


回答1:


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.




回答2:


There was an update In order to accomplish this, in the following merged enter link description here I found an implementation for resolvePlaceholders. Which gave me the idea f 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);
  }

}



回答3:


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.



来源:https://stackoverflow.com/questions/38751311/spring-cloud-config-server-environment-variables-in-properties

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