springboot[1]-多模块共用配置文件
子模块1配置信息如下redis.propertiesmysql.propertieskafka.properties私有配置参数.properties子模块2配置信息如下redis.propertiesmysql.propertieskafka.properties私有配置参数.properties问题描述中间件配置信息大体一致,但子模块1、2均需配置,存在冗余配置可能存在子模块1、2配置信息不一
·
子模块1配置信息如下
- redis.properties
- mysql.properties
- kafka.properties
- 私有配置参数.properties
子模块2配置信息如下
- redis.properties
- mysql.properties
- kafka.properties
- 私有配置参数.properties
问题描述
- 中间件配置信息大体一致,但子模块1、2均需配置,存在冗余配置
- 可能存在子模块1、2配置信息不一致,导致错误,如 : 数据库连接信息、redis库信息
- 实际业务也会存在,大体配置相同个别配置不同,如 : kafka地址、参数配置信息相同,topic、group配置不一致
需求
- 实现相同的配置仅配置一次即可
- 支持模块对于公有配置可部分复写,如 : 复写kafka的topic、group
- 支持模块可选择性引入配置,如 : 引入mysql、redis、kafka的一项或者多项配置
- 支持依据不同环境进行不同的配置
项目案例图
代码说明
- PrintConfig.java : 打印配置信息
- Module1Application.java : 启动类测试
- SystemStartInit.java : 项目启动,打印配置信息
PrintConfig
package com.simple.config;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class PrintConfig {
private static final List<String> UN_PRINT = Arrays.asList("java.class.path", "config.md5Key", "config.rc4Key", "druid.password", "redis.password", "jasypt.encryptor.password");
public static void printApplicationProperties(ConfigurableEnvironment environment) {
StreamSupport.stream(
environment.getPropertySources().spliterator(), false)
.filter(propertySource -> (propertySource instanceof EnumerablePropertySource))
.map(propertySource -> ((EnumerablePropertySource) propertySource).getPropertyNames())
.flatMap(Arrays::stream).distinct().collect(Collectors.toMap(Function.identity(), environment::getProperty))
.entrySet().stream().forEach(entry -> {
String key = entry.getKey();
if (!UN_PRINT.contains(key)) {
if(key.startsWith("custom.")) {
System.err.println(String.format("properties,[%s = %s] . ", key, entry.getValue()));
}
}
}
);
}
}
SystemStartInit
package com.simple.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;
/**
* 系统启动后初始化,此方法会在服务器启动后(已经开放端口),加载配置信息
*/
@Component
public class SystemStartInit implements CommandLineRunner {
@Autowired
private ConfigurableEnvironment environment;
@Override
public void run(String... args) throws Exception {
PrintConfig.printApplicationProperties(environment);
}
}
Module1Application
package com.simple;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Module1Application {
public static void main(String[] args) {
SpringApplication.run(Module1Application.class, args);
}
}
application-common.properties
# 公有属性,无需区分环境
custom.common.name=common-name
custom.common.name2=common-name2
application-mysql-dev.properties
# mysql-dev环境
custom.mysql.name=mysql-dev
custom.mysql.name2=mysql-dev2
application-mysql-prod.properties
# mysql-prod环境
custom.mysql.name=mysql-prod
custom.mysql.name2=mysql-prod2
application-redis-dev.properties
# redis-dev环境
custom.redis.name=redis-dev
custom.redis.name2=redis-dev2
application-redis-prod.properties
# redis-prod环境
custom.redis.name=redis-prod
custom.redis.name2=redis-prod2
application.properties
# 项目名称
spring.application.name=module1
# 端口号
server.port=8080
# 运行环境
spring.profiles.active=@environment@
# 引入所需要的模块信息 (可选择性的引入 :可只引入redis、mysql、common等)
# 引入后若想覆盖配置,则可在application-@environment@.properties 进行重写,不可在本配置文件进行重写
spring.profiles.include=common,mysql-@environment@,redis-@environment@
# 可以新增模块的私有参数
custom.module1.test=custom.module1.test
application-dev.properties
# 此配置可选,文件可放到 resources/config文件夹下,因放到config下会失去提示,故案例放在resources下
# 可以复写 base-config 里面的配置参数信息
custom.mysql.name=override-mysql-custom-dev
custom.common.name=override-common-name-custom-dev
# 可以配置模块的私有参数
custom.module1.name=private-module1-name-dev
application-prod.properties
# 此配置可选,文件可放到 resources/config文件夹下,因放到config下会失去提示,故案例放在resources下
# 可以复写 base-config 里面的配置参数信息
custom.mysql.name=override-mysql-custom-prod
custom.common.name=override-common-name-custom-prod
# 可以配置模块的私有参数
custom.module1.name=private-module1-name-prod
config.pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.simple</groupId>
<artifactId>config</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>module-1</module>
<module>base-config</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
base-config.pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>config</artifactId>
<groupId>com.simple</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>base-config</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
</project>
module-1.pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>config</artifactId>
<groupId>com.simple</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module-1</artifactId>
<dependencies>
<dependency>
<groupId>com.simple</groupId>
<artifactId>base-config</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id> <!-- 开发环境 -->
<properties>
<environment>dev</environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
<!-- 默认是dev环境 -->
</activation>
</profile>
<profile>
<id>prod</id> <!-- 生产环境 -->
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>
</project>
更多推荐
已为社区贡献2条内容
所有评论(0)