在Kafka的单个消费者中消耗多个主题



kafka可以在具有多个主题的单个消费者上使用任何方法?

这是我的消费者代码,它可以与单个主题

一起工作
let consumer = new Consumer(
        client,
        [{ topic: '1234', offset: 0, partition: 0 }],
        {
            autoCommit: true,
            fetchMaxWaitMs: 1000,
            fetchMaxBytes: 1024 * 1024,
            encoding: 'utf8',
            fromOffset: false
        }
    );
    consumer.on('message', async function (message: any) {
        console.log(
            'kafka-> ',
            message.value
        );
    })

这显然是一个列表,您应该能够在消费者中添加多个主题。

https://www.npmjs.com/package/kafka-node#consumer说你可以如下做。(明智的明智,不分配分区,让Zookeeper处理它(

var kafka = require('kafka-node'),
Consumer = kafka.Consumer,
client = new kafka.KafkaClient(),
consumer = new Consumer(
    client,
    [
        { topic: 't', partition: 0 }, { topic: 't1', partition: 1 }
    ],
    {
        autoCommit: false
    }
);

最新更新