【问题标题】:Spring cloud config server. Environment variables in properties春天云配置服务器。属性中的环境变量
【发布时间】:2016-12-09 15:08:14
【问题描述】:

我这样配置 Spring Cloud Config 服务器:

@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServer {

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

我正在使用“本机”配置文件,因此从文件系统中获取属性:

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

现在棘手的部分是某些属性包含环境变量。 'global/application-production.properties' 中的属性配置如下:

test=${DOCKER_HOST}

当我启动配置服务器时 - 一切正常。但是,当我访问 http://localhost:8888/testapp/production 时,我看到了这个:

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

因此来自 ENV 变量的值不会替换 ${DOCKER_HOST} 而是按原样返回。

但如果我访问 http://localhost:8888/application-production.properties 则结果不是 JSON,而是纯文本:

test: tcp://192.168.99.100:2376

Spring 文档说:

YAML 和属性表示有一个附加标志(以布尔查询参数 resolvePlaceholders 的形式提供)来指示源文档中的占位符(采用标准 Spring ${...​} 形式)应在输出之前尽可能在输出中解析渲染。对于不了解 Spring 占位符约定的消费者来说,这是一个有用的功能。

由于某种原因,resolvePlaceholders 不适用于 JSON 表示,因此服务器配置客户端需要了解服务器上配置的所有 ENV 变量。

是否可以强制 JSON 表示 resolvePlaceholders 以与纯文本(属性)表示相同的方式?

【问题讨论】:

    标签: spring spring-cloud-config


    【解决方案1】:

    我遇到了同样的问题。在查看 Spring Cloud Config Repository 后,我发现了以下提交: Omit system properties and env vars from placeholders in config

    似乎不支持这种行为。

    【讨论】:

      【解决方案2】:

      您可以尝试Property Overrides 功能来覆盖来自 git Environment Repository 的属性。

      要在运行时覆盖属性foo,只需在启动配置服务器之前设置系统属性或环境变量spring.cloud.config.server.overrides.foo

      【讨论】:

        【解决方案3】:

        有一个更新为了实现这一点,在以下合并的enter link description here 中,我找到了resolvePlaceholders 的实现。这让我想到了创建一个使用 EnvironmentController 的新控制器。这将允许您解析配置,这是一个很好的引导程序。

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

        【讨论】:

          猜你喜欢
          • 2018-11-29
          • 1970-01-01
          • 2016-04-08
          • 2015-02-18
          • 1970-01-01
          • 2018-04-14
          • 2017-10-29
          • 2011-06-21
          • 1970-01-01
          相关资源
          最近更新 更多