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

编程入门 行业动态 更新时间:2024-10-18 21:24:34
本文介绍了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时-一切正常.但是,当我访问 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},而是照原样返回.

但是,如果我访问 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 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 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配置服务器.属性中的环境变量

本文发布于:2023-11-01 01:44:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境变量   属性   服务器   Spring   Cloud

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!