SDK で複数リージョンのサポートを構成する

完了

Azure Cosmos DB for NoSQL の .NET SDK では、CosmosClientOptions オブジェクト内の 2 つの異なるプロパティのどちらかを選択することで、書き込み先の優先リージョンを構成します。

ヒント

優先リージョンを指定しない場合は、SDK によってアカウントのプライマリ リージョンが自動的に既定に設定されます。 プライマリ リージョンはリージョンの一覧の最初のリージョンであり、通常は Azure Cosmos DB アカウントを作成したときに最初に選択したリージョンです。

単一の書き込みリージョンの選択

ApplicationRegion プロパティは、SDK が操作に使用するリージョンを指定します。 実際には、このリージョンは使用する書き込み可能なリージョンです。 この例では、選択したリージョンは 米国西部です。

CosmosClientOptions options = new()
{
    ApplicationRegion = Regions.WestUS
};

TokenCredential managedIdentityCredential = new ManagedIdentityCredentialBuilder()
    .clientId("<your-managed-identity-client-id>")
    .build();

using CosmosClient client = new CosmosClient("<your-cosmos-endpoint>", managedIdentityCredential, options);

CosmosClientBuilder を使用して、優先リージョンを構成することもできます。

using CosmosClient client = builder
    .WithApplicationRegion(Regions.WestUS)
    .Build();

クライアントが Azure Cosmos DB に接続すると、クライアントは使用可能なリージョンの一覧を取得し、選択したリージョンからの近接性に基づいて優先順位を付けます。 選択したリージョンを使用できない場合、クライアントは確立された順序で代替リージョンを試行します。

優先書き込みリージョンの一覧の作成

読み取りと書き込みの操作を試みるために、リージョンの優先順位付けされた独自の一覧を作成する場合は、 ApplicationPreferredRegions プロパティを使用できます。 最初の例で、このプロパティは、カスタムの優先順位が付けられた 3 つのリージョンの一覧に設定されています。

CosmosClientOptions options = new()
{
    ApplicationPreferredRegions = new List<string>
    {
        Regions.WestUS,
        Regions.AustraliaSoutheast,
        Regions.NorthEurope
    }
};

TokenCredential managedIdentityCredential = new ManagedIdentityCredentialBuilder()
    .clientId("<your-managed-identity-client-id>")
    .build();

using CosmosClient client = new CosmosClient("<your-cosmos-endpoint>", managedIdentityCredential, options);

ここでも、この同じ例は CosmosClientBuilder クラスを使用して実装できます。

using CosmosClient client = builder
    .WithApplicationPreferredRegions(
        new List<string>
        {
            Regions.WestUS,
            Regions.AustraliaSoutheast,
            Regions.NorthEurope
        }
    )
    .Build();