微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