你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 Python 的 Azure 应用程序配置 客户端库 - 版本 1.5.0

Azure 应用配置是一项托管服务,可帮助开发人员轻松且安全地集中管理其应用程序配置。

新式程序,尤其是在云端运行的程序,通常具有多个事实上已分发的组件。 跨这些组件分散配置设置可能导致应用程序部署过程中出现难以解决的错误。 使用 应用程序配置 将应用程序的所有设置安全地存储在一个位置。

使用用于应用程序配置的客户端库来创建和管理应用程序配置设置。

源代码 | 包 (Pypi) | 包 (Conda) | API 参考文档 | 产品文档

免责声明

对 Python 2.7 的 Azure SDK Python 包支持已于 2022 年 1 月 1 日结束。 有关详细信息和问题,请参阅 https://github.com/Azure/azure-sdk-for-python/issues/20691使用此包需要 Python 3.7 或更高版本。有关更多详细信息,请参阅 Azure SDK for Python 版本支持策略

入门

安装包

使用 pip 安装适用于 Python 的 Azure 应用程序配置 客户端库:

pip install azure-appconfiguration

先决条件

  • 使用此包需要 Python 3.7 或更高版本。
  • 需要一个 Azure 订阅和一个 配置存储 区才能使用此包。

若要创建配置存储,可以使用 Azure 门户或 Azure CLI

之后,创建配置存储:

az appconfig create --name <config-store-name> --resource-group <resource-group-name> --location eastus

验证客户端

若要与 应用程序配置 服务交互,需要创建 AzureAppConfigurationClient 类的实例。 为此,可以使用配置存储区连接字符串或使用 AAD 令牌。

使用连接字符串

获取凭据

使用以下 Azure CLI 代码片段从配置存储中获取连接字符串。

az appconfig credential list --name <config-store-name>

或者,从 Azure 门户获取连接字符串。

创建客户端

获得连接字符串的值后,可以创建 AzureAppConfigurationClient:

import os
from azure.appconfiguration import AzureAppConfigurationClient

CONNECTION_STRING = os.environ["APPCONFIGURATION_CONNECTION_STRING"]

# Create app config client
client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING)

使用 AAD 令牌

下面我们演示如何使用 DefaultAzureCredential 作为服务主体进行身份验证。 但是, AzureAppConfigurationClient 接受任何 azure 标识 凭据。 有关其他凭据的详细信息,请参阅 azure-identity 文档。

创建服务主体 (可选)

Azure CLI 代码片段演示如何创建新的服务主体。 在使用之前,请将“your-application-name”替换为服务主体的相应名称。

创建服务主体:

az ad sp create-for-rbac --name http://my-application --skip-assignment

输出:

{
    "appId": "generated app id",
    "displayName": "my-application",
    "name": "http://my-application",
    "password": "random password",
    "tenant": "tenant id"
}

