Kafka - Multiple Consumers From Same Group Assigned Same Partition
我刚刚开始学习Kafka和Nodejs。我写了一个如下的消费者
B
输出
B.Length
// consumer.js
const kafka = require('kafka-node');
var client = new kafka.Client('localhost:2181');
var topics = [{
topic: 'topic-4'
}];
var options = {
groupId: 'kafka-node-group-2',
autoCommit: true,
fetchMaxWaitMs: 1000,
fetchMaxBytes: 1024 * 1024,
encoding: 'buffer'
};
var consumer = new kafka.HighLevelConsumer(client, topics, options);
// consumer.payloads has only one entry
console.log('Topic', consumer.payloads[0].topic);
console.log('Group', consumer.options.groupId);
console.log('Assigned Partition:', consumer.payloads[0].partition);
有四个分区。
Topic topic-4
Group kafka-node-group-2
Assigned Partition: 0
修改
我已按如下方式使用topic-4
。
./desc_topic.sh topic-4
Topic:topic-4 PartitionCount:4 ReplicationFactor:1 Configs:
Topic: topic-4 Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: topic-4 Partition: 1 Leader: 2 Replicas: 2 Isr: 2
Topic: topic-4 Partition: 2 Leader: 0 Replicas: 0 Isr: 0
Topic: topic-4 Partition: 3 Leader: 2 Replicas: 2 Isr: 2
生产者正在发送100条消息,如下所示。这就是我知道分配的分区(不是来自ConsumerGroup
对象的分区)的方法。
var options = {
host: 'localhost:2181', // zookeeper host omit if connecting directly to broker (see kafkaHost below)
groupId: 'Group-1',
sessionTimeout: 15000,
// // An array of partition assignment protocols ordered by preference.
// // 'roundrobin' or 'range' string for built ins (see below to pass in custom assignment protocol)
protocol: ['roundrobin']
};
var consumer = new kafka.ConsumerGroup(options, ['topic-4']);
当我运行两个这样的使用者实例(相同的主题和组)时,只有其中一个接收来自分区0的所有内容。这不是问题吗?
这是生产者代码。
consumer
答案 0 :(得分:2)
这是一个已知问题。我也遇到过。如果您使用的Kafka版本比该修复程序的发布版本要新,则可能值得重新检查并重新打开此问题。