教學課程:在 Python 中使用動態設定

Azure 應用程式組態 Python 提供者包含內建快取和重新整理功能。 本教學課程示範如何在 Python 應用程式中啟用動態設定。

必要條件

新增索引鍵/值

將下列金鑰值新增至 Azure 應用程式組態存放區。 如需如何使用 Azure 入口網站或 CLI 將索引鍵/值新增至存放區的詳細資訊,請移至建立索引鍵/值

機碼 標籤 內容類型
message Hello World! 保留空白 保留空白
sentinel 1 保留空白 保留空白

注意

「Sentinel 金鑰」是您完成所有其他金鑰變更之後才更新的金鑰。 您的應用程式會監視 Sentinel 金鑰。 偵測到變更時,您的應用程式會重新整理所有設定值。 相較於監視所有金鑰是否變更,此方法有助於確保應用程式中設定的一致性,並減少對 Azure 應用程式組態存放區提出的整體要求數目。

主控台應用程式

  1. 建立名為 app.py 的新 Python 檔案,並新增下列程式碼:

    from azure.appconfiguration.provider import load, WatchKey
    import os
    import time
    
    connection_string = os.environ.get("APPCONFIGURATION_CONNECTION_STRING")
    
    # Connecting to Azure App Configuration using connection string
    # Setting up to refresh when the Sentinel key is changed.
    config = load(
        connection_string=connection_string,
        refresh_on=[WatchKey("sentinel")],
        refresh_interval=10, # Default value is 30 seconds, shorted for this sample
    )
    
    print("Update the `message` in your Azure App Configuration store using Azure portal or CLI.")
    print("First, update the `message` value, and then update the `sentinel` key value.")
    
    while (True):
        # Refreshing the configuration setting
        config.refresh()
    
        # Current value of message
        print(config["message"])
    
        # Waiting before the next refresh
        time.sleep(5)
    
  2. 執行您的指令碼:

    python app.py
    
  3. 驗證輸出:

    Update the `message` in your Azure App Configuration store using Azure portal or CLI.
    First, update the `message` value, and then update the `sentinel` key value.
    Hello World!
    
  4. 將下列索引鍵/值更新至 Azure 應用程式組態存放區。

    機碼 標籤 內容類型
    message Hello World 已重新整理! 保留空白 保留空白
    sentinel 2 保留空白 保留空白
  5. 更新值之後,更新的值就會在重新整理間隔經過後列印出來。

    Hello World Refreshed!
    

Web 應用程式

下列範例示範如何更新現有的 Web 應用程式,以使用可重新整理的組態值。 您可將回呼提供給 load 函式的 on_refresh_success 關鍵字引數。 在伺服器上偵測到組態變更時,將會叫用此回呼,並可用於更新應用程式中的組態值。

app.py 中,設定 Azure 應用程式組態以載入您的組態值。 然後更新您的端點,以檢查已更新的組態值。

from azure.appconfiguration.provider import load, WatchKey

azure_app_config = None  # declare azure_app_config as a global variable

def on_refresh_success():
   app.config.update(azure_app_config)


global azure_app_config
azure_app_config = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
                        refresh_on=[WatchKey("sentinel")],
                        on_refresh_success=on_refresh_success,
                        refresh_interval=10, # Default value is 30 seconds, shortened for this sample
                    )




@app.route("/")
def index():
    global azure_app_config
    # Refresh the configuration from Azure App Configuration service.
    azure_app_config.refresh()

    # Access a configuration setting directly from within Flask configuration
    print("Request for index page received")
    context = {}
    context["message"] = app.config.get("message")
    return render_template("index.html", **context)

更新您的範本 index.html 以使用新的組態值。

<!doctype html>
<head>
  <title>Hello Azure App Configuration - Python Flask Example</title>
</head>
<html>

<body>
  <main>
    <div>
      <h1>{{message}}</h1>
    </div>
  </main>
</body>
</html>

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

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

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

注意:如果重新整理間隔尚未經過,則不會立即嘗試重新整理並傳回資料。

下一步

在本教學課程中,您已啟用 Python 應用程式,以動態方式從 Azure 應用程式組態重新整理組態設定。 如需了解如何使用 Azure 受控識別簡化存取 Azure 應用程式組態,請繼續進行下一個教學課程。