Spring Boot教程 - Spring Boot Profiles實現多環境下配置切換

來源:互聯網
上載者:User

在後端開發中,應用程式在不同的環境可能會有不同的配置,例如資料庫連接、記錄層級等,開發,測試,生產每個環境可能配置都不一致。

使用Spring Boot的Profile可以實現多情境下的配置切換,方便開發中進行測試和部署生產環境。 下面就大致介紹一下yml設定檔跟properties設定檔怎麼使用profile配置不同環境的設定檔。 開發環境 JDK 1.8 Maven 3.x Spring Boot 1.5.8 Intellij Idea 2017 一、使用Spring Boot Profiles 1. 使用yml檔案

首先,我們先建立一個名為 application.yml的屬性檔案,如下:

server:  port: 8080my:  name: demospring:  profiles:    active: dev---#development environmentspring:  profiles: devserver:  port: 8160my:  name: ricky---#test environmentspring:  profiles: testserver:  port: 8180my:  name: test---#production environmentspring:  profiles: prodserver:  port: 8190my:  name: prod

application.yml檔案分為四部分,使用 --- 來作為分隔字元,第一部分通用配置部分,表示三個環境都通用的屬性, 後面三段分別為:開發,測試,生產,用spring.profiles指定了一個值(開發為dev,測試為test,生產為prod),這個值表示該段配置應該用在哪個profile裡面。

如果我們是本地啟動,在通用配置裡面可以設定調用哪個環境的profil,也就是第一段的spring.profiles.active=XXX, 其中XXX是後面3段中spring.profiles對應的value,通過這個就可以控制本地啟動調用哪個環境的設定檔,例如:

spring:    profiles:        active: dev

表示預設 載入的就是開發環境的配置,如果dev換成test,則會載入測試環境的屬性,以此類推。

注意:如果spring.profiles.active沒有指定值,那麼只會使用沒有指定spring.profiles檔案的值,也就是只會載入通用的配置。 啟動參數

如果是部署到伺服器的話,我們正常打成jar包,啟動時通過 --spring.profiles.active=xxx 來控制載入哪個環境的配置,完整命令如下:

java -jar xxx.jar --spring.profiles.active=test 表示使用測試環境的配置java -jar xxx.jar --spring.profiles.active=prod 表示使用生產環境的配置
使用多個yml設定檔進行配置屬性檔案

我們也可以使用多個yml來配置屬性,將於環境無關的屬性放置到application.yml檔案裡面;通過與設定檔相同的命名規範,建立application-{profile}.yml檔案 存放不同環境特有的配置,例如 application-test.yml 存放測試環境特有的配置屬性,application-prod.yml 存放生產環境特有的配置屬性。

通過這種形式來配置多個環境的屬性檔案,在application.yml檔案裡面spring.profiles.active=xxx來指定載入不同環境的配置,如果不指定,則預設只使用application.yml屬性檔案,不會載入其他的profiles的配置。 2. 使用properties檔案

如果使用application.properties進行多個環境的配置,原理跟使用多個yml設定檔一致,建立application-{profile}.properties檔案 存放不同環境特有的配置,將於環境無關的屬性放置到application.properties檔案裡面,並在application.properties檔案中通過spring.profiles.active=xxx 指定載入不同環境的配置。如果不指定,則預設載入application.properties的配置,不會載入帶有profile的配置。 二、Maven Profile

如果我們使用的是構建工具是Maven,也可以通過Maven的profile特性來實現多環境配置打包。

pom.xml配置如下:

<profiles>        <!--開發環境-->        <profile>            <id>dev</id>            <properties>                <build.profile.id>dev</build.profile.id>            </properties>            <activation>                <activeByDefault>true</activeByDefault>            </activation>        </profile>        <!--測試環境-->        <profile>            <id>test</id>            <properties>                <build.profile.id>test</build.profile.id>            </properties>        </profile>        <!--生產環境-->        <profile>            <id>prod</id>            <properties>                <build.profile.id>prod</build.profile.id>            </properties>        </profile>    </profiles>    <build>        <finalName>${project.artifactId}</finalName>        <resources>            <resource>                <directory>src/main/resources</directory>                <filtering>false</filtering>            </resource>            <resource>                <directory>src/main/resources.${build.profile.id}</directory>                <filtering>false</filtering>            </resource>        </resources>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <configuration>                    <classifier>exec</classifier>                </configuration>            </plugin>        </plugins>    </build>

通過執行 mvn clean package -P ${profile} 來指定使用哪個profile。 參考資料

Spring Boot Reference Guide - Profiles

Maven profiles

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.