输入绑定和geo分布式cosmosdb

时间:2017-12-20 23:56:37

标签: azure-cosmosdb azure-functions

我有一个将读取副本分发到多个区域的ComsosDB。我在每个区域中都有多个Azure功能的实例,这些区域也有对CosmosDB的输入绑定,我希望确保它们与Cosmos数据库的本地复制进行通信,而不是回到世界各地到写入位置。 /> 我看到SDK允许设置PreferredLocations,输入绑定是否通过一些自动发现为我处理这个,或者这是我可以在我的连接字符串中设置的东西? https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmosdb.table.tableconnectionpolicy.preferredlocations?view=azure-dotnet#Microsoft_Azure_CosmosDB_Table_TableConnectionPolicy_PreferredLocations

我可以为类似于REST api https://docs.microsoft.com/en-us/azure/cosmos-db/tutorial-global-distribution-sql-api#rest的每个函数手动输入AccountEndpoint到本地区域,但是我希望函数绑定能够自动处理,或者在发生故障时使用一些提示。

1 个答案:

答案 0 :(得分:0)

DocumentClient实例的构造函数(您可以更改ConnectionPolicy的位置)在函数集成中为not exposed

在这种情况下,您可以在函数中创建自己的DocumentClient实例,并在那里设置配置。作为性能建议,不要在DocumentClient函数代码中创建Run,而是将其创建为静态变量,以便在函数执行之间重用它而不是重新创建。

如何维护静态可重用DocumentClient

的示例
#r "Microsoft.Azure.Documents.Client"
using System.Net;
using Microsoft.Azure.Documents.Client;

static DocumentClient client = new DocumentClient(
new Uri("https://<your-account>.documents.azure.com:443/"), 
"<your-key>",
new ConnectionPolicy(){
    // TODO: set your preferences here
});

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    var document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri("<your-db>","<your-collection>","<some-id>"));

    return req.CreateResponse(HttpStatusCode.OK, document.Resource.Id);
}