前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot(12)-profile多环境配置

springboot(12)-profile多环境配置

作者头像
叔牙
发布2020-11-19 15:15:56
1.2K0
发布2020-11-19 15:15:56
举报
文章被收录于专栏:一个执拗的后端搬砖工

在我们日常开发工作中,经常会遇到应用程序在不同的环境可能会有不同的配置,比如数据库连接、日志级别等,日常开发、测试和生产环境可能每个环境的配置都不一致。

使用springboot的Profile可以实现多环境下的配置切换,方便日常开发,测试环境验收和生产环境的部署。springboot的通过profile实现多环境配置的切换有常用的几种实现方式,修改application.properties(yml)、命令行方式、maven profile方式、@Profile注解方式等等,此篇我们将对以上几种常用的修改启动端口的方式做分析和代码实现。

修改主配置文件

springboot中多环境配置文件名需要满足application-{profile}.properties格式,其中{profile}对应你的环境标识,比如:

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境

我们通过不同的环境应用的启动端口不同,来分别测试多环境切换的功能。

1:添加profile配置文件

application-dev.properties:

server.port=9090

application-test.properties:

server.port=9091

application-prod.properties:

server.port=9092

2:添加主配置文件

application.properties配置文件中添加spring.profiles.active属性,指向不同的环境配置:

spring.profiles.active=dev

3:测试验证

测试入口IndexController:

@RestController public class IndexController { @GetMapping("/hello") public String hello() { return "hello,world!"; } }

运行启动类:

应用已经在9090端口启动成功,浏览器输入http://loalhost:9090/hello:

请求能够被正常的接收和处理,应用启动profile切换到dev环境成功。

修改application.properties:

spring.profiles.active=test

启动应用:

应用已经在9091端口启动成功,浏览器输入http://loalhost:9091/hello:

请求能够被正常的接收和处理,应用启动profile切换到test环境成功。

修改application.properties:

spring.profiles.active=prod

启动应用:

应用已经在9092端口启动成功,浏览器输入http://loalhost:9092/hello:

请求能够被正常的接收和处理,应用启动profile切换到prod环境成功。

命令行指定profile

应用打包之后,使用jar命令启动的时候可以指定环境。

1:应用打包

进入应用目录运行命令:

mvn -U clean package -Dmaven.test.skip=true

target下生成jar文件:

2:测试验证

进入到应用的target目录执行命令:

java -jar demo15-profile-1.0-SNAPSHOT.jar --spring.profiles.active=dev

应用已经在9090端口启动成功,配置已经切换到dev环境。

运行命令:

java -jar demo15-profile-1.0-SNAPSHOT.jar --spring.profiles.active=test

应用已经在9091端口启动成功,配置已经切换到test环境。

运行命令:

java -jar demo15-profile-1.0-SNAPSHOT.jar --spring.profiles.active=prod

应用已经在9092端口启动成功,配置已经切换到prod环境。

maven profile

Maven同样也有Profile设置,可在构建过程中针对不同的Profile环境执行不同的操作,包含配置、依赖、行为等。Maven的Profile由 pom.xml 的<Profiles>标签管理。每个Profile中可设置:id(唯一标识), properties(配置属性), activation(自动触发的逻辑条件), dependencies(依赖)等。

1:修改application.properties

配置文件application.properties中使用占位符填充属性:

spring.profiles.active=@profiles.active@ server.port=@profiles.port@

2:修改pom文件

应用pom文件中添加profiles配置:

<profiles> <profile> <!-- 本地开发环境 --> <id>dev</id> <properties> <profiles.active>dev</profiles.active> <profiles.port>9090</profiles.port> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 测试环境 --> <id>test</id> <properties> <profiles.active>test</profiles.active> <profiles.port>9091</profiles.port> </properties> </profile> <profile> <!-- 生产环境 --> <id>prod</id> <properties> <profiles.active>prod</profiles.active> <profiles.port>9092</profiles.port> </properties> </profile> </profiles>

<profiles.active>和<profiles.port>分别对应application.properties中的profiles.active和profiles.port属性,执行命令的时候会动态替换。

在应用目录下执行命令:

mvn clean package -Dmaven.test.skip=true -P dev -e

解压jar文件:

查看application.properties文件内容,占位符已经被替换:

进入target目录执行jar命令启动应用:

应用已经在9090端口启动成功,配置已经切换到dev环境。

运行命令:

mvn clean package -Dmaven.test.skip=true -P test -e

解压jar文件,查看application.properties文件内容,占位符已经被替换:

进入target目录执行jar命令启动应用:

应用已经在9091端口启动成功,配置已经切换到test环境。

运行命令:

mvn clean package -Dmaven.test.skip=true -P prod -e

解压jar文件,查看application.properties文件内容,占位符已经被替换:

进入target目录执行jar命令启动应用:

应用已经在9092端口启动成功,配置已经切换到prod环境。

@Profile注解

@Profile注解是spring提供的一个用来标明当前运行环境的注解。在spring使用DI来依赖注入的时候,能够根据当前制定的运行环境来注入相应的bean。

1:创建配置类

开发环境DevServerPortCustomizer:

@Component @Profile("dev") public class DevServerPortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> { @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(9090); } }

测试环境TestServerPortCustomizer:

@Component @Profile("test") public class TestServerPortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> { @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(9091); } }

生产环境ProdServerPortCustomizer:

@Component @Profile("prod") public class ProdServerPortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> { @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(9092); } }

2:修改application.properties

修改配置文件制定当前环境:

spring.profiles.active=dev/test/prod

3:测试验证

运行启动类:

应用已经在9090端口启动成功,配置已经切换到dev环境。

修改application.properties:

spring.profiles.active=test

启动应用:

应用已经在9091端口启动成功,配置已经切换到test环境。

修改application.properties:

spring.profiles.active=prod

启动应用:

应用已经在9092端口启动成功,配置已经切换到prod环境。

总结

经过上述一系列赘述,我们实现了几种方式的多环境配置切换,具体使用哪一种方式,除了简单易用之外,要视具体开发工作中的场景而定,希望能够给各位带来一定的参考价值和帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-10-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 PersistentCoder 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档