【问题标题】:Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: ServiceUnavailable (503)Microsoft.Azure.Cosmos.Client:响应状态代码不表示成功:ServiceUnavailable (503)
【发布时间】:2020-08-31 06:58:22
【问题描述】:

我正在尝试使用本地设置将新项目添加到 cosmos db 中。

这是我插入批量的代码:

private async Task AddSubscription(EnableOrDisableSubscriptionCommand command, SubscriptionAction subscriptionAction, IList<int> notificationCategoryTypes)
        {
            List<Task> bulkOperations = new List<Task>();
            foreach (var notificationCategory in notificationCategoryTypes)
            {
                var notificationTypes = Utility.GetNotificationTypes((NotificationCategoryType)notificationCategory);

                foreach (var notificationType in notificationTypes)
                {
                    foreach (var payerAccountSubscriptions in command.Subscriptions)
                    {
                        if (payerAccountSubscriptions.AccountNumbers?.Any() ?? false)
                        {
                            foreach (var accountNumber in payerAccountSubscriptions.AccountNumbers.Where(a => !string.IsNullOrEmpty(a)))
                            {
                                bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
                                      payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, accountNumber, command.UserRole,
                                      command.UserId));
                            }
                        }
                        else
                        {
                            bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
                                payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, null, command.UserRole,
                                command.UserId));

                        }
                    }
                }
            }
            await Task.WhenAll(bulkOperations);
        }

 public Task<ItemResponse<T>> CreateItemAsync(T item, string partitionKeyValue)
        {
            return _container.CreateItemAsync<T>(item, new PartitionKey(partitionKeyValue));
        }

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    //localhost
    "CosmosDbId": "Notifications",
    "CosmoDbAuthKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
    "CosmoDbEndpoint": "https://localhost:8081/",
}
}

我已经安装了 Azure Cosmos DB 模拟器。

我收到以下未提供太多信息的错误?

[8/31/2020 6:49:10 AM] System.Private.CoreLib:出现异常 执行函数:EnableOrDisableSubscriptions。 Microsoft.Azure.Cosmos.Client:响应状态码未指示 成功:服务不可用(503);子状态:0;活动

【问题讨论】:

  • 也可能是模拟器无法初始化网络堆栈。检查您是否安装了 Pulse 安全客户端或瞻博网络客户端,因为它们的网络过滤器驱动程序可能会导致问题。卸载第三方网络过滤器驱动程序通常可以解决此问题。或者,使用 /DisableRIO 启动模拟器,这会将模拟器网络通信切换到常规 Winsock。
  • 你能显示异常的完整堆栈跟踪吗?
  • 卸载 Azure Cosmos DB 模拟器旧版本并安装新版本 2.11.2 时问题得到解决。

标签: azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

我在遵循 Cosmos DB SQL API 入门指南后遇到了同样的问题。

解决方法是指定ConnectionMode = ConnectionMode.Gateway

var cosmosClient = new CosmosClient(EndpointUrl, AuthorizationKey, new CosmosClientOptions() { AllowBulkExecution = true, ConnectionMode = ConnectionMode.Gateway });

【讨论】:

    【解决方案2】:

    请确保在 .NET SDK 中注意以下几点:

    • 初始化单例 DocumentClient
    • 使用直接连接和 TCP 协议(​​ConnectionMode.Direct 和 ConnectionProtocol.Tcp)
    • 并行使用 100 个任务(取决于您的硬件)
    • 将 DocumentClient 构造函数中的 MaxConnectionLimit 增加到一个较高的值,例如 1000 个连接
    • 开启 gcServer
    • 确保您的集合具有适当的预置吞吐量(以及良好的分区键)

    您现在可以直接使用批量执行程序库进行批量操作 - https://docs.microsoft.com/en-us/azure/cosmos-db/bulk-executor-dot-net

    【讨论】:

    • 这些是开发连接到 CosmosDB 的应用程序时应考虑的最佳实践。感谢那。但这个特定问题与本地 CosmosDB 模拟器有关,大多数情况下“重置”就是它所需要的 docs.microsoft.com/en-us/azure/cosmos-db/…。
    • 卸载 Azure Cosmos DB 模拟器旧版本并安装新版本 2.11.2 时问题得到解决。
    猜你喜欢
    • 2016-04-22
    • 2020-10-29
    • 1970-01-01
    • 2021-11-02
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多