春季引导Tomcat上多模块项目的外部配置

huangapple go评论72阅读模式
英文:

Spring Boot external configuration of multi-module project on Tomcat

问题

以下是翻译好的内容:

我有一个多模块的Maven项目,其中包括四个Spring Boot 2.3.x应用程序,它们作为WAR文件。我了解如何在它们独立运行时,为每个应用程序管理不同配置环境(开发、测试、质量保证)的属性。

现在,我将这些应用程序部署到外部的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不是一个选项。

英文:

I have multi-module Maven project with four Spring Boot 2.3.x applications as WAR files. I understand how to manage properties for each application for different profiles (dev, test, qa) when they run independently.

Now I deploy the applications on an external Tomcat 9.x server and I would like to have external property files for every single Spring Boot application.

The property files should be externalized outside Tomcat on the file system like this:

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

So for my understanding "spring.config.location" is not an option, because I can only specify the location and one property file per Tomcat instance.

I would like to externalize those files only for the active profile 'prod', so this is set:

spring.profiles.active=prod

Question:

What is best practice to achieve this?
Spring Cloud Config is not an option at this time.

答案1

得分: 1

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

> You can add more than one files using spring.config.name, use the profile conditions before reading the file (I didn't add those lines in the code, based on your preference you can add.)

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

> If the path needs to be dynamic and outside of the war file, use the following method to read the files.

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

> This is for war deployment and properties location will be dynamic. In case it is a static path, we can achieve it using annotations.
英文:
@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);
}

> You can add more then one files using spring.config.name, use the profile conditions before read the file(I didn't add those line on code, based on your preference you can add.)

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

> If the path will read dynamic and out of war file use the following method to read the files.

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;

}

> This is for war deployment and properties location will be dynamic, incase it is static path we can achieve on annotation itself.

huangapple
  • 本文由 发表于 2020年9月11日 17:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63844404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定