SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)
实现项目的多环境配置的方法有很多,比如通过在Pom.xml中配置profiles(最常见) 然后在Install项目打War包的时候,根据需求打不同环境的包,如图:

这种配置多环境的方法在SSM框架中使用的最多,但在SpringBoot中使用最多的是在启动SpringBoot项目的时候指定运行环境,下面也是主要描述这种配置的方法:
1.添加配置文件
在SpringBoot的Resources目录下建4个配置文件 application.yml、application-dev.yml、application-qa.yml、application-online.yml
dev:开发环境
qa:测试环境
online:生产环境
然后在application.yml配置文件中配置默认的运行环境:
spring:
profiles:
active: dev
然后在dev、qa、online中分别配置不同的配置内容,例如变更端口:
dev
server:
port: 8085
servlet:
context-path: /api
tomcat:
max-threads: 100
connection-timeout: 5000
spring:
profiles: dev
qa
server:
port: 8086
servlet:
context-path: /api
tomcat:
max-threads: 100
connection-timeout: 5000
spring:
profiles: qa
online
server:
port: 8087
servlet:
context-path: /api
tomcat:
max-threads: 100
connection-timeout: 5000
spring:
profiles: online
然后在 SpringBoot系统列 1 - HelloWorld! 的基础上继续添加代码,新建WebConfig用于存放SpringBoot的一些配置信息(SpringBoot的配置即可以在配置文件中配置,也可以在类中配置):
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration; /**
* 配置类
* @author XIHONGLEI
* @date 2018-10-31
*/
@SpringBootConfiguration
public class WebConfig { @Value("${server.port}")
public String port;
}
然后改造一下HelloContrlller,为了区分环境,我们在请求/api/hello的时候将端口号展示出:
import com.hello.WebConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired
private WebConfig webConfig; @RequestMapping("hello")
public String hello() {
return "Hello World! port:".concat(webConfig.port);
}
}
然后在pom.xml配置Jar包的打包配置:
<packaging>jar</packaging>
<build>
<finalName>spring-boot-hello</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>com.hello.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
然后Install,找打Jar包 spring-boot-hello.jar;
在Window控制台或者Linux中可以使用java -jar spring-boot-hello.jar来启动SpringBoot项目,然后通过在后方添加--spring.profiles.active来指定启动SpringBoot项目时使用的环境:
# Dev环境
$ java -jar spring-boot-hello.jar --spring.profiles.active=dev # qa环境
$ java -jar spring-boot-hello.jar --spring.profiles.active=qa # online环境
$ java -jar spring-boot-hello.jar --spring.profiles.active=online
例启动Online环境:

然后通过 http://localhost:8087/api/hello 来访问,因为Online中配置的端口是8087

完成!
在IDEA中怎么在运行的时候选定执行环境,可以通过配置Application的program arguments中配置运行环境:

SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)的更多相关文章
- SpringBoot系统列 1 - HelloWorld!
学习SpringBoot系统列之HelloWorld! 1.新建一个Maven项目 2.添加POM配置 <parent> <groupId>org.springframewor ...
- WIN7系统JavaEE(java+tomcat7+Eclipse)环境配置
https://jingyan.baidu.com/article/3a2f7c2e62d25e26afd611fa.html WIN7系统JavaEE(java+tomcat7+Eclipse)环境 ...
- SpringBoot在启动时的多环境配置以及加载顺序
通常我们在开发完成一个SpringBoot项目时,总是要打包部署的. 在启动SpringBoot应用时,我们常常会使用命令java -jar xxx.jar来启动这个服务. 命令java -jar 除 ...
- SpringBoot | 第五章:多环境配置
前言 写上一篇看英文资料,耗费了心力呀,这章,相对来说简单点.也比较熟悉,但是这很实用.不扯了,开始~ 多环境配置 maven的多环境配置 springboot多环境配置 总结 老生常谈 多环境配置 ...
- windows系统下简单nodej.s环境配置 安装
国内目前关注最高,维护最好的一个关于nodejs的网站应该是http://www.cnodejs.org/ windows系统下简单nodejs环境配置. 第一步:下载安装文件 下载地址:官网 htt ...
- 配置文件,环境配置和war报分离,方便生产更改
在生产环境实现配置文件和war包 的分离,为方便在必要的时候进行一定的更改,可以避免修改包,但是需要重启 最初为这样的选择配置,单不知为何未生效,修改为配置2配置方法,但不灵活,待跟进.配置1: &l ...
- windows系统下简单node.js环境配置 安装
国内目前关注最高,维护最好的一个关于nodejs的网站应该是http://www.cnodejs.org/ windows系统下简单nodejs环境配置. 第一步:下载安装文件 下载地址:官网 htt ...
- (四)Spring Boot之配置文件-多环境配置
一.Properties多环境配置 1. application.properties配置激活选项 spring.profiles.active=dev 2.添加其他配置文件 3.结果 applica ...
- Ubuntu系统下《汇编语言》环境配置
说明 1.系统:Ubuntu codists@pc:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Des ...
随机推荐
- DOCKER上安装HASSIO
更新源列表命令 sudo apt-get update sudo apt-get upgrade –y This is the list of packages you need to have av ...
- shell脚本使用技巧3--函数调用
定义函数 function fname() { statements; } 或者 fname() { statements; } 传递参数给函数: fname arg1 arg2; ex: 函数参数定 ...
- Java 动态代理 Demo
相比于静态代理,动态代理避免了开发者编写各个繁锁的静态代理类,只需指定一组接口及目标类对象就能动态地获取代理对象. 使用动态代理的六大步骤: 1 通过实现InvocationHandler接口来自定义 ...
- python之进程和线程
1 操作系统 为什么要有操作系统 ? 操作系统位于底层硬件与应用软件之间的一层 工作方式:向下管理硬件,向上提供接口 操作系统进程切换: 出现IO操作 固定时间 2 进程和线程的概念 进程就是一个程序 ...
- JAVA自学作业02
JAVA自学作业02 1.什么是标识符?由哪些部分组成?常见的命名规则有哪些? 标识符是用户为变量的内存空间所定义的字符序列: 可以由字母.下划线.美元符号以及数字组成,但数字不可作为首字符.标识符不 ...
- hdu3466 Proud Merchants(01背包)
https://vjudge.net/problem/HDU-3466 一开始想到了是个排序后的背包,但是排序的策略一直没对. 两个物品1和2,当p1+q2>p2+q1 => q1-p1& ...
- 【小y设计】二维码条形码打印编辑器
条码打印,价格标签打印,需要对打印进行排版,于是设计了一个简单的编辑器 支持条码二维码打印进行编辑排版,支持文字.图片.条码.二维码.直线,能自由拖拉,删除,并可保存为模版. 界面如下 (下载Demo ...
- ORACLE数据恢复方法(提交事务也可以)
今天在操作数据库的时候,发现数据操作错误,想要恢复,但是没有用事务,按理说,设置成不默认提交事务,此时所做的各种操作都没有反应到数据库中.这时,你可以rollback事务,撤销所有未提交的修改.不过, ...
- too much recursion(太多递归)Uncaught RangeError: Maximum call stack size exceeded BootstrapValidator报错
在BootstrapValidator中已默认遵守Bootstrap规则,form里的每个输入项目必需包含在类为form-group的标签里,否则BootstrapValidator中定义的field ...
- Docker 集群Swarm创建和Swarm Web管理
关于Docker Swarm更多的介绍请查看<Docker管理工具-Swarm部署记录> 一.环境配置 1.安装环境 # cat /etc/redhat-release CentOS Li ...