你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
存储配置数据的常见方法是使用文件。 如果想要使用 Azure 应用配置来管理配置数据,但当前使用文件,则无需手动输入数据。 可以使用工具 导入配置文件。
如果计划在 App Configuration 中管理数据,那么导入操作就是一次性的数据迁移。 另一种方法是继续管理文件中的配置数据,并在持续集成和持续交付(CI/CD)过程中反复导入文件。 采用 配置即代码时会出现这种情况。
使用配置文件时,有两个文件内容配置文件可用:
- 默认文件内容配置文件:常规配置文件架构
- KVSet 文件内容配置文件:包含所有应用配置键值属性的架构
本文对这两个文件内容配置文件进行了讨论。 它还提供导入和导出配置文件的示例。 这些示例使用 Azure CLI,但本文中的概念也适用于其他应用配置导入方法。
文件内容配置文件:默认
在应用配置工具中,默认文件内容配置方案是现有编程框架和系统普遍采用的传统配置架构。 此配置文件用于应用程序配置导入工具,例如 Azure 门户、Azure CLI、Azure Pipelines 中的 Azure 应用程序配置导入任务、GitHub Actions。 应用配置支持 JSON、YAML 和属性文件格式。
如果要在开发过程中将文件用作应用程序的回退配置或本地配置,则此配置文件非常有用。 导入配置文件时,需指定将数据转换为应用配置键值和功能标志的方式。
以下配置文件 appsettings.json提供了默认文件内容配置文件的示例。 此文件包含一个配置设置和一个功能标志。
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"feature_management": {
"feature_flags": [
{
"id": "Beta",
"enabled": false
}
]
}
}
若要将此文件导入应用配置,请运行以下 Azure CLI 命令。 它将 dev 标签应用于设置和功能标志,并使用冒号 (:) 作为分隔符来展平键名称。
az appconfig kv import --label dev --separator : --name <App-Configuration-store-name> --source file --path appsettings.json --format json
可以选择性地将以下参数添加到上述命令: --profile appconfig/default 参数是可选的,因为默认配置文件是 appconfig/default。
导入期间,Azure Key Vault 引用需要特定内容类型。 因此,请将它们保存在单独的文件中,如以下文件中所示, keyvault-refs.json:
{
"Database:ConnectionString": {
"uri": "https://<Key-Vault-name>.vault.azure.net/secrets/db-secret"
}
}
若要导入此文件,请运行以下 Azure CLI 命令。 它将 test 标签应用于 Key Vault 引用,并采用 Key Vault 引用内容类型。
az appconfig kv import --label test --content-type "application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8" --name <App-Configuration-store-name> --source file --path keyvault-refs.json --format json
下表显示了应用配置存储中的所有导入数据:
| 密钥 | 值 | Label | 内容类型 |
|---|---|---|---|
| .appconfig.featureflag/Beta | {“id”:“Beta”,“description”:“”,“enabled”: false,“conditions”:{“client_filters”:[]}} | 开发人员 | application/vnd.microsoft.appconfig.ff+json;charset=utf-8 |
| Logging:LogLevel:Default | 警告 | 开发人员 | |
| Database:ConnectionString | {“uri”:“https://<Key-Vault-name.vault.azure.net/secrets/db-secret>“} | 测试 | application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8 |
文件内容配置文件:KVSet
在应用配置工具中,KVSet 文件内容配置文件是包含应用配置键值的所有属性的文件架构。 包括键名称、其值、标签、内容类型和标记。 由于键值的所有属性都位于文件中,因此导入文件时无需指定转换规则。
使用 KVSet 配置文件时,可以在一个文件中定义常规键值、Key Vault 引用和功能标志。 因此,如果要管理一个文件中的所有应用程序配置数据,并通过一步操作进行导入,则此配置文件非常有用。
使用此配置文件的文件采用 JSON 格式。 有关架构规范,请参阅 KVSet 文件架构。
以下文件appconfigdata.json是基于KVSet文件内容的配置规范。 此文件包含功能标志、Key Vault 引用和标准密钥值。
{
"items": [
{
"key": ".appconfig.featureflag/Beta",
"value": "{\"id\":\"Beta\",\"description\":\"Beta feature\",\"enabled\":true,\"conditions\":{\"client_filters\":[]}}",
"label": "dev",
"content_type": "application/vnd.microsoft.appconfig.ff+json;charset=utf-8",
"tags": {}
},
{
"key": "Database:ConnectionString",
"value": "{\"uri\":\"https://<Key-Vault-name>.vault.azure.net/secrets/db-secret\"}",
"label": "test",
"content_type": "application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8",
"tags": {}
},
{
"key": "Logging:LogLevel:Default",
"value": "Debug",
"label": "dev",
"content_type": null,
"tags": {}
}
]
}
在上一部分,一个示例演示如何将数据导入到应用配置存储中。 可以使用以下 Azure CLI 命令将数据导出到文件:
az appconfig kv export --profile appconfig/kvset --label * --name <App-Configuration-store-name> --destination file --path appconfigdata.json --format json
导出文件后,在文本编辑器中打开该文件,并进行以下更改:
- 将
Beta功能标志enabled属性设置为true. - 将
Logging:LogLevel:Default属性设置为Debug。
若要将更新的文件导入应用配置存储区,请运行以下 CLI 命令,其中包括参数 --profile appconfig/kvset 。 无需像为默认文件内容配置文件指定数据转换规则(如分隔符、标签或内容类型)。 所有所需的信息都已在文件中。
az appconfig kv import --profile appconfig/kvset --name <App-Configuration-store-name> --source file --path appconfigdata.json --format json
下表显示了应用配置存储中的所有导入数据:
| 密钥 | 值 | Label | 内容类型 |
|---|---|---|---|
| .appconfig.featureflag/Beta | {"id":"Beta","description":"Beta feature","enabled":true,"conditions":{"client_filters":[]}} | 开发人员 | application/vnd.microsoft.appconfig.ff+json;charset=utf-8 |
| Logging:LogLevel:Default | 调试 | 开发人员 | |
| Database:ConnectionString | {“uri”:“https://<Key-Vault-name.vault.azure.net/secrets/db-secret>“} | 测试 | application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8 |