Python 官方文档:入门教程 => 点击学习
目录Profile多环境配置Profile配置详解1.问题2.为什么要使用profilesProfile多环境配置 我们在开发项目时,通常同一套程序会被发布到几个不同的环境,比如:开
我们在开发项目时,通常同一套程序会被发布到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、Redis地 址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。
对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,而Spring Boot 就更简单了,打好包后,只需在不同的环境下启动时候指定读取的配置文件就可以了。
在spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile} 值。 如:spring.profiles.active=test就会加载application-test.properties配置文件内容。
① application.properties:
# 在application.properties文件中通过spring.profiles.active属性来设置哪个配置文件会被加载
spring.profiles.active=dev
② application-dev.properties:
server.port=8081branch=dev
③ application-prod.properties:
server.port=8083branch=prod
④ application-test.properties:
server.port=8082branch=test
⑤ 测试类:
@Controller
@RequestMapping("/test")
public class TestController {
@Value("${branch}")
private String branch;
@ResponseBody
@GetMapping("/branch")
public String test(){
return branch;
}
}
⑥ 如果我们直接启动项目,那么启动的就是dev环境,因为我们在application.properties中配置的是dev
⑦ 打包项目(clean,install),打好包后,启动时指定读取的配置文件
java -jar SpringBoot-profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
开发环境和真实的项目环境是不样的
在开发中,一般有两种环境
1,生产环境 [项目上线,客户在使用中,就是生产环境]
2,开发环境[就是开发环境,不解释]
3,创建application-dev.properties
server.port=8888
4,创建application-pro.properties
server.port=9999
5,配置application.properties
# spring.profiles.active=pro
6,配置application.yml[和第5点任选一个]
#配置激活哪一个配置文件
spring:
profiles:
active:
- dev
7,删除application.yml/.properties文件如何运行
-Dspring.profiles.active=pro
8,去掉application.properties的jar包运行方式
9,yml文件的多配置块的集成切换
#主配置,启动哪个配置文件
spring:
profiles:
active:
- dev
#插入开发配置文件
---
spring:
profiles: dev
server:
port: 8888
#插入生产的配置文件
---
spring:
profiles: pro
server:
port: 9999
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: SpringBoot Profile多环境配置方式
本文链接: https://www.lsjlt.com/news/152121.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0