使用 Azure Cosmos DB 的用于 MongoDB 的 API 设置多区域分布式数据库Set up multiple-region distributed database using Azure Cosmos DB's API for MongoDB

本文将展示如何使用 Azure 门户设置一个多区域分布式数据库,并使用 Azure Cosmos DB 的用于 MongoDB 的 API 连接到该数据库。In this article, we show how to use the Azure portal to setup a multiple-region distributed database and connect to it using Azure Cosmos DB's API for MongoDB.

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

有关 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.

[一致性]:../articles/cosmos-db/consistency-levels.md [azureregions]:https://www.azure.cn/support/service-dashboard/ [产品/服务]:https://www.azure.cn/pricing/details/cosmos-db/ [consistency]: ../articles/cosmos-db/consistency-levels.md [azureregions]: https://www.azure.cn/support/service-dashboard/ [offers]: https://www.azure.cn/pricing/details/cosmos-db/

验证区域设置Verifying your regional setup

检查 Azure Cosmos DB 的用于 MongoDB 的 API 中的多区域配置的一种简单方法是从 Mongo Shell 运行 isMaster() 命令。A simple way to check your multiple-region configuration with Cosmos DB's API for MongoDB is to run the isMaster() command from the Mongo Shell.

从 Mongo Shell:From your Mongo Shell:

   db.isMaster()

示例结果:Example results:

   {
      "_t": "IsMasterResponse",
      "ok": 1,
      "ismaster": true,
      "maxMessageSizeBytes": 4194304,
      "maxWriteBatchSize": 1000,
      "minWireVersion": 0,
      "maxWireVersion": 2,
      "tags": {
         "region": "China North "
      },
      "hosts": [
         "vishi-api-for-mongodb-chinaeast.documents.azure.cn:10255",
         "vishi-api-for-mongodb-chinanorth.documents.azure.cn:10255",
      ],
      "setName": "globaldb",
      "setVersion": 1,
      "primary": "vishi-api-for-mongodb-chinanorth.documents.azure.cn:10255",
      "me": "vishi-api-for-mongodb-chinanorth.documents.azure.cn:10255"
   }

连接到首选区域Connecting to a preferred region

使用 Azure Cosmos DB 的用于 MongoDB 的 API,可以为多区域分布式数据库指定集合的读取首选项。The Azure Cosmos DB's API for MongoDB enables you to specify your collection's read preference for a multiple-region distributed database. 为实现低延迟读取和多区域高可用性,建议将集合的读取首选项设置为“就近”。For both low latency reads and multiple-region high availability, we recommend setting your collection's read preference to nearest. 当读取首选项配置为“就近”时,将从最近的区域进行读取。A read preference of nearest is configured to read from the closest region.

var collection = database.GetCollection<BsonDocument>(collectionName);
collection = collection.WithReadPreference(new ReadPreference(ReadPreferenceMode.Nearest));

对于具有主读取/写入区域和用于灾难恢复 (DR) 方案的辅助区域的应用程序,建议将集合的读取首选项设置为“辅助优先”。For applications with a primary read/write region and a secondary region for disaster recovery (DR) scenarios, we recommend setting your collection's read preference to secondary preferred. 当读取首选项配置为“辅助优先”时,如果主区域不可用,将从辅助区域进行读取。A read preference of secondary preferred is configured to read from the secondary region when the primary region is unavailable.

var collection = database.GetCollection<BsonDocument>(collectionName);
collection = collection.WithReadPreference(new ReadPreference(ReadPreferenceMode.SecondaryPreferred));

最后,如果愿意,可以手动指定读取区域。Lastly, if you would like to manually specify your read regions. 可以在读取首选项内设置区域标记。You can set the region Tag within your read preference.

var collection = database.GetCollection<BsonDocument>(collectionName);
var tag = new Tag("region", "China East");
collection = collection.WithReadPreference(new ReadPreference(ReadPreferenceMode.Secondary, new[] { new TagSet(new[] { tag }) }));

本教程到此结束。That's it, that completes this tutorial. 阅读 Azure Cosmos DB 中的一致性级别,了解如何管理多区域复制帐户的一致性。You can learn how to manage the consistency of your multiple-region replicated account by reading Consistency levels in Azure Cosmos DB. 有关 Azure Cosmos DB 中多区域数据库复制工作原理的详细信息,请参阅使用 Cosmos DB 多区域分配数据And for more information about how multiple-region database replication works in Azure Cosmos DB, see Distribute data multiple-region with Azure Cosmos DB.

后续步骤Next steps

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

  • 使用 Azure 门户配置多区域分发Configure multiple-region distribution using the Azure portal
  • 使用 Cosmos DB 的用于 MongoDB 的 API 配置多区域分发Configure multiple-region distribution using the Cosmos DB's API for MongoDB

现可继续学习下一个教程,了解如何使用 Azure Cosmos DB 本地模拟器在本地开发。You can now proceed to the next tutorial to learn how to develop locally using the Azure Cosmos DB local emulator.