Java Tomcat上多模块项目的Spring Boot外部配置
我有一个多模块Maven项目,其中有四个SpringBoot2.3.x应用程序作为WAR文件。我了解在独立运行时如何管理不同配置文件(开发、测试、质量保证)的每个应用程序的属性 现在,我将应用程序部署在外部Tomcat9.x服务器上,我希望每个Spring引导应用程序都有外部属性文件 属性文件应在文件系统上的Tomcat外部具体化,如下所示:Java Tomcat上多模块项目的Spring Boot外部配置,java,spring-boot,configuration,tomcat9,Java,Spring Boot,Configuration,Tomcat9,我有一个多模块Maven项目,其中有四个SpringBoot2.3.x应用程序作为WAR文件。我了解在独立运行时如何管理不同配置文件(开发、测试、质量保证)的每个应用程序的属性 现在,我将应用程序部署在外部Tomcat9.x服务器上,我希望每个Spring引导应用程序都有外部属性文件 属性文件应在文件系统上的Tomcat外部具体化,如下所示: c:/webapp/spring-config/boot-app1-prod.properties c:/webapp/spring-config/boo
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
问题:
实现这一目标的最佳实践是什么?
SpringCloud配置目前不是一个选项
@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添加多个文件,在读取文件之前使用概要文件条件(根据您可以添加的首选项,我没有在代码中添加这些行)
如果路径将读取动态和非战争文件,请使用以下方法读取文件
这是用于war部署的,属性位置将是动态的,以防它是静态路径,我们可以在注释本身上实现
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;
}
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;
}