【问题标题】:how to change config server uri dynamically using OS environment如何使用操作系统环境动态更改配置服务器 uri
【发布时间】:2016-10-28 20:57:58
【问题描述】:
我有一个spring config server 和spring config client。在客户端中,我已将spring.cloud.config.uri 设置为http://localhost:8888,但我想在Windows 中使用setx 的操作系统环境将其更改为其他一些uri,例如http://example.com:8888。所以我跑了
setx spring.cloud.config.uri "http://example.com:8888"
但是当我运行我的 spring config client 时,它仍在尝试从 localhost 读取。根据this 链接我在bootstrap.yml 中的spring.cloud.config.uri 应该被我用OS environment 设置的内容覆盖,但它没有。请让我知道我在这里做错了什么。
【问题讨论】:
标签:
spring-boot
environment-variables
spring-cloud-config
setx
【解决方案1】:
setx
setx 添加变量但不使其在当前 shell (as explained here) 中可用:
setx 永久修改该值,这会影响所有未来的 shell,
但不会修改已经运行的 shell 的环境。你
必须退出外壳并重新打开它才能进行更改
可用,但该值将保持修改,直到您更改它
再次。
只要确保你是从一个新打开的 shell 窗口运行的。
我建议您使用set spring.cloud.config.uri=http://example.com:8888 只是为了测试。
检查环境变量是否存在
您可以在main 方法的第一行添加以下内容:
System.out.println(System.getenv("spring.cloud.config.uri"));
System.out.println(System.getenv("SPRING_CLOUD_CONFIG_URI"));
带下划线的变量名
Windows 支持带点的环境变量,所以对你来说应该没问题。几乎所有其他操作系统都不是这种情况。如您所知,spring 支持带有 下划线 的变量名(如您提供的链接中所述):
如果您使用环境变量而不是系统属性,大多数
操作系统不允许以句点分隔的键名,但您可以使用
下划线代替(例如 SPRING_CONFIG_NAME 而不是
spring.config.name)。