Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
本教程介绍如何在 Azure Functions 应用中启用动态配置更新。 它是基于快速入门中所介绍的 Azure Functions 应用构建的。 在继续之前,请先完成 使用 Azure 应用配置创建 Azure Functions 应用 。
本教程中,您将学习如何:
- 为 Azure Functions 应用设置动态配置刷新。
- 使用应用配置中间件启用自动配置刷新。
- 在应用配置存储中发生更改时,请在函数调用中使用最新的配置。
Azure 应用配置 .NET 提供程序支持基于应用程序活动的缓存和动态刷新配置设置。 在本部分中,将提供程序配置为动态刷新设置,并使用应用配置中间件 Microsoft.Azure.AppConfiguration.Functions.Worker
(每次执行函数时)启用自动配置刷新。
Nota
Azure Functions 可与 Azure 应用程序配置配合使用,无论是在 独立进程模型 还是 进程内模型 中。 本教程以隔离工作者模型为例。 可以在 Azure 应用配置 GitHub 存储库中找到这两个模型的完整代码示例。
打开 Program.cs 文件并更新调用
AddAzureAppConfiguration
以包含ConfigureRefresh
该方法。 此方法配置刷新配置设置的条件,包括指定要监视的密钥以及刷新检查之间的间隔。// Connect to Azure App Configuration builder.Configuration.AddAzureAppConfiguration(options => { Uri endpoint = new(Environment.GetEnvironmentVariable("AZURE_APPCONFIG_ENDPOINT") ?? throw new InvalidOperationException("The environment variable 'AZURE_APPCONFIG_ENDPOINT' is not set or is empty.")); options.Connect(endpoint, new DefaultAzureCredential()) // Load all keys that start with `TestApp:` and have no label .Select("TestApp:*") // Reload configuration if any selected key-values have changed. // Use the default refresh interval of 30 seconds. It can be overridden via AzureAppConfigurationRefreshOptions.SetRefreshInterval. .ConfigureRefresh(refreshOptions => { refreshOptions.RegisterAll(); }); });
RegisterAll
调用该方法以指示应用配置提供程序在检测到任何所选键值(以 TestApp 开头且没有标签)发生更改时重新加载整个配置。 有关监视配置更改的详细信息,请参阅 配置刷新的最佳做法。默认情况下,刷新间隔设置为 30 秒。 可以通过调用
AzureAppConfigurationRefreshOptions.SetRefreshInterval
方法来自定义此间隔。通过添加应用配置中间件更新 Program.cs 文件,以便在每个函数执行时启用自动配置刷新:
// Connect to Azure App Configuration builder.Configuration.AddAzureAppConfiguration(options => { // Omitted the code added in the previous step. }); // Add Azure App Configuration middleware to the service collection. builder.Services.AddAzureAppConfiguration() // Use Azure App Configuration middleware for dynamic configuration refresh. builder.UseAzureAppConfiguration(); builder.ConfigureFunctionsWebApplication(); builder.Build().Run();
设置 环境变量。
将名为 AZURE_APPCONFIG_ENDPOINT 的环境变量设置为 Azure 门户中应用商店的“概述”下找到的应用程序配置存储区的终结点。
如果使用 Windows 命令提示符,则请运行以下命令并重启命令提示符,这样更改才会生效:
setx AZURE_APPCONFIG_ENDPOINT "<endpoint-of-your-app-configuration-store>"
如果使用 PowerShell,请运行以下命令:
$Env:AZURE_APPCONFIG_ENDPOINT = "<endpoint-of-your-app-configuration-store>"
如果使用 macOS 或 Linux,则请运行以下命令:
export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
若要测试函数,请按 F5。 如果系统提示,请按 Visual Studio 的请求下载和安装 Azure Functions Core (CLI) 工具。 你还需要启用防火墙例外,这样工具才能处理 HTTP 请求。
在 Azure Functions 运行时输出中复制你的函数的 URL。
将 HTTP 请求的 URL 粘贴到浏览器的地址栏。 下图显示了浏览器中函数返回的本地 GET 请求的响应。
在 Azure 门户中选择应用配置存储区,并在 配置资源管理器中更新以下密钥的值。
密钥 价值 TestApp:Settings:Message Azure 应用配置的数据 - 已更新 多次刷新浏览器。 在默认刷新间隔为 30 秒后,页面会显示从 Azure Functions 应用检索到的更新值。
如果不想继续使用本文中创建的资源,请删除此处创建的资源组以避免产生费用。
Importante
删除资源组的操作不可逆。 将永久删除资源组以及其中的所有资源。 请确保不要意外删除错误的资源组或资源。 如果在包含要保留的其他资源的资源组中创建了本文的资源,请从相应的窗格中单独删除每个资源,而不是删除该资源组。
- 登录到 Azure 门户,然后选择“资源组”。
- 在“按名称筛选”框中,输入资源组的名称。
- 在结果列表中,选择资源组名称以查看概述。
- 选择“删除资源组”。
- 系统会要求确认是否删除资源组。 重新键入资源组的名称进行确认,然后选择“删除”。
片刻之后,将会删除该资源组及其所有资源。
在本教程中,你已启用 Azure Functions 应用从应用配置动态刷新配置设置。
若要了解如何在 Azure Functions 应用中使用 Azure 应用配置中的功能标志,请继续学习以下教程。
若要了解如何使用 Azure 托管标识简化对应用配置的访问,请继续学习以下教程。