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

子模块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的一项或者多项配置
  • 支持依据不同环境进行不同的配置

项目案例图

springboot[1]-多模块共用配置文件_第1张图片
springboot[1]-多模块共用配置文件_第2张图片
源码下载,代码量较少,可自行跟着写一下

代码说明

  • 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


<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.0modelVersion>

    <groupId>com.simplegroupId>
    <artifactId>configartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>module-1module>
        <module>base-configmodule>
    modules>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.0.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

project>

base-config.pom


<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>configartifactId>
        <groupId>com.simplegroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>base-configartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
        dependency>
    dependencies>

project>

module-1.pom


<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>configartifactId>
        <groupId>com.simplegroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>module-1artifactId>

    <dependencies>
        <dependency>
            <groupId>com.simplegroupId>
            <artifactId>base-configartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

    <profiles>
        <profile>
            <id>devid> 
            <properties>
                <environment>devenvironment>
            properties>
            <activation>
                <activeByDefault>trueactiveByDefault>
                
            activation>
        profile>
        <profile>
            <id>prodid> 
            <properties>
                <environment>prodenvironment>
            properties>
        profile>
    profiles>

project>

你可能感兴趣的:(java,后端,spring,boot,多模块配置)