Author Avatar
Jafir Mar 01, 2024

利用maven和spring来实现多套环境配置

配置说明

1、创建多个配置文件,dev(本地)、test(测试)、prod(生产)

1
说明:bootstrap优先于application被应用,后者可以覆盖前者。application是公共的,application-xx是相应环境的特殊配置
逻辑原理:通过spring的profile的active来动态切换使用哪套配置

dev

1
2
3
4
5
6
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.31.167:8848
register-enabled: false

test

1
2
3
4
5
6
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.31.167:8848
register-enabled: true

prod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring:
datasource:
username: srig
password: asdw@123
# 这里使用了p6spy 如果没有使用 即使用原生mysql
url: jdbc:p6spy:mysql://rm-bp1u46a5m077774ox.mysql.rds.aliyuncs.com:3306/dcmp_contract?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
cloud:
cloud:
txc:
# 不同微服务名字不同
txcAppName: contractprovider
txcServerGroup: trans_test.1585500393834815.HZ
accessKey: LTAI4GEHNrCLzDLxBHM94xUx
secretKey: nkiku7fPhpTNtmQcwoJFmlGOdas3BW
nacos:
discovery:
server-addr: 127.0.0.1:8848
register-enabled: true

2、通过maven的profile来创建环境变量参数,用于动态修改spring的profiles.active

image

3、maven配置多个profile

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!--  通过maven的profile来 动态修改spring的profile 达到多套配置切换使用的效果  -->
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
<skipDocker>true</skipDocker>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
<skipDocker>false</skipDocker>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
<skipDocker>true</skipDocker>
</properties>
</profile>
</profiles>

在build的docker配置中 设置skipDocker参数

1
<skipDocker>${skipDocker}</skipDocker>

image

4、配置发布到测试环境

增加 -Ptest 参数 可以给启动器起名 发布到测试环境
image
image
image

5、配置发布到aliyun生产环境

增加 -Pprod 参数 可以给启动器起名 发布到生产环境
image

6、可以给启动器起个名

image