Azure 应用程序配置 Python 提供程序包括内置缓存和刷新功能。 本教程介绍如何在 Python 应用程序中启用动态配置。
先决条件
- 具有活动订阅的 Azure 帐户。 创建试用版订阅。
- 一个 Azure 应用配置存储。 创建存储区。
- Python 3.8 或更高版本 - 有关在 Windows 上设置 Python 的信息,请参阅 Windows 上的 Python 文档
添加键值
将以下键值添加到 Azure 应用程序配置存储。 有关如何使用 Azure 门户或 CLI 将键值添加到存储区的详细信息,请转到创建键值。
| 密钥 | 值 | 标签 | 内容类型 |
|---|---|---|---|
| message | 世界您好! | 留空 | 留空 |
| sentinel | 1 | 留空 | 留空 |
注意
哨兵键是指在完成对所有其他键的更改后才更新的键。 您的应用会监控哨兵键。 检测到更改时,应用会刷新所有配置值。 与监视所有键以了解更改情况相比,此方法有助于确保应用程序中配置的一致性,并减少对 Azure 应用程序配置存储发出的请求总数。
控制台应用程序
创建名为 app.py 的新 Python 文件并添加以下代码:
可以使用
DefaultAzureCredential向应用程序配置存储区进行身份验证。 按照说明,将应用配置数据读取者角色分配给您的凭据。 在运行应用程序之前,请务必留出足够的时间让权限生效。from azure.appconfiguration.provider import load, WatchKey from azure.identity import DefaultAzureCredential import os import time endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT") # Connecting to Azure App Configuration using connection string # Setting up to refresh when the Sentinel key is changed. config = load( endpoint=endpoint, credential=DefaultAzureCredential(), 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)运行脚本:
python app.py验证输出。
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!将以下键值更新到 Azure 应用程序配置存储。
密钥 值 标签 内容类型 message Hello World 已刷新! 留空 留空 哨兵 2 留空 留空 值更新后,经过刷新间隔,更新后的值才会显示出来。
Hello World Refreshed!
Web 应用程序
以下示例演示如何更新现有 Web 应用程序以使用可刷新的配置值。 可以向 on_refresh_success 函数的 load 关键字参数提供回调。 在服务器上检测到配置更改时,将会调用此回调,并且还可用于更新应用程序中的配置值。
在 app.py 中,设置 Azure 应用程序配置以加载配置值。 然后更新您的端点,以检查是否有更新后的配置值。
from azure.appconfiguration.provider import load, WatchKey
from azure.identity import DefaultAzureCredential
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(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"),
credential=DefaultAzureCredential(),
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 应用程序配置的访问,请继续学习下一教程。