快速入門:將功能旗標新增至 Spring Boot 應用程式

在這個快速入門中,你將在 Azure 應用程式組態 建立一個功能標誌,並用它來動態控制 Spring Boot 應用程式,建立一個端對端的功能管理實作。

Spring Boot 功能管理庫不依賴任何 Azure 函式庫。 它們可透過 Spring Boot 組態提供者完美地與應用程式組態整合。

先決條件

新增功能旗標

將名為 Beta 的功能旗標新增至應用程式組態存放區,並保留標籤和描述的預設值。 如需如何使用 Azure 入口網站或 CLI 將功能旗標新增至存放區的詳細資訊,請移至建立功能旗標。 在此階段,應取消勾選 [啟用功能旗標] 核取方塊。

啟用名為 Beta 的功能旗標的螢幕擷取畫面。

建立主控台應用程式

  1. 建立新的 Spring Boot 專案:

    1. 瀏覽至 Spring Initializr。

    2. 指定下列選項:

      • 使用 Java 產生 Maven 專案。
      • 指定一個等於或大於 3.0 的 Spring Boot 版本。
      • 指定應用程式的群組和成品名稱。 本文使用 com.example 和 demo。
    3. 在指定先前的選項之後,選取 [產生專案]。 下載並解壓專案到你本地的電腦。

  2. 在你的應用程式根目錄中找到 pom.xml ,然後用文字編輯器打開。

  3. 將以下項目加入以下清單 <dependencies>:

    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-appconfiguration-config</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-feature-management</artifactId>
    </dependency>
    
  4. 新增以下 <dependencyManagement> 區塊以管理 Spring Cloud Azure 函式庫版本:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure.spring</groupId>
                <artifactId>spring-cloud-azure-dependencies</artifactId>
                <version>7.2.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
  5. 瀏覽至應用程式的 resources 目錄,然後開啟 application.properties 或 application.yaml 檔案。

    您可使用 DefaultAzureCredential 來向應用程式組態存放區進行驗證。 若要讓授權正常運作,您必須將 應用程式配置資料讀取者 角色授予應用程式使用的憑證。 相關說明請參見 「使用憑證憑證認證」。在執行應用程式前,務必預留足夠時間讓許可傳播。

    spring.config.import=azureAppConfiguration
    spring.cloud.azure.appconfiguration.stores[0].endpoint= ${AZURE_APPCONFIG_ENDPOINT}
    spring.cloud.azure.appconfiguration.stores[0].feature-flags.enabled=true
    
  6. 用以下程式碼更新 DemoApplication.java 你應用程式套件目錄中的檔案:

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    
    import com.azure.spring.cloud.feature.management.FeatureManager;
    
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @Bean
        public CommandLineRunner runner(FeatureManager featureManager) {
            return args -> {
                System.out.println("Beta is enabled: " + featureManager.isEnabled("Beta"));
            };
        }
    }
    
  7. 設定一個名為 AZURE_APPCONFIG_ENDPOINT 的環境變數,並設定為你的 App Configuration Store 的端點。 在命令列執行下列命令,然後重新啟動命令提示字元,讓變更生效:

    setx AZURE_APPCONFIG_ENDPOINT "<AppConfigurationEndpoint>"
    

    重新啟動命令提示字元,讓變更生效。 透過列印環境變數的值來驗證其是否設定正確。

  8. 用 Maven 編譯並執行你的 Spring Boot 應用程式。

    mvn clean package
    mvn spring-boot:run
    
  9. 在應用程式組態入口網站中,選取 [功能管理員],然後使用 [已啟用] 資料行中的切換開關,將 Beta 功能旗標的狀態變更為 [開啟]。

    Key State
    Beta 另一
  10. 重新啟動應用程式。 申請表將列印以下內容:

    Beta is enabled: true
    

清除資源

如果您不想繼續使用本文中建立的資源,請刪除在此處建立的資源群組,以避免產生費用。

重要事項

刪除資源群組是無法回復的動作。 資源群組和其中的所有資源都將被永久刪除。 請確定您不會誤刪錯誤的資源群組或資源。 如果您是在包含需保留其他資源的資源群組內部,建立本文的資源,則可以從每個資源各自的窗格中個別刪除每個資源,而不必刪除整個資源群組。

  1. 登入 Azure 入口網站,然後選取 [資源群組]。
  2. 在 [依名稱篩選] 方塊中,輸入您資源群組的名稱。
  3. 在結果清單中,選取資源群組名稱以查看概觀。
  4. 選取 [刪除資源群組]。
  5. 系統將會要求您確認是否刪除資源群組。 輸入您資源群組的名稱以進行確認,然後選取 [刪除]。

不久後,系統便會刪除該資源群組及其所有的資源。

後續步驟

在這個快速入門中,你建立了一個新的應用程式組態商店,並透過 功能管理函式庫管理 Spring Boot 應用程式的功能。