使用来自多个应用程序的单个配置文件特定的属性文件。
我有一些Spring-Boot微服务,我想对所有微服务使用一个特定于个人资料的属性文件。特定于配置文件的属性文件应在罐子之外。
示例:
application-common-profile.properties 文件和jar文件在同一文件夹中,
java -jar Microservice-1.jar --spring.config.location=classpath:/application.properties --spring.profiles.active=common-profile
java -jar Microservice-2.jar --spring.config.location=classpath:/application.properties --spring.profiles.active=common-profile
以上命令不会占据共同的profile属性。请帮助实现这一目标。谢谢。
重要细节
此JAR文件不包含“应用程序 - 命令profile.properties”文件。该文件位于罐子外,但位于同一文件夹中。如果我将“应用程序”文件放在同一foder中,那么它正在使用以下命令工作(检索属性)。
java -jar microservice-1.jar -spring.config.location = c:/folderpath/-spring.profiles.actiles.active = common-profile
我可以放置其他微服务的application.properties'properties'文件在具有不同名称的同一文件夹中(例如:应用程序 - 麦克罗服务-2.properties)。以及带有不同名称的配置文件属性文件。(例如:application-microservice-2-common.properties)拥有不同的application.properties文件。但是需要拥有一个共同的配置文件属性文件。
filex
我在同一文件夹中添加了公共配置文件属性文件(application-common.properties)和jar,并简单地在命令下运行,并且正在工作。我认为添加不必要的论点是问题所在。
java -jar Microservice-1.jar --spring.profiles.active=common
java -jar Microservice-2.jar --spring.profiles.active=common
I have a few spring-boot microservices and I want to use a single profile-specific property file for all the microservices. profile-specific property file should be outside of the jars.
example:
The application-common-profile.properties file and jar files are inside the same folder
java -jar Microservice-1.jar --spring.config.location=classpath:/application.properties --spring.profiles.active=common-profile
java -jar Microservice-2.jar --spring.config.location=classpath:/application.properties --spring.profiles.active=common-profile
the above commands won't take the common-profile properties. please help to achieve this. Thanks.
IMPORTANT DETAILS
This jar file doesn't contain 'application-common-profile.properties' file. this file resides outside the jar but in the same folder. if I put my 'application.properties' file inside the same foder then it is working(retrieving the properties) with the below command.
java -jar Microservice-1.jar --spring.config.location=C:/folderpath/ --spring.profiles.active=common-profile
I can place other microservices 'application.properties' files in same folder with different names(ex: application-microservice-2.properties). and also the profile property files with different names.(ex: application-microservice-2-common.properties ) It's okay having different application.properties files. but need to have one common profile property file.
FIXED
I added common profile property file(application-common.properties) and jars in same folder and run below commands simply and it is working. I think adding unnecessary arguments was the problem.
java -jar Microservice-1.jar --spring.profiles.active=common
java -jar Microservice-2.jar --spring.profiles.active=common
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的application.properties文件被捆绑在您的jar中,则只需将特定于个人资料的属性文件添加到您的工作目录中,就可以将其拾取。但是,当您设置
spring.config.location
时,您将覆盖Spring会从中查找这些属性的路径时,您应该删除此属性,这不是必需的。如果不在JAR中捆绑,Spring将在您的工作目录中查找属性文件。就个人而言,我会避免尝试将属性文件维护在环境中,以支持环境变量。
将您的罐子与顶级应用程序捆绑在一起。其中包含的过程,以及对于给定环境不同的变量,为其设置了环境变量。
例如,如果您想覆盖spring.datasource.driver-class-name,请设置环境变量spring_datasource_driver_class_name。我认为您会发现这种方法更加灵活。
If your application.properties file is bundled in your jar, then simply adding the profile-specific property file to your working directory will allow it to be picked up. However, when you set
spring.config.location
you're overriding the path that Spring will look up these properties from, you should drop this, it's not necessary. Spring will look in your working directory for the properties files by default if they're not bundled in the jar.Personally, I would avoid trying to maintain the property files in the environments in favor of environment variables.
Bundle your jar with the top-level application.properties included in it, and for the variables that are different given the environment, set environment variables for them.
e.g. if you want to override spring.datasource.driver-class-name, set the environment variable SPRING_DATASOURCE_DRIVER_CLASS_NAME. I think you'll find this approach is far more flexible.