SpringBoot profles配置多环境

23/100

发布文章

u014427391

软件环境简介

这里介绍一下SpringBoot提供的profiles属性加上maven配置一下多环境,在实践生产中,正规一点的可能有开发环境、测试环境、预发布环境、生产环境等等,而这些环境的参数肯定都不一样,换环境的时候,经常需要修改参数,参数一多,维护起来很麻烦,所以SpringBoot提供了通过profiles配置来达到多环境配置,不需要项目一上生产环境还是预发布就改一堆配置文件。

软件环境:

  • application-dev(开发环境)
  • application-test(测试环境)
  • application-uat(预发布)
  • application-prod(生产环境)

配置文件格式可以为preperties或者yml,因为yml写起来比较简介,所以本博客介绍一yml的配置文件,介绍一下配置方式:

yml配置profiles

先介绍一下通过SpringBoot配置文件的这种方式,这里需要新建如图yml配置文件:

  • application-dev.yml(开发环境)
  • application-test.yml(测试环境)
  • application-uat.yml(预发布)
  • application-prod.yml(生产环境)

配置比较容易,需要在application.yml配置:

spring:
profiles:
active: dev

给出我的application.yml配置,可以参考:

server:
context-path: /jeeplatform
error:
whitelabel:
enabled: true spring:
profiles:
active: dev

开发环境apprlication-dev.yml配置,根据自己需要配置:

server:
port: 8080 spring: datasource: # 主数据源
shop:
url: jdbc:mysql://127.0.0.1:3306/jeeplatform?autoReconnect=true&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
username: root
password: root driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource # 连接池设置
druid:
initial-size: 5
min-idle: 5
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
# Oracle请使用select 1 from dual
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
# 打开PSCache,并且指定每个连接上PSCache的大小
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
use-global-data-source-stat: true # JPA配置
jpa:
database: mysql
hibernate:
show_sql: true
format_sql: true
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl # MVC配置
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp #Jedis配置
jedis :
pool :
host : 127.0.0.1
port : 6379
password : redispassword
timeout : 0
config :
maxTotal : 100
maxIdle : 10
maxWaitMillis : 100000

测试环境、预发布环境、生产环境的配置类似,根据需要配置

如果是jar,可以直接运行命令:

java -jar [jar名称].jar --spring.profiles.active=[环境参数(dev|test|uat|prod)]

eg:

java -jar myproject.jar --spring.profiles.active = dev

拓展,配置maven

这是另外的拓展,其实也是基于前面的配置,配置多环境信息在maven的pom文件里,主要目的是maven打包的时候,可以通过传环境参数对应package

配置,需要在maven的pom文件添加配置信息,这里设置默认开发环境,设置<activeByDefault>为true

<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
<project.packaging>jar</project.packaging>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<activatedProperties>test</activatedProperties>
<project.packaging>jar</project.packaging>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<activatedProperties>uat</activatedProperties>
<project.packaging>jar</project.packaging>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
<project.packaging>jar</project.packaging>
</properties>
</profile>
</profiles>

可以修改一下application.yml:直接读maven配置文件:

server:
context-path: /jeeplatform
error:
whitelabel:
enabled: true spring:
profiles:
active: @activatedProperties@
application:
name: @project.name@_${spring.profiles.active}

maven根据环境package

maven clean package -Pdev | -Ptest | -Puat | -Pprod

eg:直接package开发环境的

maven clean package -Pdev

