使用適用於 MongoDB 的 Azure Cosmos DB API 來設定全域散發的資料庫Set up global distributed database using Azure Cosmos DB's API for MongoDB

在本文中,我們將示範如何使用 Azure 入口網站,利用適用於 MongoDB 的 Azure Cosmos DB API 來設定全域散發的資料庫並與之連線。In this article, we show how to use the Azure portal to setup a global distributed database and connect to it using Azure Cosmos DB's API for MongoDB.

本文涵蓋下列工作:This article covers the following tasks:

使用 Azure 入口網站新增全球資料庫區域Add global database regions using the Azure portal

全球所有 Azure 區域都有提供 Azure Cosmos DB。Azure Cosmos DB is available in all Azure regions worldwide. 選取資料庫帳戶的預設一致性層級之後,您可以關聯一或多個區域 (取決於您對於預設一致性層級和全球發佈需求的選擇)。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 global 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.

對於 BCDR,建議根據業務持續性和災害復原 (BCDR):Azure 配對區域一文中所述的區域配對來新增區域。For BCDR, it is recommended to add regions based on the region pairs described in the Business continuity and disaster recovery (BCDR): Azure Paired Regions article.

驗證您的區域設定Verifying your regional setup

使用適用於 MongoDB 的 Azure Cosmos DB API 來檢查全域設定的最簡單方式就是從 Mongo 殼層執行 isMaster() 命令。A simple way to check your global configuration with Cosmos DB's API for MongoDB is to run the isMaster() command from the Mongo Shell.

從您的 Mongo 殼層︰From your Mongo Shell:

   db.isMaster()

範例結果︰Example results:

   {
      "_t": "IsMasterResponse",
      "ok": 1,
      "ismaster": true,
      "maxMessageSizeBytes": 4194304,
      "maxWriteBatchSize": 1000,
      "minWireVersion": 0,
      "maxWireVersion": 2,
      "tags": {
         "region": "South India"
      },
      "hosts": [
         "vishi-api-for-mongodb-southcentralus.documents.azure.com:10255",
         "vishi-api-for-mongodb-westeurope.documents.azure.com:10255",
         "vishi-api-for-mongodb-southindia.documents.azure.com:10255"
      ],
      "setName": "globaldb",
      "setVersion": 1,
      "primary": "vishi-api-for-mongodb-southindia.documents.azure.com:10255",
      "me": "vishi-api-for-mongodb-southindia.documents.azure.com:10255"
   }

連線到慣用的區域Connecting to a preferred region

適用於 MongoDB 的 Azure Cosmos DB API 可讓您針對全域散發的資料庫,指定集合的讀取喜好設定。The Azure Cosmos DB's API for MongoDB enables you to specify your collection's read preference for a globally distributed database. 為了兼顧低延遲讀取和全球高可用性,建議將集合的讀取喜好設定設為 [最接近]。For both low latency reads and global 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", "Southeast Asia");
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 globally replicated account by reading Consistency levels in Azure Cosmos DB. 如需有關 Azure Cosmos DB 中全域資料庫複寫運作方式的詳細資訊,請參閱使用 Azure Cosmos DB 來全域散發資料And for more information about how global database replication works in Azure Cosmos DB, see Distribute data globally with Azure Cosmos DB.

後續步驟Next steps

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

  • 使用 Azure 入口網站來設定全域散發Configure global distribution using the Azure portal
  • 使用適用於 MongoDB 的 Cosmos DB API 來設定全域散發Configure global 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.