springboot[1]-多模块共用配置文件

25 篇文章 0 订阅

子模块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>
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小安灬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值