使用输出设置 AZURE_CLIENT_ID) 上方 (“appId”, AZURE_CLIENT_SECRET) 上方 (“password”,) 环境变量上方 AZURE_TENANT_ID (“tenant”。 以下示例演示了在 Bash 中执行此操作的方法:

export AZURE_CLIENT_ID="generated app id"
export AZURE_CLIENT_SECRET="random password"
export AZURE_TENANT_ID="tenant id"

将其中一个适用的应用程序配置角色分配给服务主体。

创建客户端

设置 AZURE_CLIENT_IDAZURE_CLIENT_SECRETAZURE_TENANT_ID 环境变量后, DefaultAzureCredential 将能够对 AzureAppConfigurationClient 进行身份验证。

构造客户端还需要配置存储的 URL,可以从 Azure CLI 或 Azure 门户获取该 URL。 在 Azure 门户中,可以找到作为服务“终结点”列出的 URL

from azure.identity import DefaultAzureCredential
from azure.appconfiguration import AzureAppConfigurationClient

credential = DefaultAzureCredential()

client = AzureAppConfigurationClient(base_url="your_endpoint_url", credential=credential)

关键概念

配置设置

配置设置是配置存储区中的基本资源。 最简单的形式是键和值。 但是,还有其他属性,例如可修改内容类型和标记字段,它们允许以不同方式解释或关联值。

配置设置的 Label 属性提供了一种将配置设置分为不同维度的方法。 这些维度是用户定义的,可以采用任何形式。 用于标签的维度的一些常见示例包括区域、语义版本或环境。 许多应用程序都有一组必需的配置键,这些键具有不同的值,因为应用程序存在于不同的维度中。

例如,MaxRequests 在“NorthAmerica”中可以是 100,在“WestEurope”中可以是 200。 通过在“WestEurope”标签中创建一个名为 MaxRequests 且标签为“NorthAmerica”和另一个(仅具有不同值)的配置设置,应用程序可以在这两个维度中运行时无缝检索配置设置。

配置设置的属性:

key : str
label : str
content_type : str
value : str
last_modified : str
read_only : bool
tags : dict
etag : str

快照

Azure 应用程序配置允许用户创建配置存储的时间点快照,使他们能够将设置视为一个一致的版本。 此功能使应用程序能够保留一致的配置视图,确保不会因在进行更新时读取而导致各个设置的版本不匹配。 快照是不可变的,确保在出现问题时,可以放心地将配置回滚到上次已知良好的配置。

示例

以下部分提供了几个代码片段,涵盖了一些最常见的配置服务任务,包括:

创建配置设置

创建要存储在配置存储中的配置设置。 可通过两种方式存储配置设置:

  • 仅当存储区中不存在设置时,add_configuration_setting才会创建设置。
config_setting = ConfigurationSetting(
    key="MyKey", label="MyLabel", value="my value", content_type="my content type", tags={"my tag": "my tag value"}
)
added_config_setting = client.add_configuration_setting(config_setting)
  • 如果设置不存在,则set_configuration_setting将创建设置,或替代现有设置。
added_config_setting.value = "new value"
added_config_setting.content_type = "new content type"
updated_config_setting = client.set_configuration_setting(added_config_setting)

获取配置设置

获取以前存储的配置设置。

fetched_config_setting = client.get_configuration_setting(key="MyKey", label="MyLabel")

删除配置设置

删除现有配置设置。

client.delete_configuration_setting(
    key="MyKey",
    label="MyLabel",
)

列出配置设置

列出使用label_filter和/或key_filter筛选的所有配置设置。

config_settings = client.list_configuration_settings(label_filter="MyLabel")
for item in config_settings:
    print_configuration_setting(item)

创建快照

from azure.appconfiguration import ConfigurationSettingsFilter

filters = [ConfigurationSettingsFilter(key="my_key1", label="my_label1")]
response = client.begin_create_snapshot(name=snapshot_name, filters=filters)
created_snapshot = response.result()
print_snapshot(created_snapshot)

获取快照

received_snapshot = client.get_snapshot(name=snapshot_name)

存档快照

archived_snapshot = client.archive_snapshot(name=snapshot_name)
print_snapshot(archived_snapshot)

恢复快照

recovered_snapshot = client.recover_snapshot(name=snapshot_name)
print_snapshot(recovered_snapshot)

列出快照

for snapshot in client.list_snapshots():
    print_snapshot(snapshot)

列出快照的配置设置

for config_setting in client.list_configuration_settings(snapshot_name=snapshot_name):
    print_configuration_setting(config_setting)

异步 API

支持异步客户端。 若要使用异步客户端库,请从包 azure.appconfiguration.aio 而不是 azure.appconfiguration 导入 AzureAppConfigurationClient

import os
from azure.appconfiguration.aio import AzureAppConfigurationClient

CONNECTION_STRING = os.environ["APPCONFIGURATION_CONNECTION_STRING"]

# Create app config client
client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING)

此异步 AzureAppConfigurationClient 具有与同步签名相同的方法签名,只是它们是异步的。 例如,若要异步检索配置设置,可以使用async_client:

fetched_config_setting = await client.get_configuration_setting(key="MyKey", label="MyLabel")

若要使用list_configuration_settings,请同步调用它,并异步循环访问返回的异步迭代器

config_settings = client.list_configuration_settings(label_filter="MyLabel")
async for item in config_settings:
    print_configuration_setting(item)
from azure.appconfiguration import ConfigurationSettingsFilter

filters = [ConfigurationSettingsFilter(key="my_key1", label="my_label1")]
response = await client.begin_create_snapshot(name=snapshot_name, filters=filters)
created_snapshot = await response.result()
print_snapshot(created_snapshot)
received_snapshot = await client.get_snapshot(name=snapshot_name)
archived_snapshot = await client.archive_snapshot(name=snapshot_name)
print_snapshot(archived_snapshot)
recovered_snapshot = await client.recover_snapshot(name=snapshot_name)
print_snapshot(recovered_snapshot)
async for snapshot in client.list_snapshots():
    print_snapshot(snapshot)
async for config_setting in client.list_configuration_settings(snapshot_name=snapshot_name):
    print_configuration_setting(config_setting)

故障排除

有关如何诊断各种故障方案的详细信息,请参阅 故障排除指南

后续步骤

更多示例代码

此 GitHub 存储库中提供了多个应用程序配置客户端库示例。 其中包括:

有关详细信息,请参阅 示例自述文件

贡献

本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 https://cla.microsoft.com

提交拉取请求时,CLA 机器人将自动确定你是否需要提供 CLA,并相应地修饰 PR(例如标签、注释)。 直接按机器人提供的说明操作。 只需使用 CLA 对所有存储库执行一次这样的操作。

此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答;若有其他任何问题或意见,请联系 opencode@microsoft.com