SpringBoot如何实现针对不同环境的配置管理?
- 内容介绍
- 文章标签
- 相关推荐
本文共计689个文字,预计阅读时间需要3分钟。
多种环境配置+开发应用时,常用部署的应用有多个,如:开发、测试、联调、生产等不同应用环境,这些环境对应不同的配置项,例如swagger在生产环境下通常是关闭的。
多环境配置
在开发应用时,常用部署的应用是多个的,比如:开发、测试、联调、生产等不同的应用环境,这些应用环境都对应不同的配置项,比如swagger一般上在生产时是关闭的;不同环境数据库地址、端口号等都是不尽相同的,要是没有多环境的自由切换,部署起来是很繁琐也容易出错的。
maven的多环境配置
在没有使用过springboot的多环境配置时,原先是利用maven的profile功能进行多环境配置,这里我简单回顾下。
maven配置
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <pom.port>8080</pom.port> </properties> </profile> <profile> <id>test</id> <properties> <pom.port>8888</pom.port> </properties> </profile> </profiles> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> <resource> <directory>${project.basedir}/src/main/resources</directory> <includes> <include>**/*.properties</include> </includes> <!-- 加入此属性,才会进行过滤 --> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <!-- 需要加入,因为maven默认的是${},而springbooot 默认会把此替换成@{} --> <useDefaultDelimiters>true</useDefaultDelimiters> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>然后编译时,加入-Ptest,则会替换test环境下的参数值。 完整参数:
mvn clean install -DskipTests -Ptestapplication.properties
server.port=${pom.port}利用maven实现多环境配置,比较麻烦的就是每次部署新环境时,都需要再次指定环境编译打包一次。一下进入主题,springboot的多环境,比较优雅了许多。
springboot多环境配置
Profile是Spring针对不同环境不同配置的支持。需要满足application-{profile}.properties,{profile}对应你的环境标识。如:
- application-dev.properties:开发环境
- application-test.properties:测试环境
而指定执行哪份配置文件,只需要在application.properties配置spring.profiles.active为对应${profile}的值。
# 指定环境为dev spring.profiles.active=dev则会加载:application-dev.properties的配置内容。
2018-07-15 14:52:41.304 INFO 15496 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (blog.lqdev.cn/2018/07/15/springboot/chapter-five/本文共计689个文字,预计阅读时间需要3分钟。
多种环境配置+开发应用时,常用部署的应用有多个,如:开发、测试、联调、生产等不同应用环境,这些环境对应不同的配置项,例如swagger在生产环境下通常是关闭的。
多环境配置
在开发应用时,常用部署的应用是多个的,比如:开发、测试、联调、生产等不同的应用环境,这些应用环境都对应不同的配置项,比如swagger一般上在生产时是关闭的;不同环境数据库地址、端口号等都是不尽相同的,要是没有多环境的自由切换,部署起来是很繁琐也容易出错的。
maven的多环境配置
在没有使用过springboot的多环境配置时,原先是利用maven的profile功能进行多环境配置,这里我简单回顾下。
maven配置
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <pom.port>8080</pom.port> </properties> </profile> <profile> <id>test</id> <properties> <pom.port>8888</pom.port> </properties> </profile> </profiles> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> <resource> <directory>${project.basedir}/src/main/resources</directory> <includes> <include>**/*.properties</include> </includes> <!-- 加入此属性,才会进行过滤 --> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <!-- 需要加入,因为maven默认的是${},而springbooot 默认会把此替换成@{} --> <useDefaultDelimiters>true</useDefaultDelimiters> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>然后编译时,加入-Ptest,则会替换test环境下的参数值。 完整参数:
mvn clean install -DskipTests -Ptestapplication.properties
server.port=${pom.port}利用maven实现多环境配置,比较麻烦的就是每次部署新环境时,都需要再次指定环境编译打包一次。一下进入主题,springboot的多环境,比较优雅了许多。
springboot多环境配置
Profile是Spring针对不同环境不同配置的支持。需要满足application-{profile}.properties,{profile}对应你的环境标识。如:
- application-dev.properties:开发环境
- application-test.properties:测试环境
而指定执行哪份配置文件,只需要在application.properties配置spring.profiles.active为对应${profile}的值。
# 指定环境为dev spring.profiles.active=dev则会加载:application-dev.properties的配置内容。
2018-07-15 14:52:41.304 INFO 15496 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (blog.lqdev.cn/2018/07/15/springboot/chapter-five/
