使用表 API 设置 Azure Cosmos DB 多区域分发Set up Azure Cosmos DB multiple-region distribution using the Table API

本文涵盖以下任务:This article covers the following tasks:

  • 使用 Azure 门户配置多区域分发Configure multiple-region distribution using the Azure portal
  • 使用表 API 配置多区域分发Configure multiple-region distribution using the Table API

有关 Azure Cosmos DB 中多区域数据库复制工作原理的详细信息,请参阅使用 Cosmos DB 全局分发数据For more information about how multiple-region database replication works in Azure Cosmos DB, see Distribute data globally with Cosmos DB.

使用 Azure 门户添加全局数据库区域Add global database regions using the Azure portal

在中国的所有 Azure 区域中都可使用 Azure Cosmos DB。Azure Cosmos DB is available in all Azure regions across china. 为数据库帐户选择默认的一致性级别后,可以关联一个或多个区域(具体取决于所选的默认一致性级别和多区域分发需求)。After selecting the default consistency level for your database account, you can associate one or more regions (depending on your choice of default consistency level and multiple-region distribution needs).

  1. Azure 门户的左侧栏中,单击“Azure Cosmos DB” 。In the Azure portal, in the left bar, click Azure Cosmos DB.

  2. 在“Azure Cosmos DB” 页中,选择要修改的数据库帐户。In the Azure Cosmos DB page, select the database account to modify.

  3. 在“帐户”页上的菜单中单击“全局复制数据” 。In the account page, click Replicate data globally from the menu.

  4. 在“全局复制数据” 页中,通过单击地图中的区域选择要添加或删除的区域,并单击“保存” 。In the Replicate data globally page, select the regions to add or remove by clicking regions in the map, and then click Save. 添加区域会产生费用,有关详细信息,请参阅定价页使用 Azure Cosmos DB 全局分发数据There is a cost to adding regions, see the pricing page or the Distribute data globally with Azure Cosmos DB article for more information.

    单击图中的区域可以添加或删除区域

添加第二个区域后,门户中的“全局复制数据”页上会启用“手动故障转移”选项。Once you add a second region, the Manual Failover option is enabled on the Replicate data globally page in the portal. 可以使用此选项测试故障转移过程或更改主写入区域。You can use this option to test the failover process or change the primary write region. 添加第三个区域后,会在同一页上启用“故障转移优先级” 选项,以便更改读取的故障转移顺序。Once you add a third region, the Failover Priorities option is enabled on the same page so that you can change the failover order for reads.

选择全局数据库区域Selecting global database regions

配置两个或更多区域有两个常见方案:There are two common scenarios for configuring two or more regions:

  1. 向最终用户提供对数据的低延迟访问,而无论用户位于全球范围内的何位置Delivering low-latency access to data to end users no matter where they are located around the globe
  2. 添加区域复原以实现业务连续性和灾难恢复 (BCDR)Adding regional resiliency for business continuity and disaster recovery (BCDR)

若要向最终用户提供低延迟,建议在与应用程序用户所在位置对应的区域中同时部署应用程序和 Azure Cosmos DB。For delivering low-latency to end users, it is recommended that you deploy both the application and Azure Cosmos DB in the regions that correspond to where the application's users are located.

使用表 API 连接到首选区域Connecting to a preferred region using the Table API

为了利用多区域分发,客户端应用程序应当指定其应用程序在其中运行的当前位置。In order to take advantage of the multiple-region distribution, client applications should specify the current location where their application is running. 这是通过设置 CosmosExecutorConfiguration.CurrentRegion 属性实现的。This is done by setting the CosmosExecutorConfiguration.CurrentRegion property. CurrentRegion 属性应当包含单个位置。The CurrentRegion property should contain a single location. 每个客户端实例都可以指定其自己的区域,以实现低延迟读取。Each client instance can specify their own region for low latency reads. 区域必须使用其显示名称命名,例如“中国北部”。The region must be named by using their display names such as "China North".

Azure Cosmos DB 表 API SDK 将基于帐户配置和当前的区域可用性自动选取要与之通信的最佳终结点。The Azure Cosmos DB Table API SDK automatically picks the best endpoint to communicate with based on the account configuration and current regional availability. 它优先选择最靠近的区域来使客户端实现更低的延迟。It prioritizes the closest region to provide better latency to clients. 在设置当前的 CurrentRegion 属性后,读取和写入请求将如下所述进行定向:After you set the current CurrentRegion property, read and write requests are directed as follows:

  • 读取请求: 所有读取请求都将发送到所配置的 CurrentRegionRead requests: All read requests are sent to the configured CurrentRegion. SDK 会基于邻近性自动选择回退异地复制区域以实现高可用性。Based on the proximity, the SDK automatically selects a fallback geo-replicated region for high availability.

  • 写入请求: SDK 会自动将所有写入请求发送到当前写入区域。Write requests: The SDK automatically sends all write requests to the current write region. 在多主帐户中,当前区域也将为写入请求提供服务。In a multi master account, current region will serve the writes requests as well. SDK 会基于邻近性自动选择回退异地复制区域以实现高可用性。Based on the proximity, the SDK automatically selects a fallback geo-replicated region for high availability.

如果未指定 CurrentRegion 属性,则 SDK 会将当前写入区域用于所有操作。If you don't specify the CurrentRegion property, the SDK uses the current write region for all operations.

例如,如果某个 Azure Cosmos 帐户位于“中国北部”和“中国东部”区域中。For example, if an Azure Cosmos account is in "China North" and "China East" regions. 如果“中国北部”是写入区域,并且应用程序位于“中国东部”。If "China North" is the write region and the application is present in "China East". 如果未配置 CurrentRegion 属性,则所有读取和写入请求始终会定向到“中国北部”区域。If the CurrentRegion property is not configured, all the read and write requests are always directed to the "China North" region. 如果配置了 CurrentRegion 属性,则所有读取请求都将在“中国东部”区域中接受服务。If the CurrentRegion property is configured, all the read requests are served from "China East" region.

后续步骤Next steps

在本教程中已完成以下操作:In this tutorial, you've done the following:

  • 使用 Azure 门户配置多区域分发Configure multiple-region distribution using the Azure portal
  • 使用 Azure Cosmos DB 表 API 配置多区域分发Configure multiple-region distribution using the Azure Cosmos DB Table APIs