實作Springboot的多環境配置方式有幾種
這裡我列出我所了解的兩種方式:
一、不需要打包(即使用mvn clean package),直接運作項目就以哪種配置檔案運作的方式
(1)建立三個配置檔案

三個配置檔案解釋:
注意:這裡為了簡單起見,三個檔案中我都隻是設定了不同的端口号
a)application.properties,是預設的配置檔案,如果在這裡面不配置spring.profiles.active這個屬性的話,項目預設會以這個配置檔案來運作。
b)application-dev.properties,這個是開發環境的配置檔案。
c)application-pro.properties,這個是生産環境的配置檔案。
貼一個預設的application.properties
現在重點來了
如果要以開發環境的配置為基礎來運作項目,則隻需要在application.properties中添加一句:
spring.profiles.active=dev
那麼運作項目的時候就是加載application-dev.properties檔案中的内容了,
如果application-dev.properties和application.properties有相同的屬性,那麼前者會覆寫後者的屬性。
這就實作了springboot的多環境配置的第一種方式,基于application.properties
二、需要打包(即使用mvn clean package)的這種方式,這種方式是基于maven來完成的多環境配置
同理也是建立三個配置檔案,和上面類似。
然後在pom.xml檔案中
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<groupId>com.dubbo.xf</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.dubbo.xf</groupId>
<artifactId>exa-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
<!-- 配置多環境的檔案 -->
<profiles>
<profile>
<!--預設環境,就是執行application.properties -->
<id>default</id>
<!-- 預設啟動這個配置檔案-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<active.profile></active.profile>
</properties>
</profile>
<profile>
<!--預設環境,就是執行application-dev.properties -->
<id>dev</id>
<properties>
<active.profile>dev</active.profile>
</properties>
</profile>
<profile>
<!--預設環境,就是執行application-pro.properties -->
<id>pro</id>
<properties>
<active.profile>pro</active.profile>
</properties>
</profile>
</profiles>
<build>
<!-- 配置資源檔案 -->
<resources>
<resource>
<!-- 是否使用真正的值替換占位符,比如配置檔案中有${key},然後用指向的值将其替換}-->
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/</directory>
<!-- 打包的時候最少包含一個檔案application.properties -->
<includes>
<include>application.properties</include>
<include>application-${active.profile}.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources/</directory>
<!-- 打包的時候不打包application*.properties這種檔案 -->
<excludes>
<exclude>application*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
</project>
上面pom.xml檔案中需要解釋的是(依賴可以不用看,沒有使用)
1.<profiles>這個标簽下面包含的就是多環境配置的東西,每一個對應一個配置檔案。
2.<resources>這個标簽對應的是打包的時候需要打包那些資源檔案。
這兩個是必須要配置的。
<resources>中有一點要搞清楚。
為什麼第一個<resource>中使用<includes>包含了兩個配置檔案要打包,而第二個<resource>中使用<excludes>又将所有類似于application*.properties的配置檔案不進行打包?
這是因為第一個<resource>中是指定要打包哪些配置檔案,如果不指定的話,那麼所有的配置檔案都會被打包下來,這很明顯是沒有必要的。
而第二個<resource>就是指明所有的配置檔案都不需要打包,如果要打包的話,就隻打包第一個<resource>中指定的配置檔案。
如上配置完成之後,然後在mvn終端輸入如下指令:
mvn clean package -P 指定配置檔案
比如mvn clean package -P dev
那麼在你的target目錄下就隻會看到兩個配置檔案application.properties和
application-deiv.properties,之後運作jar包就可以啟動項目了。
可以看到隻存在兩個配置檔案,若隻需要application.properties檔案,那麼在maven終端直接直接打包就可以,比如:mvn clean package,就隻會打包 application.properties這一個檔案。如圖