我是kafka的新手,一直在尝试实现消费者。以下是我的情况
尽管auto.offset.commit=earliest
将使用消息,但将使用发布到该主题的所有消息。 我只想使用在消费者不满意时发布的那些消息。
var options = new KafkaOptions(new Uri(kafkaUri));
var router = new BrokerRouter(options);
var consumer = new Consumer(new ConsumerOptions(kafkaTopic, router));
var offset = consumer.GetTopicOffsetAsync(kafkaTopic, 100000).Result;
var t = from x in offset select new OffsetPosition(x.PartitionId, x.Offsets.Max());
consumer.SetOffsetPosition(t.ToArray());
foreach (Message msg in consumer.Consume()) {
string kafkaResponse = System.Text.Encoding.UTF8.GetString(msg.Value);
Console.WriteLine("PickList Json : " + kafkaResponse);
offsetCommitRequest.Offset = msg.Meta.Offset;
offsetCommitRequest.PartitionId = msg.Meta.PartitionId;
offsetCommitRequest.Topic = kafkaTopic;
offsetCommitRequest.Metadata = "CommitOffset";
var offsetCommitResponse = await _kafkaPublishService.SetOffsetvalue(kafkaUri, kafkaTopic, consumerGroup, offsetCommitRequest);
}
答案 0 :(得分:0)
您应该重新启动使用者而不设置任何手动偏移量。 意味着您不应该这样做:
var offset = consumer.GetTopicOffsetAsync(kafkaTopic, 100000).Result;
var t = from x in offset select new OffsetPosition(x.PartitionId, x.Offsets.Max());
consumer.SetOffsetPosition(t.ToArray());
您的消费者将从上次提交的地方赶上
Yannick