共用方式為


快速入門: 使用 Azure 應用程式組態建立 Java Spring 應用程式

在本快速入門中,您會將 Azure 應用程式組態納入 Java Spring 應用程式中,以集中儲存和管理應用程式設定 (與您的程式碼分開)。

先決條件

  • 具有有效訂用帳戶的 Azure 帳戶。 建立免費帳戶
  • 應用程式組態存放區,如 建立存放區的教學課程所示。
  • 支援的 Java 開發套件 (JDK) 第 11 版。
  • Apache Maven 3.0 版或更新版本。
  • Spring Boot 應用程式。 如果您沒有這個應用程式,請使用 Spring Initializr 來建立 Maven 專案。 請務必選取 [Maven專案],然後在 [相依性] 下新增 Spring Web 相依性,然後選取 Java 版本 8 或更高版本。

新增金鑰值

將下列索引鍵/值新增至應用程式組態存放區,並保留標籤內容類型的預設值。 如需如何使用 Azure 入口網站或 CLI 將索引鍵-值新增至存放區的詳細資訊,請移至建立索引鍵-值

Key
/application/config.message 您好

連線至應用程式組態存放區

現在您已擁有應用程式組態存放區,可以使用 Spring Cloud Azure Config 入門版,讓應用程式與您建立的應用程式組態存放區進行通訊。

若要安裝 Spring Cloud Azure Config 入門版模組,請將下列相依性新增至您的 pom.xml 檔案:

<dependencies>
    ...
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-appconfiguration-config-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-dependencies</artifactId>
        <version>6.0.0</version>
        <type>pom</type>
        <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  1. 根據預設,程式庫會透過受控身分識別連接到應用程式組態存放區。 請遵循指示,將 [應用程式組態資料讀取者] 角色指派給您的認證。 在執行應用程式之前,請確保留出足夠的時間來散佈權限。 然後建立名為 AppConfigCredential.java 的新檔案,並新增下列幾行:

    import org.springframework.stereotype.Component;
    
    import com.azure.data.appconfiguration.ConfigurationClientBuilder;
    import com.azure.identity.DefaultAzureCredentialBuilder;
    import com.azure.spring.cloud.appconfiguration.config.ConfigurationClientCustomizer;
    
    @Component
    public class AppConfigCredential implements ConfigurationClientCustomizer {
    
        @Override
        public void customize(ConfigurationClientBuilder builder, String endpoint) {
            builder.credential(new DefaultAzureCredentialBuilder().build());
        }
    }
    

    備註

    此外,您可以使用 Spring Cloud Azure 驗證 來提供驗證資訊。 使用 Azure Spring 設定進行驗證時,它會針對所有 Azure Spring 連結庫使用相同的驗證。

  2. 然後建立組態 Bootstrap 組態,方法是在 spring.factories 目錄之下建立 resources/META-INF 檔案並新增下列幾行,然後使用您的應用程式名稱和套件來更新 com.example.MyApplication

    org.springframework.cloud.bootstrap.BootstrapConfiguration=\
    com.example.MyApplication
    
  3. 在應用程式的 resources 目錄下建立名為 application.properties 的新檔案,並將下列行新增至檔案。

    spring.config.import=azureAppConfiguration
    spring.cloud.azure.appconfiguration.stores[0].endpoint= ${APP_CONFIGURATION_ENDPOINT}
    

從應用程式組態存放區讀取

若要使用 Spring Cloud Azure Config 入門版,讓應用程式與您建立的應用程式組態存放區進行通訊,請使用下列步驟來設定應用程式。

  1. 建立名為 MyProperties.java 的新 Java 檔案,並新增下列幾行:

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "config")
    public class MyProperties {
        private String message;
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
  2. 建立名為 HelloController.java 的新 Java 檔案,並新增下列幾行:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @Autowired
        private MyProperties properties; 
    
        @GetMapping
        public String getMessage() {
            return "Message: " + properties.getMessage();
        }
    }
    
  3. 開啟自動產生的單元測試,並更新以停用 Azure 應用程式組態,或在執行單元測試時嘗試從服務載入。

    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest(properties = "spring.cloud.azure.appconfiguration.enabled=false")
    class DemoApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
    }
    

於本機建置並執行應用程式

  1. 設定一個名為 APP_CONFIGURATION_ENDPOINT 的環境變數,並將其設定為應用程式組態存放區的存取金鑰。 在命令列執行下列命令,然後重新啟動命令提示字元,讓變更生效:

    setx APP_CONFIGURATION_ENDPOINT "<endpoint-of-your-app-configuration-store>"
    

    如果您使用 Windows PowerShell,請執行下列命令:

    $Env:APP_CONFIGURATION_ENDPOINT = "<endpoint-of-your-app-configuration-store>"
    

    如果您使用 macOS 或 Linux,請執行下列命令:

    export APP_CONFIGURATION_ENDPOINT='<endpoint-of-your-app-configuration-store>'
    
  2. 將命令提示字元開啟至根目錄,然後執行下列命令,以使用 Maven 組建 Spring Boot 應用程式並予以執行。

    mvn clean package
    mvn spring-boot:run
    
  3. 在您的應用程式執行之後,使用 curl 來測試您的應用程式;例如:

    curl -X GET http://localhost:8080/
    

    您會看到您在應用程式組態存放區中輸入的訊息。

清除資源

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

重要事項

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

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

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

後續步驟

在本快速入門中,您已建立新的應用程式組態存放區,並將其與 Java Spring 應用程式搭配使用。 如需詳細資訊,請參閱 Azure 上的 Spring。 若有進一步的問題,請參閱參考文件,其中包含 Spring Cloud Azure 應用程式組態程式庫運作方式的所有詳細資料。 若要了解如何讓 Java Spring 應用程式以動態方式重新整理組態設定,請繼續進行下一個教學課程。