例如,一组中有两个消费者。 两个消费者必须从主题中获得不同的消息。
我使用如下,但似乎不起作用。两位消费者从主题中获取所有消息。
std::string topic_str = "sample";
std::string errstr;
int32_t partition = 0;
int64_t start_offset = RdKafka::Topic::OFFSET_END;
RdKafka::Conf *conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
RdKafka::Conf *tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
//...omit...
conf->set("broker.version.fallback", "0.8.2.2", errstr);
conf->set("group.id", "group_001", errstr);
//...omit...
RdKafka::Consumer *consumer = RdKafka::Consumer::create(conf, errstr);
RdKafka::Topic *topic = RdKafka::Topic::create(consumer, topic_str, tconf, errstr);
RdKafka::ErrorCode resp = consumer->start(topic, partition, start_offset);
//...omit...
while (1) {
//...omit...
RdKafka::Message *msg = consumer->consume(topic, partition, 1000);
//print message and offset
}
答案 0 :(得分:1)
librdkafka只支持在Kafka 0.9中添加的基于代理的新平衡消费者群体(KafkaConsumer类)。 (在Kafka 0.8上平衡的消费者群体基于Zookeeper,并且仅在官方Scala客户端中实现。)
您还使用了没有任何形式的平衡消费者支持的传统低级别消费者(消费者类)。
我建议将Kafka群集升级到0.9(或0.10!)并更改代码以改为使用新的KafkaConsumer类。
此处示例: https://github.com/edenhill/librdkafka/blob/master/examples/rdkafka_consumer_example.cpp