Spring cloud config server. Environment variables in properties

我的梦境 提交于 2019-12-03 16:13:31

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.

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);
  }

}

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.

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