Spring Cloud配置服务器.属性中的环境变量 [英] Spring cloud config server. Environment variables in properties

查看:665
本文介绍了Spring Cloud配置服务器.属性中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样配置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},而是照原样返回.

但是,如果我访问 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 Cloud Config Repository之后,我发现了以下提交: 忽略配置中的占位符的系统属性和环境变量

似乎不支持这种行为.

I configured Spring Cloud Config server like this:

@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServer {

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

I'm using 'native' profile so properties are picked up from the file system:

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

Now the tricky part is that some properties contain environmental variables. Properties in 'global/application-production.properties' are configured like this:

test=${DOCKER_HOST}

When I start up Config Server - everything works fine. However when I access http://localhost:8888/testapp/production I see this:

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

So value from ENV variable is not replacing ${DOCKER_HOST} put rather returned as is.

But if I access http://localhost:8888/application-production.properties then result is non JSON but rather plain text:

test: tcp://192.168.99.100:2376

Spring documentation says:

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.

For some reason resolvePlaceholders is not working for JSON representation thus server config clients need to be aware of all ENV variables configured on server.

Is it possible to force JSON representation resolvePlaceholders same way as plain text (properties) representation?

解决方案

I faced the same issue. After looking into Spring Cloud Config Repository I have found the following commit: Omit system properties and env vars from placeholders in config

It looks like such behavior is not supported.

这篇关于Spring Cloud配置服务器.属性中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