Rumah  >  Artikel  >  Java  >  Bagaimana untuk melaksanakan pensuisan berbilang persekitaran profil SpringBoot

Bagaimana untuk melaksanakan pensuisan berbilang persekitaran profil SpringBoot

WBOY
WBOYke hadapan
2023-05-17 10:04:111175semak imbas

Penukaran berbilang persekitaran profil SpringBoot

1 Konfigurasikan profil dan bina dalam fail pom
<!-- 环境 -->
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <!--默认激活配置-->
                <activebydefault>true</activebydefault>
            </activation>
            <properties>
                <!--当前环境-->
                <profile.name>dev</profile.name>
                <!--Nacos配置中心地址-->
                <config.server-addr>127.0.0.1:8848</config.server-addr>
                <!--Nacos配置中心命名空间,用于支持多环境.这里必须使用ID,不能使用名称,默认为空-->
                <config.namespace>DEV</config.namespace>
                <!--Nacos服务发现地址-->
                <discovery.server-addr>127.0.0.1:8848</discovery.server-addr>
                <discovery.namespace>DEV</discovery.namespace>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <!--当前环境-->
                <profile.name>test</profile.name>
                <!--Nacos配置中心地址-->
                <config.server-addr>127.0.0.1:8848</config.server-addr>
                <!--Nacos配置中心命名空间,用于支持多环境.这里必须使用ID,不能使用名称,默认为空-->
                <config.namespace>TEST</config.namespace>
                <!--Nacos服务发现地址-->
                <discovery.server-addr>127.0.0.1:8848</discovery.server-addr>
                <discovery.namespace>TEST</discovery.namespace>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <!--当前环境-->
                <profile.name>prod</profile.name>
                <!--Nacos配置中心地址-->
                <config.server-addr>127.0.0.1:8848</config.server-addr>
                <!--Nacos配置中心命名空间,用于支持多环境.这里必须使用ID,不能使用名称,默认为空-->
                <config.namespace>PROD</config.namespace>
                <!--Nacos服务发现地址-->
                <discovery.server-addr>127.0.0.1:8848</discovery.server-addr>
                <discovery.namespace>PROD</discovery.namespace>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 处理文件时替换文件中的变量 -->
                <filtering>true</filtering>
                <excludes>
                    <!-- 打包时排除文件,可自行添加test.yml -->
                    <exclude>application.yml</exclude>
                    <exclude>application-dev.yml</exclude>
                    <exclude>application-test.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <!-- 打包时所包含得文件 -->
                <includes>
                    <include>application.yml</include>
                    <include>application-${profiles}.yml</include>
                </includes>
            </resource>
        </resources>
    </build>

2
Bagaimana untuk melaksanakan pensuisan berbilang persekitaran profil SpringBoot3. Kandungan fail sumber Bootstrap

spring:
  profiles:
    active: test

4 >

Atas ialah kandungan terperinci Bagaimana untuk melaksanakan pensuisan berbilang persekitaran profil SpringBoot. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:yisu.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam