kafka-node consumer-group不消耗来自所有分区的消息

时间:2018-03-08 22:01:27

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

所以,我正在尝试听一个有3个分区的kafka客户端,主题名为=" some_test_topic&#34 ;;

我已经编写了以下代码来使用来自最新偏移量的消息。

var async = require('async');
var ConsumerGroup = require('kafka-node').ConsumerGroup;
var logger = winston.logstart();

var buildConsumerOptions = {
    host: 'example.com:2181',
    autoCommit: false,
    groupId: 'some_test_topic',
    sessionTimeout: 15000,
    protocol: ['roundrobin'],
    partitioner: 3, // Not sure if kafka-node is using this or not.
    fromOffset: 'earliest' // other options 'none', 'earliest'
};
var buildTopics = ['build_status_events'];
var consumerGroup = new ConsumerGroup(Object.assign({id: 'build_status_events'}, buildConsumerOptions), buildTopics);
var consumerGroup1 = new ConsumerGroup(Object.assign({id: 'build_status_events'}, buildConsumerOptions), buildTopics);
module.exports = {
    listenBuildEventsKafka: function(connections){
        try{
            // var consumerGroup = new ConsumerGroup(Object.assign({id: 'build_status_events'}, buildConsumerOptions), buildTopics);
            consumerGroup.on('error', onError);
            consumerGroup.on('message', onMessage);
            consumerGroup1.on('error', onError);
            consumerGroup1.on('message', onMessage);
            consumerGroup.on('connect', function(){
                logger.info("ConsumerGroup is ready. ");
            });
            consumerGroup1.on('connect', function(){
                logger.info("ConsumerGroup1 is ready. ");
            });
            function onMessage(message){
                var jsonData = JSON.parse(message.value);
                console.log(" -- " +jsonData.buildid+ " Time: " +jsonData.time + " Offset: " +message.offset+ " Partition: " +message.partition);
            }
            function onError (error) {
              logger.error("Some error occured while listening kafka events " +error);
            }
            process.once('SIGINT', function(){
                async.each([consumerGroup, consumerGroup1], function (consumer, callback) {
                    logger.info("Closing the kafka build consumer process");
                    consumer.close(true, callback);
                });
            })
        }catch(error){
            logger.error("Could not connect to kafka events for build " +error);
        }
    }
};

不知何故,当我运行此代码时,它只消耗来自分区0和分区1的事件。不会打印来自partition2的任何事件。

不确定,我还需要配置什么呢。我尝试做足够的谷歌,但找不到任何有用的东西。 此外,我尝试将partitionerType = 1添加到4(逐个)..但没有任何帮助。 谁能在这里帮助我。

0 个答案:

没有答案