簡體   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