SpringBoot系列之profles配置多环境(篇一)的更多相关文章

  1. SpringBoot系列之profles配置多环境(篇二)

    SpringBoot系列之profles配置多环境(篇二) 继续上篇博客SpringBoot系列之profles配置多环境(篇一)之后,继续写一篇博客进行补充 写Spring项目时,在测试环境是一套数 ...

  2. SpringBoot系列之外部配置用法简介

    SpringBoot系列之外部配置用法简介 引用Springboot官方文档的说法,官方文档总共列举了如下用法: 1.Devtools global settings properties on yo ...

  3. SpringBoot 系列教程自动配置选择生效

    191214-SpringBoot 系列教程自动配置选择生效 写了这么久的 Spring 系列博文,发现了一个问题,之前所有的文章都是围绕的让一个东西生效:那么有没有反其道而行之的呢? 我们知道可以通 ...

  4. SpringBoot日记——SpringMvc自动配置与扩展篇

    为了让SpringBoot保持对SpringMVC的全面支持和扩展,而且还要维持SpringBoot不写xml配置的优势,我们需要添加一些简单的配置类即可实现: 通常我们使用的最多的注解是: @Bea ...

  5. SpringBoot系列教程JPA之基础环境搭建

    JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Spring Data JPA是在 Hibernat ...

  6. SpringBoot系列之YAML配置用法

    1.全局配置 SpringBoot的全局配置文件有两种: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值,主要是 ...

  7. springboot系列四、配置模板引擎、配置热部署

    一.配置模板引擎 在之前所见到的信息显示发现都是以 Rest 风格进行显示,但是很明显在实际的开发之中,所有数据的显示最终都应该交由页面完成,但是这个页面并不是*.jsp 页面,而是普通的*.html ...

  8. nopCommerce 3.9 大波浪系列 之 NUnit 配置调试环境

    官网:http://nunit.org/ GitHub:https://github.com/nunit 本文只介绍NUnit在VS中的配置使用,进一步学习 NUnit 请参考官方文档. nopCom ...

  9. SpringBoot系列之学习教程汇总

    对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 一.配置篇 SpringBoot系列之@PropertySource读取yaml文件     >> source down ...

随机推荐

  1. Windows cmd 和 PowerShell 中文乱码问题解决

    临时方案: 在命令行下输入:chcp 65001 长期方案: 要修改注册表,自己网上搜吧

  2. OpenJDK下SpringBoot使用HttpSession时页面打开卡住

    近期将一个老项目向ARM版的CentOS7移植时,遇到了SpringBoot启动顺利,但访问页面卡住的问题.由于是aarch64架构,因此使用了openjdk,这个项目之前在x86_64环境下一直是用 ...

  3. 剑指:和为S的两个数字

    题目描述 输入一个数组和一个数字 s,在数组中查找两个数,使得它们的和正好是 s. 如果有多对数字的和等于s,输出任意一对即可. 你可以认为每组输入中都至少含有一组满足条件的输出. 样例 输入:[1, ...

  4. K8s容器编排

    K8s容器编排 Kubernetes(k8s)具有完备的集群管理能力: 包括多层次的安全防护和准入机制 多租户应用支撑能力 透明的服务注册和服务发现机制 内建智能负载均衡器 强大的故障发现和自我修复能 ...

  5. 服务器Oracle数据库配置与客户端访问数据库的一系列必要设置

    tips:所有路径请对应好自己电脑的具体文件路径. 一.服务器及Oracle数据库设置 1.刚装完的Oracle数据库中只有一个dba账户,首先需要创建一个用户. 2.配置监听,C:\app\Admi ...

  6. 使用git的几个常用指令

    1. 移除git:rm -rf .git/ 2.查看结果:ls -al 3.语法糖(查看列表):ll 4.将远程库的内容,更新到本地:git pull origin master: 5.添加到本地仓库 ...

  7. 学习9:MongoDB知识

    MongoDB学习笔记 1 基本介绍 基本概念 MongoDB**是一种面向文档的数据库管理系统,由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.2007年10月,MongoDB由10g ...

  8. pdfium:创建CreateNewDoc

    CreateNewDoc  //创建文档   https://github.com/PureFusionOS/android_external_pdfium/blob/040b899a933cdb37 ...

  9. js闭包和原型链好文

    http://www.cnblogs.com/wangfupeng1988/p/3977924.html

  10. 201871010104-陈园园《面向对象程序设计(java)》第十六周学习总结

    201871010104-陈园园<面向对象程序设计(java)>第十六周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...