博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)
阅读量:6948 次
发布时间:2019-06-27

本文共 4529 字,大约阅读时间需要 15 分钟。

 实现项目的多环境配置的方法有很多,比如通过在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: 5000spring:  profiles: dev

qa

server:  port: 8086  servlet:    context-path: /api  tomcat:    max-threads: 100  connection-timeout: 5000spring:  profiles: qa

online

server:  port: 8087  servlet:    context-path: /api  tomcat:    max-threads: 100  connection-timeout: 5000spring:  profiles: online

然后在   的基础上继续添加代码,新建WebConfig用于存放SpringBoot的一些配置信息(SpringBoot的配置即可以在配置文件中配置,也可以在类中配置):

import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringBootConfiguration;/** * 配置类 * @author XIHONGLEI * @date 2018-10-31 */@SpringBootConfigurationpublic 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;@RestControllerpublic class HelloController {    @Autowired    private WebConfig webConfig;    @RequestMapping("hello")    public String hello() {        return "Hello World! port:".concat(webConfig.port);    }}

然后在pom.xml配置Jar包的打包配置:

jar
spring-boot-hello
src/main/java
**/*.yml
**/*.properties
**/*.xml
false
src/main/resources
**/*.yml
**/*.properties
**/*.xml
false
org.springframework.boot
spring-boot-maven-plugin
true
com.hello.Application
repackage
maven-resources-plugin
2.5
UTF-8
true
org.apache.maven.plugins
maven-surefire-plugin
2.18.1
true
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
1.8

然后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中配置运行环境:

 

转载地址:http://tzenl.baihongyu.com/

你可能感兴趣的文章
STP
查看>>
yii 一些引用路径的方法
查看>>
vue图片上传相关(持续更新)
查看>>
java内存简单总结
查看>>
实现windows server 2008 R2多用户同时登陆或者同一用户名同时登陆
查看>>
PMD 插件的安装和使用
查看>>
利用JavaScript生成二维码并且中间有logo
查看>>
泛型小例子
查看>>
译文:C#中的弱事件(Weak Events in C#)
查看>>
抽象工厂模式
查看>>
Maven
查看>>
Unix-Linux 编程实践教程 第八章 小结
查看>>
linux下ElasticSearch安装及问题
查看>>
2019测试指南-web应用程序安全测试(二)指纹Web应用程序
查看>>
Quartus Prime 下载程序到FPGA流程
查看>>
php instanceof 运算符
查看>>
5月3日云栖精选夜读丨寒武纪重磅发布首款AI云芯片,阿里专家告诉你必须注意的Java编程细节...
查看>>
机器学习从业人员到底做什么?
查看>>
MyBatis mapper.xml处理sql中的 大于,小于,大于等于,小于等于
查看>>
java 受检异常和非受检异常
查看>>