[英]Event Hub Consumer in Service Fabric
我正在嘗試讓服務結構始終從 azure 事件中心提取消息。 我似乎已經把所有東西都連接好了,但請注意我的消費者只是停止拉動事件。
我有一個集線器,其中包含我推送的數千個事件。 為集線器配置了 1 個分區,並讓我的服務結構服務也只有 1 個分區以簡化調試。
服務啟動,創建 EventHubClient,從那里使用它來創建 PartitionReceiver。 接收者被傳遞給一個“EventLoop”,該“EventLoop”在調用receiver.ReceiveAsync 時進入“無限”。 EventLoop 的代碼如下。
我觀察到的是第一次通過循環我幾乎總是收到 1 條消息。 第二次我收到了 103 到 200 條消息。 在那之后,我沒有收到任何消息。 似乎如果我重新啟動服務,我會再次收到相同的消息 - 但那是因為當我重新啟動服務時,我讓它在 stream 的開頭重新啟動。
預計它會繼續運行,直到我的 2000 條消息被消耗,然后它會等待我(偶爾輪詢)。
我需要對 Azure.Messaging.EventHubs 5.3.0 package 做一些具體的事情來讓它繼續拉事件嗎?
//Here is how I am creating the EventHubClient:
var connectionString = "something secret";
var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString)
{
EntityPath = "NameOfMyEventHub"
};
try
{
m_eventHubClient = EventHubClient.Create(connectionStringBuilder);
}
//Here is how I am getting the partition receiver
var receiver = m_eventHubClient.CreateReceiver("$Default", m_partitionId, EventPosition.FromStart());
//The event loop which the receiver is passed to
private async Task EventLoop(PartitionReceiver receiver)
{
m_started = true;
while (m_keepRunning)
{
var events = await receiver.ReceiveAsync(m_options.BatchSize, TimeSpan.FromSeconds(5));
if (events != null) //First 2/3 times events aren't null. After that, always null and I know there are more in the partition/
{
var eventsArray = events as EventData[] ?? events.ToArray();
m_state.NumProcessedSinceLastSave += eventsArray.Count();
foreach (var evt in eventsArray)
{
//Process the event
await m_options.Processor.ProcessMessageAsync(evt, null);
string lastOffset = evt.SystemProperties.Offset;
if (m_state.NumProcessedSinceLastSave >= m_options.BatchSize)
{
m_state.Offset = lastOffset;
m_state.NumProcessedSinceLastSave = 0;
await m_state.SaveAsync();
}
}
}
}
m_started = false;
}
**編輯,有人問了關於分區數量的問題。 事件中心有一個分區,SF 服務也有一個分區。
打算使用服務結構 state 來跟蹤我在集線器中的偏移量,但這不是現在的問題。
為每個分區創建分區偵聽器。 我得到這樣的分區:
public async Task StartAsync()
{
// slice the pie according to distribution
// this partition can get one or more assigned Event Hub Partition ids
string[] eventHubPartitionIds = (await m_eventHubClient.GetRuntimeInformationAsync()).PartitionIds;
string[] resolvedEventHubPartitionIds = m_options.ResolveAssignedEventHubPartitions(eventHubPartitionIds);
foreach (var resolvedPartition in resolvedEventHubPartitionIds)
{
var partitionReceiver = new EventHubListenerPartitionReceiver(m_eventHubClient, resolvedPartition, m_options);
await partitionReceiver.StartAsync();
m_partitionReceivers.Add(partitionReceiver);
}
}
當調用 partitionListener.StartAsync 時,它實際上創建了 PartitionListener,如下所示(實際上比這多一點,但采取的分支是這個:
m_eventHubClient.CreateReceiver(m_options.EventHubConsumerGroupName, m_partitionId, EventPosition.FromStart());
感謝您的任何提示。 將要
你有幾個分區? 我在您的代碼中看不到您如何確保讀取默認使用者組中的所有分區。
您使用PartitionReceiver
而不是使用EventProcessorHost的任何具體原因?
對我來說,SF 似乎非常適合使用事件處理器主機。 我看到已經有一個使用有狀態服務進行檢查點的SF 集成解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.