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