加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:33:08 所属栏目:大数据 来源:网络整理
导读:我像这样配置了Spring Cloud Config服务器: @SpringBootApplication@EnableAutoConfiguration@EnableConfigServerpublic class ConfigServer { public static void main(String[] args) { SpringApplication.run(ConfigServer.class,args); }} 我正在使用“

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读