本文通过maven打包的方式实现“SpringCloud配置中心使用多环境profile配置”。
如果希望通过更精简的启动命令的方式切换springcloud环境,可参考我的另一篇文章:SpringCloud配置中心使用多环境profile配置_胡海超的博客-CSDN博客说明由于springcloud配置中心和springboot的多环境配置并没有打通,所以使用java -jar xxxx.jar --spring.profiles.active=prod命令只能对springboot项目中的配置有效,并不能从配置中心获取不同的环境配置,想要实现目标还需要多做一些工作。项目配置在bootstrap.yml文件中配置配置中心,如下所示使用三个...https://blog.csdn.net/u011936655/article/details/86175439
1 设置maven打包方式
1.1 修改maven打包配置
打开pom.xml并添加以下内容
<profiles>
<profile>
<!--开发环境-->
<id>dev</id>
<properties>
<dev.environment>dev</dev.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!--生产环境-->
<id>test</id>
<properties>
<dev.environment>test</dev.environment>
</properties>
</profile>
<profile>
<!--生产环境-->
<id>prod</id>
<properties>
<dev.environment>prod</dev.environment>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 跳过目录 -->
<excludes>
<exclude>dev/*</exclude>
<exclude>test/*</exclude>
<exclude>prod/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${dev.environment}</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</resources>
</build>
1.2 添加不同profile对应的文件夹
在resources目录下分别建立dev、test、prod文件夹
src/main/resources/dev
src/main/resources/test
src/main/resources/prod
2 在不同环境目录下,分别添加不同环境springcloud配置
2.1 dev环境
resources/dev/application.yaml
spring:
cloud:
bootstrap:
enabled: false
config:
uri: http://localhost:8888
name: webclient
profile: dev
2.2 test环境
resources/test/application.yaml
spring:
cloud:
bootstrap:
enabled: false
config:
uri: http://localhost:8888
name: webclient
profile: test
2.3 prod环境
resources/prod/application.yaml
spring:
cloud:
bootstrap:
enabled: false
config:
uri: http://localhost:8888
name: webclient
profile: prod
3 通过maven打包使用不同切换不同环境
mvn clean package -Pdev -Dmaven.test.skip=true
mvn clean package -Ptest -Dmaven.test.skip=true
mvn clean package -Pprod -Dmaven.test.skip=true
通过maven打包后的jar包已经只包含指定环境的配置,通过java命令直接启动即可
java -jar xxxx.jar