我正在使用EventHubProducerClient类在 Azure 事件中心发布消息。当我尝试发送大量数据时,比如一条一条地发送 1000 条消息,这个测试用例通过了,但我在事件中心仪表板上只看到 400-500(随机数)。这是代码。
public async Task TestEventHubPublishMessagesSequentially()
{
var eventHubConnectionString = "XXXXX.CONNECTION STRING.XXXX";
var eventHubName = "XXXXX.EVENT HUB NAME.XXXX";
var messsage = "I am a message"
int tobePublishCount = 2000;
int publishCount = 0;
for (int i = 0; i < tobePublishCount; i++)
{
var completeMessage = $"Index:{i}-message";
var success = await PublishMessageSequentially(eventHubConnectionString, eventHubName, completeMessage);
if (success)
{
publishCount++;
}
}
Assert.Equal(2000, publishCount);
}
public async Task<bool> PublishMessageSequentially(string eventHubConnectionString, string eventHubName, string messageToPublish)
{
await using (var producerClient = new EventHubProducerClient(eventHubConnectionString, eventHubName))
{
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
var data = new Azure.Messaging.EventHubs.EventData(Encoding.UTF8.GetBytes(messageToPublish));
if (eventBatch.TryAdd(data))
{
await producerClient.SendAsync(eventBatch);
return true;
}
}
return false;
}
但是,如果我尝试在列表中发布它们,我可以在事件中心看到所有数据。
List<string> totalMessages = new List<string>();
for (int i = 0; i < tobePublishCount; i++)
{
totalMessages.Add($"Index: {i}, I am a message");
}
await PublishMessageTogether(eventHubConnectionString, eventHubName, completeMessage);
public async Task PublishMessageTogether(string eventHubConnectionString, string eventHubName, List<string> messagesToPublish)
{
await using (var producerClient = new EventHubProducerClient(_eventHubsConnectionString, _eventHubName))
{
List<Azure.Messaging.EventHubs.EventData> azureEventData = new List<Azure.Messaging.EventHubs.EventData>>();
foreach(var messageToPublish in messagesToPublish)
{
var eventData = new Azure.Messaging.EventHubs.EventData(Encoding.UTF8.GetBytes(message));
azureEventData.Add(eventData);
}
await producerClient.SendAsync(azureEventData);
}
return publishCount;
}
}
我不知道出了什么问题,唯一的区别是,我可以看到我正在一次又一次地创建 EventHubProducerClient 的新对象,但我也在处理它。我认为事件中心旨在消耗大量数据(每秒数百万)。我的用例是我必须一一发布。