欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

从0开始学习SpringCould(7)--SpringBoot 多环境配置

程序员文章站 2024-02-10 17:06:16
...

为什么需要多环境配置?

真正开发中,环境一般都是分离的,多数情况下都分为:开发环境、测试环境、生产环境等等。
以这三套环境为例,如果这三套环境有三个数据库,每次上线都要修改配置文件中数据库链接,是不太现实的,因为现在几乎都是自动化部署,因此就需要引入多环境配置。

本文以数据库链接为例进行演示,使用到的代码均是前几篇中所产生的;


1、在application.yml中配置开发及测试环境MySQL链接

yml 使用 --- 来进行内容分离,可以理解为将一个文件分成了几个文件
文末有完整的application.yml示例
配置开发及测试数据源连接 示例如下:

---
##########################################################
###################  开发环境的profile  ##################
##########################################################
spring:
  profiles: dev
  datasource:       # mybatis 配置,使用druid数据源
    url: jdbc:mysql://localhost:3306/springboot_demo?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root123
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

---
##########################################################
###################  测试环境的profile  ##################
##########################################################
spring:
  profiles: test
  datasource:       # mybatis 配置,使用druid数据源
    url: jdbc:mysql://10.0.201.196:3306/springboot_demo?useUnicode=true&characterEncoding=UTF-8
    username: user
    password: password
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

注意:properties文件,不能使用这种配置方式

到此,我们已经定义出了开发及测试环境的两套数据源,下一步要指定出当前活动环境,也就是默认环境;


2、指定活动环境

指定活动环境,在application.yml的第一部分(以 “---“分割)定义如下代码即可

spring:
  profiles:
    active: dev

这段代码的意思就是,指定spring: profiles: dev 的环境为默认活动环境


3、开始第一次测试

启动应用–》访问swagger-ui–》填写测试数据
(这些教程均在前面的博客中有介绍,如果不太清楚建议查看前期博客)

从0开始学习SpringCould(7)--SpringBoot 多环境配置


查看本地数据库数据:
从0开始学习SpringCould(7)--SpringBoot 多环境配置

可以看到已经多了一条数据。


4、如何动态切换环境

4.1、使用IDEA动态切换环境配置

“Edit Configuractions..” –> “SpringBoot”–> “VM Options” –> “-Dspring.profiles.active=test”

从0开始学习SpringCould(7)--SpringBoot 多环境配置

这个配置是指,设置spring.profiles.active为test环境

再次测试:启动应用,去swagger发送一条测试数据,步骤参考3


查看测试数据库:
从0开始学习SpringCould(7)--SpringBoot 多环境配置

可以看到数据已经存入了test环境对应的测试数据库;

4.2、使用jar包动态切换环境配置

在测试及生产环境的Linux服务器上,需要直接启动jar包的方式来启动应用,多环境配置的切换当做一个参数传递
具体如下:

java -jar **.jar --spring.profiles.active=test


打jar包测试
从0开始学习SpringCould(7)--SpringBoot 多环境配置

再次使用swagger-ui发送一遍测试数据
查看test环境对应的MySQL数据:
从0开始学习SpringCould(7)--SpringBoot 多环境配置

新增了一条数据,没问题 多环境配置完成。

5、完整application.yml示例

server:
  port: 9000 # 端口号
  context-path: /${spring.application.name}
spring:
  profiles:
    active: dev
  application:
    name: demo    # 项目名称
  http:
    encoding:     # http编码
      force: true
      charset: UTF-8
      enabled: true
  freemarker:   # freemarker配置
    suffix: .ftl
    check-template-location: true
    defaultEncoding: UTF-8
    templateLoaderPath: classpath:/demo
    content-type: text/html
    request-context-attribute: request
logging:          # 日志
  level.root: info
  level.com.example.springboot_demo: debug
  path: logs/
  file: demo.log

---
##########################################################
###################  开发环境的profile  ##################
##########################################################
spring:
  profiles: dev
  datasource:       # mybatis 配置,使用druid数据源
    url: jdbc:mysql://localhost:3306/springboot_demo?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root123
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

---
##########################################################
###################  测试环境的profile  ##################
##########################################################
spring:
  profiles: test
  datasource:       # mybatis 配置,使用druid数据源
    url: jdbc:mysql://10.0.201.196:3306/springboot_demo?useUnicode=true&characterEncoding=UTF-8
    username: user
    password: password
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20


本篇结束,谢谢!

更多内容请关注微信公众号:

从0开始学习SpringCould(7)--SpringBoot 多环境配置