Spring cloud config - 加载附加文件
Posted
技术标签:
【中文标题】Spring cloud config - 加载附加文件【英文标题】:Spring cloud config - loading additional files 【发布时间】:2017-06-08 00:14:07 【问题描述】:我的应用程序有一个 spring 云服务器,在启动时提供公共属性文件。
“正常”功能运行良好。应用程序获取文件的层次结构并获得对加载参数的完全访问权限。
Application.properties
Application-<profile>.properties
<applicationname>.properties
...
现在的问题是: 除了通用命名,如何加载配置文件?
如果我在存储库中存储另一个文件,例如sharedCommonServers.properties,我如何使用配置服务器加载这个文件? 该文件包含一些但不是所有程序使用的属性。
提前谢谢你!
亨德里克
【问题讨论】:
@PropertySource("classpath:sharedCommonServers.properties")
,只需将其添加到您的应用程序类中。
sharedCommonServers.properties 应该使用云配置服务器加载。我将您的行添加到配置中,但似乎没有加载它。对不对,这条线只会从本地存储加载文件,还是也应该使用配置服务器?
见this answer,应该会有帮助。
@Kane 是的,这就是我目前正在调查的内容。不是真正的开箱即用的解决方案,但也许是一个很好的尝试。
【参考方案1】:
好吧,至少我找到了解决办法:
我采用了云配置的正常应用程序上下文。 在这种情况下,我将我的文件 sharedCommonServers.properties 放置为具有特殊配置文件的默认应用程序属性文件。
重命名:
'sharedCommonServers.properties' to 'application-commonServers.properties'
通过使用“commonServers”扩展配置文件设置,spring 云配置正在获取它。
注意其他配置文件行中没有重叠的属性键。
所以在我设置的环境中...
-Dspring.profiles.active=prod,commonServers'
...我得到了我的默认 prod 配置和那些标记为“commonServers”的配置文件。
application.properties
application-prod.properties
application-commonServers.properties // the additional one
<appname>.properties
<appname>-prod.properties
// and <appname>-commonServers.properties if I would need it
感谢您的建议和想法!
【讨论】:
以上是关于Spring cloud config - 加载附加文件的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud Config 中属性的加载优先级是啥?
Spring Cloud Config客户端未从配置服务器加载值