教學課程:使用 Java Spring 應用程式中的動態組態

應用程式組態有兩個適用於 Spring 的程式庫。

  • spring-cloud-azure-appconfiguration-config 需要 Spring Boot,並相依於 spring-cloud-context
  • spring-cloud-azure-appconfiguration-config-web 需要 Spring Web 以及 Spring Boot,也新增了自動檢查設定重新整理的支援。

這兩個程式庫都支援手動觸發,檢查重新整理的組態值。

重新整理可讓您更新組態值,而不需要重新啟動應用程式,但會因此重新建立 @RefreshScope 中所有的 Bean。 這會檢查所設定觸發程序的任何變更,包括中繼資料。 根據預設,檢查變更、重新整理間隔之間的最小時間量會設定為 30 秒。

spring-cloud-azure-appconfiguration-config-web 的自動重新整理會根據活動觸發,尤其是 Spring Web 的 ServletRequestHandledEvent。 如果 ServletRequestHandledEvent 未觸發,即使快取到期時間已過期,spring-cloud-azure-appconfiguration-config-web 的自動重新整理也不會觸發重新整理。

使用手動重新整理

若要使用手動重新整理,請從使用應用程式組態的 Spring Boot 應用程式開始;例如遵循應用程式組態的 Spring Boot 快速入門所建立的應用程式。

應用程式組態公開 AppConfigurationRefresh,這可用來檢查快取是否已過期,以及是否觸發重新整理。

  1. 更新 HelloController 以使用 AppConfigurationRefresh

    import com.azure.spring.cloud.config.AppConfigurationRefresh;
    
    @RestController
    public class HelloController {
        private final MessageProperties properties;
    
        @Autowired(required = false)
        private AppConfigurationRefresh refresh;
    
        public HelloController(MessageProperties properties) {
            this.properties = properties;
        }
    
        @GetMapping
        public String getMessage() throws InterruptedException, ExecutionException {
            if (refresh != null) {
                refresh.refreshConfigurations();
            }
            return "Message: " + properties.getMessage();
        }
    }
    

    AppConfigurationRefreshrefreshConfigurations()如果已觸發重新整理,則傳回 true 的 Mono,如果不是,則傳回 false。 False 表示快取到期時間尚未過期,且沒有變更,或目前另一個執行緒正在針對重新整理進行檢查。

  2. 更新 bootstrap.properties 來啟用重新整理

    spring.cloud.azure.appconfiguration.stores[0].monitoring.enabled=true
    spring.cloud.azure.appconfiguration.stores[0].monitoring.refresh-interval= 30s
    spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key=sentinel
    
  3. 開啟 Azure 入口網站,並瀏覽至應用程式相關的應用程式組態資源。 選取 [作業] 下的 [組態總管],然後選取[+ Create]> [Key-value ] 並新增下列參數,以建立新的機碼值組:

    機碼
    Sentinel 1

    目前先讓 [標籤] 和 [內容類型] 保持空白。

  4. 選取套用

  5. 使用 Maven 建置 Spring Boot 應用程式並加以執行。

    mvn clean package
    mvn spring-boot:run
    
  6. 開啟瀏覽器視窗並移至 URL:http://localhost:8080。 您會看到與金鑰相關聯的訊息。

    您也可以使用 curl 來測試您的應用程式,例如:

    curl -X GET http://localhost:8080/
    
  7. 若要測試動態設定,請開啟與應用程式相關聯的 Azure 應用程式組態入口網站。 選取 [組態總管],然後更新所顯示金鑰的值,例如:

    機碼
    /application/config.message Hello - 已更新
  8. 將您稍早建立的 Sentinel 金鑰更新為新的值。 經過重新整理間隔之後,這項變更會觸發應用程式重新整理所有組態金鑰。

    機碼
    Sentinel 2
  9. 重新整理瀏覽器頁面兩次,以查看所顯示的新訊息。 第一次會觸發重新整理,第二次會載入變更。

注意

程式庫只會在重新整理間隔經過之後檢查是否有變更,如果期間尚未經過,則不會看到任何變更,您必須等候期間經過,然後觸發重新整理檢查。

使用自動重新整理

若要使用自動重新整理,請從使用應用程式組態的 Spring Boot 應用程式開始;例如遵循應用程式組態的 Spring Boot 快速入門所建立的應用程式。

接著,在文字編輯器中開啟 pom.xml 檔案,並使用下列程式碼為 spring-cloud-azure-appconfiguration-config-web 新增一個 <dependency>

Spring Boot

<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-appconfiguration-config-web</artifactId>
    <version>5.8.0</version>
</dependency>
  1. 更新 bootstrap.properties 來啟用重新整理

    spring.cloud.azure.appconfiguration.stores[0].monitoring.enabled=true
    spring.cloud.azure.appconfiguration.stores[0].monitoring.refresh-interval= 30s
    spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key=sentinel
    
  2. 開啟 Azure 入口網站,並瀏覽至應用程式相關的應用程式組態資源。 選取 [作業] 下的 [組態總管],然後選取[+ Create]> [Key-value ] 並新增下列參數,以建立新的機碼值組:

    機碼
    Sentinel 1

    目前先讓 [標籤] 和 [內容類型] 保持空白。

  3. 選取套用

  4. 使用 Maven 建置 Spring Boot 應用程式並加以執行。

    mvn clean package
    mvn spring-boot:run
    
  5. 開啟瀏覽器視窗並移至 URL:http://localhost:8080。 您會看到與金鑰相關聯的訊息。

    您也可以使用 curl 來測試您的應用程式,例如:

    curl -X GET http://localhost:8080/
    
  6. 若要測試動態設定,請開啟與應用程式相關聯的 Azure 應用程式組態入口網站。 選取 [組態總管],然後更新所顯示金鑰的值,例如:

    機碼
    /application/config.message Hello - 已更新
  7. 將您稍早建立的 Sentinel 金鑰更新為新的值。 經過重新整理間隔之後,這項變更會觸發應用程式重新整理所有組態金鑰。

    機碼
    Sentinel 2
  8. 重新整理瀏覽器頁面兩次,以查看所顯示的新訊息。 第一次觸發會重新整理,第二次會載入變更,因為第一個要求會使用原始範圍傳回。

注意

程式庫只會在重新整理間隔經過之後檢查變更。 如果重新整理間隔尚未經過,則不會檢查變更,您必須等候間隔經過,然後觸發重新整理檢查。

下一步

在本教學課程中,您已啟用 Spring Boot 應用程式,以動態方式從應用程式組態重新整理組態設定。 若有進一步的問題,請參閱參考文件,其中包含 Spring Cloud Azure 應用程式組態程式庫運作方式的所有詳細資料。 若要了解如何使用 Azure 受控服務識別來簡化對應用程式組態的存取,請繼續進行下一個教學課程。