开发的流程是本地>测试>预发布>正式,所以不同的环境,肯定是不同的配置文件,所以我们需要针对不同的环境做不同的配置切换.

下面我们来说说 springboot 是怎么来切换的:

1、package 方式使用 war,应用部署到 tomcat

先来看一下文件结构,

可以看到,这里我们有 3 个 properties 的配置文件,还有 1 个 application.yml文件,这个文件里有一个参数 spring.profiles.active 的选项,这个用来激活不同环境的配置.

然后重点是看下面 pom.xml 的配置,如下:

<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>pro</id>
<properties>
<profileActive>pro</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles> <build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.yml</exclude>
<exclude>application.properties</exclude>
<exclude>application-dev.properties</exclude>
<exclude>application-test.properties</exclude>
<exclude>application-pro.properties</exclude>
</excludes>
</resource> <resource>
<directory>src/main/resources</directory>
<includes>
<include>application-${profileActive}.properties</include>
<include>application.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>@</delimiters>
<encoding>utf-8</encoding>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>

然后当你在构建项目的时候可以这样来写命令 mvn clean package -Dmaven.test.skip=true -P dev/test/pro(根据不同的环境来切换不同的变量值)

2、package 方式是 jar,服务器上使用 java -jar 命令来启动

pom.xml 里的 profiles 配置可以完全删除,还有 build 里这段配置也可以删除.

然后在项目启动的时候通过指定spring.profiles.active 参数来激活配置

nohup java -jar *.jar -dprocesName=templateDecoration --spring.profiles.active=test > /xxx/xxx/xxx.log &

springboot 不同环境切换不同的配置文件的更多相关文章

  1. SpringBoot配置文件-多环境切换

    profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境: 多个文件-配置多环境: 需要多个配置文件,文件名可以是 application-{prof ...

  2. SpringBoot 多环境配置文件切换

    背景 很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同 ...

  3. Java开发学习(三十七)----SpringBoot多环境配置及配置文件分类

    一.多环境配置 在工作中,对于开发环境.测试环境.生产环境的配置肯定都不相同,比如我们开发阶段会在自己的电脑上安装 mysql ,连接自己电脑上的 mysql 即可,但是项目开发完毕后要上线就需要该配 ...

  4. spring-boot多环境配置文件

    spring-boot多环境配置文件 目录 配置 多环境配置文件名称要遵循格式 application-{profile}.yml application.yml spring: profiles: ...

  5. 微服务-springboot多环境配置(开发生产测试环境切换)

    springboot根据spring.profiles.active会去寻找应该加载开发环境配置还是生产环境配置 application.properties #生产环境,开发环境,测试环境切换 pr ...

  6. SpringBoot学习笔记(八):SpringBoot启动端口+访问路、SpringBoot配置文件yml、SpringBoot多环境区分、SpringBoot打包发布

    SpringBoot启动端口+访问路径 配置文件: server.port=9090 server.context-path=/springboot 现在只能用http://127.0.0.1:909 ...

  7. 「快学springboot」SpringBoot多环境配置文件

    前言 我们都知道springboot的配置卸载application.properties配置文件上(或者application.yml).但是,如果想要把不同的环境(如开发环境,测试环境,生产环境) ...

  8. spring boot--日志、开发和生产环境切换、自定义配置(环境变量)

    Spring Boot日志常用配置: # 日志输出的地址:Spring Boot默认并没有进行文件输出,只在控制台中进行了打印 logging.file=/home/zhou # 日志级别 debug ...

  9. SpringBoot-多环境切换相关(六)

    多环境切换 profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境: 方式一:多配置文件 我们在主配置文件编写的时候,文件名可以是 applicat ...

随机推荐

  1. django RESTful设计方法

    1. 域名 应该尽量将API部署在专用域名之下. https://api.example.com 如果确定API很简单,不会有进一步扩展,可以考虑放在主域名下. https://example.org ...

  2. vue使用qrcode插件生成二维码

    参考:https://www.jianshu.com/p/d3883e020d99 步骤: 第一步:vue-cli下载插件 cnpm install --save qrcodejs2 第二步:组件中引 ...

  3. Python入门day04_函数与装饰器

    一.函数入门 什么是函数: # 函数:函数是一系列代码的集,用来完成特定功能的代码块,类似于工具,可以重复不但的去使用 为什么要有函数: # 优点:# 1. 避免代码的冗余 # 2. 让程序代码结构更 ...

  4. iview 路由权限判断的处理

    主要是在main.vue做处理 其它地方不需要处理 menuList () { let getRouter = JSON.parse(sessionStorage.getItem('getUserDa ...

  5. MVC开发T4代码生成之一----文本模板基础

    T4文本模板 T4全写为Text Template Transformation Toolkit,是一种编程辅助工具,用来使程序代码自(懒)动(猿)生(福)成(利)的工具.MVC开发中大量使用了T4模 ...

  6. 获取txt md5值上传文件完整性校验

    网络上传文件到服务器 做md5 校对.判断文件是否破坏 首先求txt文件的md5值 ,1万条数据 求出的值 文件MD5:e5467b6a8e3c26af8c5af0bda3739280 服务器处理程序 ...

  7. Spring Boot Web应用开发 CORS 跨域请求支持:

    Spring Boot Web应用开发 CORS 跨域请求支持: 一.Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等CORS与JSONP相比 1. JSONP只能实现 ...

  8. day 1:计算机发展史和组成部分

    本节内容: 1,计算机的发展史 2,计算机的组成 1,计算机的发展史 1946年2月14日,由美国军方定制的世界上第一台电子计算机“电子数字积分计算机”(ENIAC Electronic Numeri ...

  9. centos服务器监控 服务器虚拟机里面的客户端zabbix-agent安装

    开启zabbix要用的端口, 也可以关闭服务器的防火 [html] view plain copy #vim /etc/sysconfig/iptables -A INPUT -m state --s ...

  10. python基础入门之函数基础

    **python函数**一.def语名 def语名在运行的时候创建一个新的函数对象并且赋值一个变量名 一个def语句可以出现在任一语句可以出现的地方(python中所有的语名都是实时运行的,没有编译这 ...