SpringBoot后端多环境配置
1、前言
SpringBoot项目开发过程中我们可能会使用多种不同的环境。如开发环境、生产环境、测试环境等。
如何通过某种方式类灵活的使用不同的配置环境呢?
这里就需要使用多环境的配置方式来进行实现。
2、环境类型
主要用到了四个配置环境:
application.yml:主环境——用来指定需要使用何种分环境。
application-dev.yml:开发环境——用来配置开发环境;
application-pro.yml:生产环境——用来配置生产环境;
application-test.yml:测试环境——用来配置测试环境;
3、具体实现
3.1开发环境
该环境主要用来作为本地开发阶段使用的环境,配置本地数据库、本地缓存配置等信息。
# 开发环境配置文件
# 服务器端口
server:
port: 8003
# 服务名
spring:
application:
name: wiki
# mysql数据库配置
datasource:
url: jdbc:mysql://localhost:3306/wiki?serverTimezone=GMT%2B8&allowMultiQueries=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: xxxx
password: xxxx
# 返回json的全局时间格式
jackson:
date-format:
yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# 配置redis
redis:
host: 127.0.0.1
port: 6379
database: 0
connect-timeout: 1800000
# 最大阻塞等待时间
lettuce:
pool:
max-active: 20
max-wait: -1
# 最小空闲
max-idle: 5
min-idle: 0
# # 配置rocketMQ端口与地址、分组
# rocketmq:
# name-server: 127.0.0.1:9876
# producer:
# group: default
# 配置文件上传大小限制
servlet:
multipart:
max-file-size: 100000000
max-request-size: 100000000
# 配置mapper.xml路径
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.song.wiki.domain
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
# 分页插件
pagehelper:
auto-dialect: true
helper-dialect: mysql
reasonable: true
# 日志级别
logging:
level:
com.song: debug
3.2生产环境
该部分主要用来指定生产期间的环境,可以使用服务器配置等。
# 生产环境配置文件
# 服务器端口
server:
port: 8003
# 服务名
spring:
application:
name: wiki
# mysql数据库配置
datasource:
url: jdbc:mysql://服务器IP:3306/数据库名?serverTimezone=GMT%2B8&allowMultiQueries=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: 数据库用户名
password: 密码
hikari:
max-lifetime: 120000
# #下面这两个配置,可以在每次连接的时候判断一些连接是否有效
# druid:
# test-on-borrow: true
# test-while-idle: true
# 返回json的全局时间格式
jackson:
date-format:
yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# 配置redis
redis:
host: 服务器IP
password: 密码
port: 端口
database: 0
connect-timeout: 1800000
# 最大阻塞等待时间
lettuce:
pool:
max-active: 20
max-wait: -1
# 最小空闲
max-idle: 5
min-idle: 0
# # 配置rocketMQ端口与地址、分组
# rocketmq:
# name-server: 127.0.0.1:9876
# producer:
# group: default
# 配置文件上传大小限制
servlet:
multipart:
max-file-size: 100000000
max-request-size: 100000000
# 配置mapper.xml路径
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.song.wiki.domain
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
# 分页插件
pagehelper:
auto-dialect: true
helper-dialect: mysql
reasonable: true
# 日志级别
logging:
level:
com.song: debug
3.3测试环境
测试环境和其他环境配置类似。