我像这样配置了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}
当我启动Config Server时 – 一切正常.但是当我访问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} put而是按原样返回.
但是,如果我访问http://localhost:8888/application-production.properties,那么结果是非JSON而是纯文本:
test: tcp://192.168.99.100:2376
Spring文档说:
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.
由于某些原因,resolvePlaceholders不适用于JSON表示,因此服务器配置客户端需要知道在服务器上配置的所有ENV变量.
是否可以像纯文本(属性)表示一样强制JSON表示resolvePlaceholders?
Omit system properties and env vars from placeholders in config
看起来这种行为不受支持.