我有四个SpringBoot2.3.x应用程序作为WAR文件的多模块Maven项目。当每个应用程序独立运行时,我了解如何为不同的配置文件(dev、test、qa)管理属性。
现在,我将应用程序部署在外部Tomcat 9.x服务器上,并且希望为每个Spring应用程序提供外部属性文件。
属性文件应该在文件系统的Tomcat外部进行外部化,如下所示:
c:/webapp/spring-config/boot-app1-prod.properties
c:/webapp/spring-config/boot-app2-prod.properties
c:/webapp/spring-config/boot-app3-prod.properties
c:/webapp/spring-config/boot-app4-prod.properties因此,就我的理解而言,"spring.config.location“不是一个选项,因为我只能指定每个Tomcat实例的位置和一个属性文件。
我只想将这些文件外部化为活动配置文件“prod”,因此设置如下:
spring.profiles.active=prod问题:
实现这一目标的最佳做法是什么?目前,不是一个选项。
发布于 2020-10-23 04:18:23
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
return springApplicationBuilder
.sources(ExtpropApplication.class)
.properties(getProperties());
}
public static void main(String[] args) {
new SpringApplicationBuilder(ExtpropApplication.class)
.sources(ExtpropApplication.class)
.properties(getProperties())
.run(args);
}您可以使用spring.config.name添加多个文件,在读取文件之前使用配置文件条件(根据您可以添加的首选项,我没有在代码中添加这些行)。
static Properties getProperties() {
Properties props = new Properties();
props.put("spring.config.name","boot-app1-prod.properties,boot-app2-prod.properties,boot-app3-prod.properties,boot-app4-prod.properties");
props.put("spring.config.location", "file://"+configPath());
return props;
}如果路径将读取动态和非war文件,请使用以下方法读取这些文件。
public static String configPath() {
File file = new File(".");
String path=file.getAbsolutePath();
int secondLast = path.length()-2;
String destinationPath = path.substring(0, path.lastIndexOf("/",secondLast)+1);
String resourcePath=destinationPath+"/webapps/";
return resourcePath;
}--这是用于war部署的,属性位置将是动态的,如果是静态路径,我们可以在注释本身上实现。
https://stackoverflow.com/questions/63844404
复制相似问题