Share via


チュートリアル: Python で動的な構成を使用する

Azure App Configuration Python プロバイダーには、組み込みのキャッシュと更新の機能が含まれています。 このチュートリアルでは、Python アプリケーションで動的構成を有効にする方法について説明します。

前提条件

キーと値を追加する

Azure App Configuration ストアに次のキーと値を追加します。 Azure portal または CLI を使用してストアにキーと値を追加する方法の詳細については、キーと値の作成に関する記事を参照してください。

キー Label Content type
message Hello World! 空のままにします 空のままにします
sentinel 1 空のままにします 空のままにします

Note

''センチネル キー'' は、他のすべてのキーの変更を完了した後に更新するキーです。 センチネル キーをアプリで監視してください。 変更が検出されると、アプリによって構成の値がすべて更新されます。 このアプローチは、アプリの構成の一貫性を確保するのに役立ち、すべてのキーの変更を監視する場合と比べると、Azure App Configuration ストアに対して行う全体的な要求の数が少なくなります。

コンソール アプリケーション

  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 App Configuration ストアに対して次のキーと値を更新します。

    Key Label Content type
    message Hello World が更新されました。 空のままにします 空のままにします
    sentinel 2 空のままにします 空のままにします
  5. 値が更新されると、更新間隔が経過すると更新された値が出力されます。

    Hello World Refreshed!
    

Web アプリケーション

次の例は、更新可能な構成値を使用するように既存の Web アプリケーションを更新する方法を示しています。 コールバックは、load 関数の on_refresh_success キーワード引数に指定できます。 このコールバックは、サーバーで構成の変更が検出されたときに呼び出され、アプリケーションの構成値を更新するために使用できます。

app.py で、構成値を読み込むように Azure App Configuration を設定します。 次に、エンドポイントを更新して、更新された構成値を確認します。

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>

完全なサンプル プロジェクトはこちらから参照できます。

これらのエンドポイントがトリガーされるたびに、最新の構成値が使用されていることを確認するための更新チェックを実行できます。 更新間隔が経過していない場合、または更新が既に進行中の場合、チェックはすぐに返されます。

更新が完了すると、すべての値が一度に更新されるため、構成は常にオブジェクト内で一貫しています。

注: 更新間隔が経過していない場合、更新は試行されないため、すぐに返されます。

次のステップ

このチュートリアルでは、Azure App Configuration から動的に構成設定を更新できるように Python アプリを設定しました。 Azure App Configuration へのアクセスを効率化する Azure マネージド ID を使用する方法については、次のチュートリアルに進んでください。