从0开始学习SpringCould(7)--SpringBoot 多环境配置
来源:互联网 发布:淘宝刷单工作室 编辑:程序博客网 时间:2024/06/05 16:19
为什么需要多环境配置?
真正开发中,环境一般都是分离的,多数情况下都分为:开发环境、测试环境、生产环境等等。
以这三套环境为例,如果这三套环境有三个数据库,每次上线都要修改配置文件中数据库链接,是不太现实的,因为现在几乎都是自动化部署,因此就需要引入多环境配置。
本文以数据库链接为例进行演示,使用到的代码均是前几篇中所产生的;
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–》填写测试数据
(这些教程均在前面的博客中有介绍,如果不太清楚建议查看前期博客)
查看本地数据库数据:
可以看到已经多了一条数据。
4、如何动态切换环境
4.1、使用IDEA动态切换环境配置
“Edit Configuractions..” –> “SpringBoot”–> “VM Options” –> “-Dspring.profiles.active=test”
这个配置是指,设置spring.profiles.active为test环境
再次测试:启动应用,去swagger发送一条测试数据,步骤参考3
查看测试数据库:
可以看到数据已经存入了test环境对应的测试数据库;
4.2、使用jar包动态切换环境配置
在测试及生产环境的Linux服务器上,需要直接启动jar包的方式来启动应用,多环境配置的切换当做一个参数传递
具体如下:
java -jar **.jar --spring.profiles.active=test
打jar包测试
再次使用swagger-ui发送一遍测试数据
查看test环境对应的MySQL数据:
新增了一条数据,没问题 多环境配置完成。
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: requestlogging: # 日志 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 多环境配置
- 从0开始学习SpringCould(1)--SpringBoot项目搭建
- 从0开始学习SpringCould(3)--SpringBoot 定时任务
- 从0开始学习SpringCould(4)--SpringBoot 集成freemarker
- 从0开始学习SpringCould(6)--SpringBoot 集成swagger2
- 从0开始学习SpringCould(8)--SpringBoot 分环境读取配置文件
- 从0开始学习SpringCould(2)--SpringBoot yml简单配置
- 从0开始学习SpringCould(5)--SpringBoot 集成Mybatis使用Druid数据源
- 从0开始学习SpringCould(9)--Eureka 服务注册发现
- 从今天开始学习springboot--
- 从0开始学习android开发---java环境配置
- 从0开始学习SpringBoot-第1天
- 从0开始学习SpringBoot-第2天
- 从0开始学习SpringBoot-第3天
- 从0开始配置BCI工作环境
- springcould学习(二)之eureka
- springboot多环境配置
- SpringBoot属性配置实战(多环境)
- 5. Longest Palindromic Substring
- Dlib + python + opencv 实时人脸68特征点提取
- 数据结构 第五周项目二 建立链栈算法库
- RocketMQ实战(二)
- caffe版本-车型检测-A Large-Scale Car Dataset for Fine-Grained Categorization and Verification
- 从0开始学习SpringCould(7)--SpringBoot 多环境配置
- Leetcode Two Maximum Subarray Questions
- 链路层--计算机网络自顶向下笔记(五)
- Day018
- 分答项目_技能点:移动端domobile***参数
- 第三章 Java基本程序设计结构(2)
- Hibernate5.x动态表名实现
- mac-sourceTree创建新仓库时账号密码错误报无效URL路径
- IDEA 创建Spring mvc 项目