【问题标题】:How can I speed up my rate of sending event to AzureEventHub?如何加快向 AzureEventHub 发送事件的速度?
【发布时间】:2021-06-04 06:49:14
【问题描述】:

我正在使用这个简单的 foreach 循环将事件发送到 Azure EventHub,问题是它看起来很慢并且只发送 1-2 个事件/秒,所以我觉得我一定错过了一些东西,我怎样才能加快速度?我是异步发送它们还是我做错了什么?

 if (!apiInputPutCalendarService.TimeSlotIdsToClose.Any())
                {
                    return new BadRequestObjectResult(new
                    {
                        Status = "NOK",
                        Error = "There are no timeslots to close",
                        Result = ""
                    });
                }
                else
                {
                    foreach (String calendarTimeSlotId in apiInputPutCalendarService.TimeSlotIdsToClose)
                    {
                        EventHubCDSSoftDeleteTriggerModel scanMsg = new EventHubCDSSoftDeleteTriggerModel
                        {
                            TimeSlotId = calendarTimeSlotId
                        };

                        var scanMessageJsonString = JsonConvert.SerializeObject(scanMsg);

                        await EventHubHelper.SendEventToEventHubAsync(_eventHubClient, scanMessageJsonString);

                        log.LogInformation($"Message: {scanMessageJsonString} sent!");
                    }
                }

这是一条如此简单的小消息,我预计每秒至少要发送 100 条消息

我在这里错过了什么?

【问题讨论】:

  • 从变量命名来看,您使用的是旧版事件中心 SDK。我建议升级到当前一代的软件包Azure.Messaging.EventHubs,这就是彼得在下面的答案中所展示的。
  • 除了 Peter 关于批量发布的建议之外,您可能还需要考虑您正在使用的 throughput units 的数量以及应用于每个事件中心层的 quotas。这些共同限制入口和出口流量,并可能导致大负载或非常频繁的发布受到限制。

标签: c# azure foreach azure-eventhub


【解决方案1】:

那么,您是否测量了日志记录和序列化以及对TimeSlotIdsToClose 的调用的影响?另外,我不会一一发送消息,而是分批发送消息:

await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
        {
            // Create a batch of events 
            using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();

            // Add events to the batch. An event is a represented by a collection of bytes and metadata. 
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("First event")));
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Second event")));
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Third event")));

            // Use the producer client to send the batch of events to the event hub
            await producerClient.SendAsync(eventBatch);
            Console.WriteLine("A batch of 3 events has been published.");
        }

(source)

请注意,上面的代码不会重用EventHubProducerClient,这是推荐的,您不应忽略TryAdd 的结果,因为它会告诉您是否可以将消息添加到批处理中(有每个批次的基于大小的限制)。

另外,因为我不知道你的代码:

在应用程序的整个生命周期内,每种事件中心客户端类型都可以安全地缓存和用作单例,这是定期发布或读取事件时的最佳实践。

(Source)

【讨论】:

  • 我建议避免显示示例,因为它展示了客户端生命周期的反模式并忽略了TryAdd 的结果,这可能导致数据丢失。我建议使用 publishing samples 作为模型,直到我们能够修复快速入门。
  • 我知道,但我是直接从文档中得到的 ;-) 主要收获是使用批处理和重用客户端。
  • 是的,这是一个非常公平的观点。快速入门指南完全错误,我们仍在与拥有它们的人合作以纠正问题。
  • 分批发送产生了巨大的影响我还添加了多个发件人现在事情真的很顺利
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多