我有一个C#视窗服务,它安装在3个不同的服务器。所有这3个服务都注册到同一个事件中心,该中心有8个分区。在某种程度上,这3台服务器是我的消费者。启动windows服务后,可以在不同的服务器上初始化多个分区。据我观察,这种初始化总是随机的。
假设初始化场景如下:
Server1 :分区0,1,2
Server2 :分区3,4
Server3 :分区5、6、7
这个初始化是通过下面的代码实现的:
public Task OpenAsync(PartitionContext context)
{
Console.WriteLine($"SimpleEventProcessor initialized. Partition: '{context.PartitionId}'");
return Task.CompletedTask;
} 我的要求是,即使有多个分区,我也希望每个使用者/服务器只初始化一个分区。有没有办法限制所有分区都被初始化?
我想的是,在构建功能中可能会有一些计数,并告诉已经初始化的分区的数量。因此,我可以使用来检查该值是否为"1",然后跳过初始化其他分区。但我找不到任何这样的功能。
因为我有3个服务器,所以我只想初始化3个分区,每个服务器一个分区。其余的5个将是免费的,不会收到任何消息。这些可以在将来服务器数量增加时使用。
这种情况可能吗?
发布于 2020-02-11 01:56:53
如果这是您的需求,那么您使用的SDK是错误的。Processor Host SDK被设计为从所有分区消费,它不会让您轻松地约束插入分区管理器。
我建议你使用低级API,这样你就可以在你喜欢的任何分区上创建独立的接收器。通过这种方式,您可以更好地控制分区管理。
var receiver = eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, PartitionReceiver.EndOfStream);
// Receive a maximum of 100 messages in this call to ReceiveAsync
var ehEvents = await receiver.ReceiveAsync(100);
// ReceiveAsync can return null if there are no messages
if (ehEvents != null)
{
// Since ReceiveAsync can return more than a single event you will need a loop to process
foreach (var ehEvent in ehEvents)
{
// Decode the byte array segment
var message = UnicodeEncoding.UTF8.GetString(ehEvent.Body.Array);
// Load the custom property that we set in the send example
var customType = ehEvent.Properties["Type"];
// Implement processing logic here
}
} https://stackoverflow.com/questions/60145618
复制相似问题