繁体   English   中英

Tomcat上多模块项目的Spring Boot外部配置

[英]Spring Boot external configuration of multi-module project on Tomcat

我有四个 Spring Boot 2.3.x 应用程序作为 WAR 文件的多模块 Maven 项目。 我了解在独立运行时如何管理不同配置文件(dev、test、qa)的每个应用程序的属性。

现在我将应用程序部署在外部 Tomcat 9.x 服务器上,并且我希望每个 Spring Boot 应用程序都有外部属性文件。

属性文件应该在 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

题:

实现这一目标的最佳实践是什么? Spring Cloud Config 目前不是一个选项。

@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;

}

这是用于战争部署和属性位置将是动态的,以防它是我们可以在注释本身上实现的静态路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM