前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第十二节:Springboot多环境配置

第十二节:Springboot多环境配置

作者头像
入门笔记
发布2022-06-02 19:04:57
2440
发布2022-06-02 19:04:57
举报
文章被收录于专栏:入门小站入门小站

系列文章

第一节:创建SpringBoot项目并运行HelloWorld

第二节:SpingBoot单元测试

第三节:SpringBoot中web项目推荐目录结构

第四节:SpringBoot中web模版数据渲染展示

第五节:SpringBoot常用注解介绍

第六节:SpingBoot基本配置一

第七节:SpringBoot高级属性配置二

第八节:SpringBoot指定配置文件配置三

第九节:SpringBoot在线文档Swagger2入门

第十节:SpringBoot中的日志管理

第十一节:Springboot整合log4j2日志

开发的阶段会需要设定基本的属性或是自定义的属性,而且通常会被应用和安装到几个不同的环境上,比如:开发(dev)、测试(test)、生产(prod)等,其中对应的每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误。

通常有下面两种配置方式

1.maven的多环境配置

在没有使用过Spring Boot的多环境配置时,是用maven的profile功能进行多环境配置。

maven配置pom.xml

代码语言:javascript
复制
<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>

application.properties的配置

代码语言:javascript
复制
server.port=${pom.port}

编译打包项目

代码语言:javascript
复制
> mvn clean install -DskipTests -Ptest

-Ptest表示编译为测试环境,对应<profile>下的<id>test</id>

2.SpringBooot的多环境配置

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识。

代码语言:javascript
复制
application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境

而决定使用哪种环境的的配置文件,需要在application.properties中通过spring.profiles.active属性来设定,其值对应{profile}值。

application.properties的配置

代码语言:javascript
复制
# 指定dev開發環境
spring.profiles.active=dev

spring.profiles.active=dev 就会载入application-dev.properties 配置文件内容

测试

三个配置文件application-dev.propertiesapplication-test.propertiesapplication-prod.properties

三个文件的 server.port属性不一样,

  • application-dev.properties server.port=8081
  • application-test.properties server.port=8082
  • application-prod.properties server.port=8080

运行测试

代码语言:javascript
复制
# 运行端口被改成8081
> java -jar rumenz.jar --spring.profiles.active=dev  

# 运行端口被改成8082
> java -jar rumenz.jar --spring.profiles.active=test


# 运行端口被改成8080
> java -jar rumenz.jar --spring.profiles.active=prod

本小结源码地址:

  • GitHub:https://github.com/mifunc/springboot/tree/main/lession12
  • Gitee:https://gitee.com/rumenz/springboot/tree/master/lession12
  • https://rumenz.com/rumenbiji/springboot-multiple-env.html

介绍

  • 我的博客 https://rumenz.com/
  • 我的工具箱 https://tooltt.com/
  • 微信公众号:【入门小站】
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-02-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 入门小站 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.maven的多环境配置
  • 2.SpringBooot的多环境配置
  • 测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档