背景
在日常開發過程中,我們都會有多套開發環境,比如:開發、測試、生產等不同的應用環境,這些應用環境都對應不同的配置項,包括不同環境數據庫地址、端口號等都是不盡相同的,要是沒有多環境的自由切換,部署起來是很繁瑣也容易出錯的。本文主要介紹在 SpringBoot 項目中如何進行多環境配置的方法。
採用Maven環境切換方式
在 Maven項目中,我們有一種簡潔的多環境配置方式,Maven 的思路是資源文件根據環境進行隔離,在測試的時候去加載正確的配置資源進行配置,另外 Maven 的多環境資源隔離配置與 Jenkins CI 集成較好。
pom.xml配置
先配置 pom.xml
文件的 build 節點。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<!--掃描替換參數的文件路徑-->
</resource>
</resources>
<filters>
<filter>src/main/filters/filter-${env}.properties</filter>
<!--環境過濾器的配置方式,回頭需要在該路徑下建立對應文件-->
</filters>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<!-- 需要加入,因爲maven默認的是${},而springbooot 默認會把此替換成@{} -->
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
在 pom.xml
文件配置 properties
<!-- 主要依賴庫的版本定義,可以採用${屬性名}引用 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- plugin 定義 -->
<spring-boot-maven-plugin.version>2.1.6.RELEASE</spring-boot-maven-plugin.version>
</properties>
以上的配置主要做的事情就是標記資源文件,把 src/main/filters/filter-${env}.properties
也標記爲了資源文件,{env} 的具體值見下面的配置
在 pom.xml
文件配置 Properties 環境,多環境配置參數切換
<!-- 不同的測試環境 -->
<profiles>
<!-- 開發環境,默認激活 -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault><!--默認啓用環境配置-->
</activation>
</profile>
<!-- 測試環境 -->
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<!-- 線上環境 -->
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
</profiles>
配置文件
在 src/main/filters
下創建配置文件:
- filter-dev.properties:開發環境
- filter-product.properties:線上環境
- filter-test.properties:測試環境
用於環境信息記錄,如:
#Environment
Environment=dev
host=http://127.0.0.1
port=8082
jdbc-url=xxxx
jdbc-username=xxxx
jdbc-password=xxxx
在 src/resource
下創建 application-maven.properties
文件。
該文件記錄的信息是跟環境切換相關的參數,裏面可以使用 key=value
的形式配置變量。
如:接口請求不同環境的host、數據庫等,因不同環境的信息。
server.port=${port}
# Environment
Environment=${Environment}
Host.url=${host}
# 數據源配置
spring.datasource.url=${jdbc-url}
spring.datasource.username==${jdbc-usernamel}
spring.datasource.password==${jdbc-password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
如圖:
然後編譯時,maven 命令加入參數 -P
命令即可指定相應的環境資源,比如: -Ptest
,則會替換 test 環境下的參數值。
mvn clean install -DskipTests -Ptest
Jenkins 配置
在 Jenkins 使用 Maven 構建項目測試前,先通過本地使用 maven 測試是否通過。
這裏本來要將參數化構建,但參數化構建前先說明下是如何利用 maven 構建測試的。
同樣,env 對應 maven 構建中的 -P%env%
,再對應 pom.xml
中的build信息,加入運行的環境選項
如下:
clean test -U -DxmlFileName=%xmlFileName% -P%env%
springboot多環境配置
Profile 是 Spring 針對不同環境不同配置的支持。需要滿足 application-{profile}.properties
,{profile} 對應你的環境標識。
如:
- application-dev.properties:開發環境
- application-test.properties:測試環境
- application-product.properties:線上環境
在不同環境的配置文件中使用 key=value
的形式配置變量。
server.port=8081
而指定執行哪份配置文件,只需要在 application.properties
配置 spring.profiles.active
爲對應 ${profile} 的值。
# 指定環境爲dev
spring.profiles.active=dev
則會加載 application-dev.properties
的配置內容
小結
一般我們在做自動化測試集成執行的時候,推薦Maven環境切換方式,因爲可以做到動態環境切換, 而 springboot多環境配置在使用IDE開發的時候使用比較方便。
本文源碼:
https://github.com/zuozewei/Java-API-Test-Examples/tree/master/springboot-mulienvironment-demo