复原能力和灾难恢复Resiliency and disaster recovery

目前,Azure 应用配置是一个区域性的服务。Currently, Azure App Configuration is a regional service. 每个配置存储在特定的 Azure 区域中创建。Each configuration store is created in a particular Azure region. 区域范围的服务中断会影响该区域中的所有存储。A region-wide outage affects all stores in that region. 应用配置不提供自动故障转移到另一个区域的机制。App Configuration doesn't offer automatic failover to another region. 本文提供有关如何使用跨 Azure 区域的多个配置存储来提高应用程序异地复原能力的一般性指导。This article provides general guidance on how you can use multiple configuration stores across Azure regions to increase the geo-resiliency of your application.

高可用性体系结构High-availability architecture

若要实现跨区域冗余,需要在不同的区域中创建多个应用程序配置存储。To realize cross-region redundancy, you need to create multiple App Configuration stores in different regions. 完成此设置后,应用程序至少会获得一个附加的配置存储,当主要存储不可访问时,应用程序可以回退到该附加存储。With this setup, your application has at least one additional configuration store to fall back on if the primary store becomes inaccessible. 下图演示了应用程序与其主要配置存储和辅助配置存储之间的拓扑:The following diagram illustrates the topology between your application and its primary and secondary configuration stores:

异地冗余的存储

应用程序会以并行方式从主要存储和辅助存储加载其配置。Your application loads its configuration from both the primary and secondary stores in parallel. 这样做可以提高成功获取配置数据的可能性。Doing this increases the chance of successfully getting the configuration data. 使两个存储中的数据保持同步由你负责。以下部分介绍如何将异地复原能力内置到应用程序中。You're responsible for keeping the data in both stores in sync. The following sections explain how you can build geo-resiliency into your application.

配置存储之间的故障转移Failover between configuration stores

从技术上讲,应用程序不会执行故障转移。Technically, your application isn't executing a failover. 它将会尝试同时从两个应用配置存储检索相同的配置数据集。It's attempting to retrieve the same set of configuration data from two App Configuration stores simultaneously. 按以下方式安排代码:让代码先从辅助存储加载数据,再从主要存储加载数据。Arrange your code so that it loads from the secondary store first and then the primary store. 此方法可确保只要主要存储中的配置数据可用就会优先进行加载。This approach ensures that the configuration data in the primary store takes precedence whenever it's available. 以下代码片段演示如何在 .NET Core 中实现这种安排:The following code snippet shows how you can implement this arrangement in .NET Core:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var settings = config.Build();
            config.AddAzureAppConfiguration(settings["ConnectionString_SecondaryStore"], optional: true)
                  .AddAzureAppConfiguration(settings["ConnectionString_PrimaryStore"], optional: true);
        })
        .UseStartup<Startup>();
    

请注意传入 AddAzureAppConfiguration 函数中的 optional 参数。Notice the optional parameter passed into the AddAzureAppConfiguration function. 将此参数设置为 true 时,如果该函数无法加载配置数据,则此参数可防止应用程序无法继续。When set to true, this parameter prevents the application from failing to continue if the function can't load configuration data.

配置存储之间的同步Synchronization between configuration stores

所有异地冗余的配置存储必须包含相同的数据集,这一点非常重要。It's important that your geo-redundant configuration stores all have the same set of data. 可通过两种方式实现此目的:There are two ways to achieve this:

使用“导出”功能手动备份Backup manually using the Export function

可以按需使用应用配置中的“导出”功能将数据从主要存储复制到辅助存储。You can use the Export function in App Configuration to copy data from the primary store to the secondary on demand. 可通过 Azure 门户和 CLI 使用此功能。This function is available through both the Azure portal and the CLI.

在 Azure 门户中,可以按照以下步骤将更改推送到另一个配置存储。From the Azure portal, you can push a change to another configuration store by following these steps.

  1. 转到“导入/导出”选项卡,然后依次选择“导出” > “应用配置” > “目标” > “选择资源”。Go to the Import/Export tab, and select Export > App Configuration > Target > Select a resource.

  2. 在打开的新边栏选项卡中,指定辅助存储的订阅、资源组和资源名称,然后选择“应用”。In the new blade that opens, specify the subscription, resource group, and resource name of your secondary store, then select Apply.

  3. UI 将会更新,使你可以选择要导出到辅助存储的配置数据。The UI is updated so that you can choose what configuration data you want to export to your secondary store. 可将默认时间值保持原样,并将“原始标签”和“标签”设置为相同的值 。You can leave the default time value as is and set both From label and Label to the same value. 选择“应用”。Select Apply. 对主要存储中的所有标签重复此操作。Repeat this for all the labels in your primary store.

  4. 每当配置更改时,应再次执行上述步骤。Repeat the previous steps whenever your configuration changes.

还可以使用 Azure CLI 来实现导出过程。The export process can also be achieved using the Azure CLI. 以下命令演示如何将所有配置从主要存储导出到辅助存储:The following command shows how to export all configurations from the primary store to the secondary:

    az appconfig kv export --destination appconfig --name {PrimaryStore} --dest-name {SecondaryStore} --label * --preserve-labels -y

使用 Azure Functions 实现自动备份Backup automatically using Azure Functions

可以使用 Azure Functions 自动执行备份过程。The backup process can be automated by using Azure Functions. 它利用了与应用程序配置中 Azure 事件网格的集成。It leverages the integration with Azure Event Grid in App Configuration. 一旦设置后,对于配置存储区中键值的任何更改,应用程序配置会向事件网格发布事件。Once set up, App Configuration will publish events to Event Grid for any changes made to key-values in a configuration store. 因此,Azure Functions 应用可以相应地侦听这些事件和备份数据。Thus, an Azure Functions app can listen to these events and backup data accordingly. 有关详细信息,请参阅有关如何自动备份应用程序配置存储教程。For details, see the tutorial on how to backup App Configuration stores automatically.

后续步骤Next steps

本文已介绍如何增强应用程序,以便在应用程序配置运行时实现异地复原。In this article, you learned how to augment your application to achieve geo-resiliency during runtime for App Configuration. 也可以在生成或部署时嵌入来自应用配置的配置数据。You also can embed configuration data from App Configuration at build or deployment time. 有关详细信息,请参阅与 CI/CD 管道集成For more information, see Integrate with a CI/CD pipeline.