SpringBoot 多环境配置

SpringBoot 多环境配置

SpringBoot 多环境配置

在开发环境、生产环境、测试环境中使用的配置也许有些不同,使用同一个配置文件时每次package前都需要修改成指定的环境配置。写该笔记已解决上述困,坑又被填平了一个….真好。

当前开发环境: SpringBoot
适用场景: 多环境指定配置参数

划重点

  1. 命名规范application-xx.propertiesappliction-xx.yml
  2. 使用当前配置构建的 jar 文件包含多环境的配置文件,可以通过java -jar xx.jar --spring.profiles.active=prod命令加载指定的配置文件

多配制文件(推荐)

主文件

application.yml

1
2
3
4
5
6
7
8
9
10
11
spring:
profiles:
active: dev #指定应用`pacakage`时加载的文件名
mvc:
static-path-pattern: /**
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
resources:
static-locations:
- classpath:/static,classpath:/templates,file:${web.upload-path}

开发环境

application-dev.yml

1
2
3
4
5
6
7
8
9
10
11
12
server:
port: 8090
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@192.168.203.158:1521/test
username: test
password: test
jpa:
hibernate:
ddl-auto: validate
show-sql: true

生产环境

application-prod.yml

1
2
3
4
5
6
7
8
9
10
11
12
server:
port: 8090
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@127.0.0.1:1521/test
username: 数据库用户名
password: 数据库密码
jpa:
hibernate:
ddl-auto: validate
show-sql: true

单配置文件

application-conlony.yml

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
server:
port: 1001
spring:
profiles: server-1
eureka:
instance:
hostname: server-1
client:
register-with-eureka: false
fetch-registry: false
service-url:
# defaultZone: http://${eureka.instance.hostname}:${server.port}/${spring.application.name}
defaultZone: http://server-2:1002/eureka-server

---

server:
port: 1002
spring:
profiles: server-2
eureka:
instance:
hostname: server-2
client:
register-with-eureka: false
fetch-registry: false
service-url:
# defaultZone: http://${eureka.instance.hostname}:${server.port}/${spring.application.name}
defaultZone: http://server-1:1001/eureka-server

启动

1
使用`application-conlony.yml` 配置文件时,需要指定激活的配置项.(例如启动`server-1`,需运行`java -jar xx.jar --spring.profiles.active=server-1`)