Node.js Kafka使用者无限循环

时间:2018-08-31 15:08:04

标签: node.js apache-kafka kafka-consumer-api mq

我正在ubuntu 16.04机器上运行kafka_2.11-2.0.0。创建了一个主题,并从命令行界面向其生成了一些消息。

enter image description here

并从命令行启动了使用者,它的使用状况很好。

enter image description here

但是当我像下面那样启动nodejs消费者时,它是无限迭代的。客户代码中缺少什么?

var kafka = require('kafka-node'),
Consumer = kafka.Consumer,
client = new kafka.Client(),
consumer = new Consumer(
    client,
    [
        {topic: 'mytopic', partition: 0}
    ],
    {
        autoCommit: true
    }
);
consumer.on('message', function (message) {
        console.log(message);
});
consumer.on('error', function (err){
        console.log(err);

})
consumer.on('offsetOutOfRange', function (err){
        console.log(err);
        process.exit();
})

这是输出。

{ topic: 'mytopic',
  value: '',
  offset: 0,
  partition: 0,
  highWaterOffset: 3,
  key: '' }
{ topic: 'mytopic',
  value: 'message2',
  offset: 1,
  partition: 0,
  highWaterOffset: 3,
  key: null }
{ topic: 'mytopic',
  value: 'message3',
  offset: 2,
  partition: 0,
  highWaterOffset: 3,
  key: null }
{ topic: 'mytopic',
  value: '',
  offset: 0,
  partition: 0,
  highWaterOffset: 3,
  key: '' }
{ topic: 'mytopic',
  value: '',
  offset: 0,
  partition: 0,
  highWaterOffset: 3,
  key: '' }
{ topic: 'mytopic',
  value: 'message2',
  offset: 1,
  partition: 0,
  highWaterOffset: 3,
  key: null }
{ topic: 'mytopic',
  value: 'message3',
  offset: 2,
  partition: 0,
  highWaterOffset: 3,
  key: null }
{ topic: 'mytopic',
  value: '',
  offset: 0,
  partition: 0,
  highWaterOffset: 3,
  key: '' }
{ topic: 'mytopic',
  value: '',
  offset: 0,
  partition: 0,
  highWaterOffset: 3,
  key: '' }
{ topic: 'mytopic',
  value: 'message2',
  offset: 1,
  partition: 0,
  highWaterOffset: 3,
  key: null }
{ topic: 'mytopic',
  value: 'message3',
  offset: 2,
  partition: 0,
  highWaterOffset: 3,
  key: null }
{ topic: 'mytopic',
  value: '',
  offset: 0,
  partition: 0,
  highWaterOffset: 3,
  key: '' }

1 个答案:

答案 0 :(得分:0)

最后发现kafka新版本2.0.0的问题。所以我移到了以前的版本,现在可以使用了。