Springboot多环境下多个配置文件规范配置方案

遇到的问题:
1、单个配置文件过大、多人协同困难
2、每一个环境又要部署给不同的租户

解决方案:
1、所有环境都一致且不会修改的配置放在application.yml里
2、每个环境创建该环境的配置文件夹,该环境下用到的配置都放在该文件夹下
3、对应环境文件夹下的配置文件拆分为多个

直接上配置
1、profiles的配置

<profiles>
        <profile>
            <id>local</id>
            <properties>
                <env>local,part</env>
                <profiles.active>local</profiles.active>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <id>test</id>
            <properties>
                <env>test,part</env>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>

        <profile>
            <id>pre</id>
            <properties>
                <env>pre,part</env>
                <profiles.active>pre</profiles.active>
            </properties>
        </profile>

        <profile>
            <id>staging</id>
            <properties>
                <env>staging,part</env>
                <profiles.active>staging</profiles.active>
            </properties>
        </profile>

        <profile>
            <id>product</id>
            <properties>
                <env>product,part</env>
                <profiles.active>product</profiles.active>
            </properties>
        </profile>
    </profiles>

2、配置文件结构


image.png

3、打包配置

<build>
        <resources>
            <resource>
                <directory>src/main/resources/profiles/${profiles.active}</directory>
                <includes>
                    <include>*.*</include>
                </includes>
                <filtering>true</filtering>
                <targetPath>./</targetPath>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>./</targetPath>
                <filtering>true</filtering>
                <excludes>
                    <exclude>profiles/**</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容