1. Configure profile and build in the pom file
<!-- 环境 --> <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. Create three files dev\test
3. Bootstrap source file content
spring: profiles: active: test
4. bootstrap-dev content
server: port: 8080 spring: profiles: dev
5. bootstrap-test content
server: port: 8080 spring: profiles: test
6. Switch
The above is the detailed content of How to implement SpringBoot profile multi-environment switching. For more information, please follow other related articles on the PHP Chinese website!