Spring云配置服务器.属性中的环境变量

我像这样配置了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?

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

看起来这种行为不受支持.

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...