如何在使用 Azure Cosmos DB 的应用程序配置多主数据库How to configure multi-master in your applications that use Azure Cosmos DB

要在应用程序中使用多主数据库功能,需要启用多区域写入并在 Azure Cosmos DB 中配置多宿主功能。To use multi-master feature in your application, you need to enable multi-region writes and configure the multi-homing capability in Azure Cosmos DB. 可通过设置部署应用程序的区域来配置多宿主功能。Multi-homing is configured by setting the region where the application is deployed.

.NET SDK v2.NET SDK v2

若要在应用程序中启用多主数据库,请将 UseMultipleWriteLocations 设置为 true,并将 SetCurrentLocation 配置为在其中部署应用程序并复制 Azure Cosmos DB 的区域。To enable multi-master in your applications set UseMultipleWriteLocations to true, and configure SetCurrentLocation to the region in which the application is being deployed and Azure Cosmos DB is replicated.

ConnectionPolicy policy = new ConnectionPolicy
    {
        ConnectionMode = ConnectionMode.Direct,
        ConnectionProtocol = Protocol.Tcp,
        UseMultipleWriteLocations = true
    };
policy.SetCurrentLocation("China North 2");

.NET SDK v3(预览版).NET SDK v3 (preview)

若要在应用程序中启用多主数据库,请将 UseCurrentRegion 配置为在其中部署应用程序并复制 Cosmos DB 的区域。To enable multi-master in your applications configure UseCurrentRegion to the region in which the application is being deployed and Cosmos DB is replicated.

CosmosConfiguration config = new CosmosConfiguration("endpoint", "key");
config.UseCurrentRegion("China North");
CosmosClient client = new CosmosClient(config);

Java 异步 SDKJava Async SDK

若要在应用程序中启用多主数据库,请设置 policy.setUsingMultipleWriteLocations(true),并将 policy.setPreferredLocations 配置为在其中部署应用程序并复制 Cosmos DB 的区域。To enable multi-master in your applications set policy.setUsingMultipleWriteLocations(true) and configure policy.setPreferredLocations to the region in which the application is being deployed and Cosmos DB is replicated.

ConnectionPolicy policy = new ConnectionPolicy();
policy.setUsingMultipleWriteLocations(true);
policy.setPreferredLocations(Collections.singletonList(region));

AsyncDocumentClient client =
    new AsyncDocumentClient.Builder()
        .withMasterKeyOrResourceToken(this.accountKey)
        .withServiceEndpoint(this.accountEndpoint)
        .withConsistencyLevel(ConsistencyLevel.Eventual)
        .withConnectionPolicy(policy).build();

Node.js、JavaScript、TypeScript SDKNode.js, JavaScript, TypeScript SDK

若要在应用程序中启用多主数据库,请将 connectionPolicy.UseMultipleWriteLocations 设置为 true,并将 connectionPolicy.PreferredLocations 配置为在其中部署应用程序并复制 Cosmos DB 的区域。To enable multi-master in your applications set connectionPolicy.UseMultipleWriteLocations to true and configure connectionPolicy.PreferredLocations to the region in which the application is being deployed and Cosmos DB is replicated.

const connectionPolicy: ConnectionPolicy = new ConnectionPolicy();
connectionPolicy.UseMultipleWriteLocations = true;
connectionPolicy.PreferredLocations = [region];

const client = new CosmosClient({
  endpoint: config.endpoint,
  auth: { masterKey: config.key },
  connectionPolicy,
  consistencyLevel: ConsistencyLevel.Eventual
});

Python SDKPython SDK

若要在应用程序中启用多主数据库,请将 connection_policy.UseMultipleWriteLocations 设置为 true,并将 connection_policy.PreferredLocations 配置为在其中部署应用程序并复制 Cosmos DB 的区域。To enable multi-master in your applications set connection_policy.UseMultipleWriteLocations to true and configure connection_policy.PreferredLocations to the region in which the application is being deployed and Cosmos DB is replicated.

connection_policy = documents.ConnectionPolicy()
connection_policy.UseMultipleWriteLocations = True
connection_policy.PreferredLocations = [region]

client = cosmos_client.CosmosClient(self.account_endpoint, {'masterKey': self.account_key}, connection_policy, documents.ConsistencyLevel.Session)

后续步骤Next steps

接下来可以阅读以下文章:Next you can read the following articles: