1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > spring boot + maven使用profiles进行环境隔离

spring boot + maven使用profiles进行环境隔离

时间:2023-08-27 23:46:11

相关推荐

spring boot + maven使用profiles进行环境隔离

Spring Profile

Spring可使用Profile决定程序在不同环境下执行情况,包含配置、加载Bean、依赖等。

Spring的Profile一般项目包含:dev(开发), test(单元测试), qa(集成测试), prod(生产环境)。由spring.profiles.active属性决定启用的profile。

SpringBoot的配置文件默认为 application.properties(或yaml,此外仅以properties配置为说明)。不同Profile下的配置文件由application-{profile}.properties管理,同时独立的 Profile配置文件会覆盖默认文件下的属性。

Maven Profile

Maven同样也有Profile设置,可在构建过程中针对不同的Profile环境执行不同的操作,包含配置、依赖、行为等。

Maven的Profile由 pom.xml 的标签管理。每个Profile中可设置:id(唯一标识), properties(配置属性), activation(自动触发的逻辑条件), dependencies(依赖)等。

此文章不对Spring和Maven的Profile作过多说明,详细情况请自行查阅。

spring 多环境

maven增加profiles配置

<profiles><profile><id>dev</id><properties><!-- 环境标识,需要与配置文件的名称相对应 --><profile>deve</profile></properties><activation><!-- 默认环境 --><activeByDefault>true</activeByDefault></activation></profile><profile><id>test</id><properties><profile>test</profile></properties></profile><profile><id>prod</id><properties><profile>prod</profile></properties></profile></profiles>

application.properties

spring.profiles.active=@profile@

application-deve.properties

management.server.port=9001management.endpoints.web.base-path=/monitormanagement.endpoints.web.exposure.include=*management.endpoints.web.exposure.exclude=envmanagement.endpoint.health.show-details=alwaysmanagement.endpoint.beans.cache.time-to-live=10smanagement.endpoints.web.cors.allowed-origins=management.endpoints.web.cors.allowed-methods=GET,POST

application.properties中定义激活的环境@profile@与maven中定义的profile保持一致

打包

run --> maven build... --> profile选择对应的环境,如下图则是打的生成的包

本地运行

通过spring boot参数覆盖的方式执行激活环境

-11-16 14:52:28.211 INFO 2068 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9001 (http) with context path ''-11-16 14:52:28.223 INFO 2068 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''-11-16 14:52:28.224 INFO 2068 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.179 seconds (JVM running for 2.587)

热部署

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>true</scope><optional>true</optional></dependency><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork> <!-- 如果没有该配置,devtools不会生效 --></configuration></plugin></plugins></build>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。