8

我这样配置 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以与纯文本(属性)表示相同的方式?

4

3 回答 3

2

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

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

于 2018-07-23T13:18:40.557 回答
2

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

看起来不支持这种行为。

于 2017-02-16T19:28:15.590 回答
0

有一个更新为了做到这一点,在下面的合并输入链接描述中,我找到了 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);
  }

}
于 2017-09-13T21:11:34.130 回答