共用方式為


快速入門:將功能旗標新增至 Python 應用程式 (預覽)

在本快速入門中,您將在 Azure 應用程式組態 中建立功能旗標,並用它來動態控制 Python 應用程式,以建立功能管理的端對端實作。

功能管理支援會擴充應用程式設定中的動態設定功能。 快速入門中的這些範例是以動態設定教學課程中引進的Ppython 應用程式為基礎。 繼續之前,請先完成快速入門教學課程,以使用動態設定建立 Python 應用程式。

此連結庫不相依於任何 Azure 連結庫。 它們可透過其 Python 設定提供者與 應用程式組態 緊密整合。

必要條件

新增功能旗標

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

啟用名為 Beta 的功能旗標螢幕快照。

主控台應用程式

  1. 使用 pip install 命令安裝功能管理。

    pip install featuremanagement
    
  2. 建立名為 app.py 的新 Python 檔案,並新增下列程式代碼:

    from featuremanagement import FeatureManager
    from azure.identity import InteractiveBrowserCredential
    from azure.appconfiguration.provider import load
    import os
    from time import sleep
    
    endpoint = os.environ["APP_CONFIGURATION_ENDPOINT"]
    
    # Connecting to Azure App Configuration using an endpoint
    # credential is used to authenticate the client, the InteractiveBrowserCredential is used for this sample. It will open a browser window to authenticate the user. For all credential options see [credential classes](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credential-classes).
    # feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration
    # feature_flag_refresh_enabled makes it so that the provider will refresh feature flags from Azure App Configuration, when the refresh operation is triggered
    config = load(endpoint=endpoint, credential=InteractiveBrowserCredential(), feature_flag_enabled=True, feature_flag_refresh_enabled=True)
    
    feature_manager = FeatureManager(config)
    
    # Is always false
    print("Beta is ", feature_manager.is_enabled("Beta"))
    
    while not feature_manager.is_enabled("Beta"):
        sleep(5)
        config.refresh()
    
    print("Beta is ", feature_manager.is_enabled("Beta"))
    

啟動應用程式時,瀏覽器視窗會開啟以驗證使用者。 用戶必須至少有App Configuration Data Reader角色才能存取 應用程式組態 存放區,如需詳細資訊,請參閱 應用程式組態 角色

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

    若要使用 Windows 命令提示字元在本機建置並執行應用程式,請執行下列命令:

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

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

  2. 執行 Python 應用程式。

    python app.py
    
  3. 在 應用程式組態 入口網站中,選取 [功能管理員],然後使用 [已啟用] 數據行中的切換,將 Beta 功能旗標的狀態變更為 [開啟]。

    機碼 州/省
    Beta 另一
  4. 大約 30 秒之後,這是提供者的重新整理間隔,應用程式會列印下列內容:

    Beta is True
    

Web 應用程式

下列範例示範如何使用 Azure 應用程式組態 搭配動態重新整理來更新現有的 Web 應用程式,以使用功能旗標。 如需如何使用動態重新整理設定值的更詳細範例,請參閱 Python 動態 設定。 繼續之前,請確定您已在 應用程式組態 存放區中啟用 Beta 功能旗標。

在 中app.py,設定 Azure 應用程式組態的load方法以額外載入功能旗標,以及啟用功能旗標的重新整理。

from featuremanagement import FeatureManager

...

global azure_app_config, feature_manager
# Connecting to Azure App Configuration using an endpoint
# credential is used to authenticate the client, the InteractiveBrowserCredential is used for this sample. It will open a browser window to authenticate the user. For all credential options see [credential classes](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credential-classes).
# feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration
# feature_flag_refresh_enabled makes it so that the provider will refresh feature flags from Azure App Configuration, when the refresh operation is triggered
azure_app_config = load(endpoint=endpoint, credential=InteractiveBrowserCredential(),
                        refresh_on=[WatchKey("sentinel")],
                        on_refresh_success=on_refresh_success,
                        refresh_interval=10, # Default value is 30 seconds, shortened for this sample
                        feature_flag_enabled=True,
                        feature_flag_refresh_enabled=True,
                    )
feature_manager = FeatureManager(config)

同時更新您的路由,以檢查更新的功能旗標。

@app.route("/")
def index():
    ...
    context["message"] = azure_app_config.get("message")
    context["beta"] = feature_manager.is_enabled("Beta")
    ...

更新您的範本 index.html 以使用新功能旗標。

...

<body>
  <main>
    <div>
      <h1>{{message}}</h1>
      {% if beta %}
      <h2>Beta is enabled</h2>
      {% endif %}
    </div>
  </main>
</body>

更新並執行應用程式之後,您可以看到功能旗標作用中,訊息會出現在頁面上,Beta is enabled但前提是功能旗標已在 應用程式組態 存放區中啟用。

啟用功能旗標 Beta 已啟用的螢幕快照。

您可以在這裡找到完整的範例專案。

每當這些端點觸發時,就可以執行重新整理檢查,以確保使用最新的組態值。 如果重新整理間隔尚未通過,或重新整理正在進行中,則檢查可以立即傳回。

當重新整理完成時,所有值都會立即更新,因此組態一律在物件內保持一致。

清除資源

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

重要

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

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

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

下一步

在本快速入門中,您已建立新的 應用程式組態 存放區,並用它來透過功能管理連結庫來管理 Python 應用程式中的功能。