简体   繁体   English

无法通过多个使用者从具有单个分区的单个主题读取事件

[英]Not able to read event from single topic that has single partition through multiple consumers

I'm a newbie to Kafka, using Kafka-node. 我是使用Kafka-node的Kafka的新手。 I'm using Kafka for real-time data processing. 我正在使用Kafka进行实时数据处理。 The system has one producer and multiple consumers. 该系统有一个生产者和多个消费者。 I'm able to receive data if any one of the consumers is running at a time but if I run two consumers, only one of them will receive data and there is no data received by another one. 如果有一个使用者同时运行,则可以接收数据,但是如果我运行两个使用者,则只有一个使用者可以接收数据,而另一个使用者没有接收到数据。

Code for kafka producer is: 卡夫卡生产者的代码是:

const config = require('../config');
const logger = require('./logger');
const kafka = require('kafka-node'),
HighLevelProducer = kafka.HighLevelProducer,
client = new kafka.Client(`${config.kafka.host}:${config.kafka.port}`),
producer = new HighLevelProducer(client);

producer.on('ready', () => {
logger.info("Events producer to kafka is ready...");
});

producer.on('error', (err) => {
logger.error("Error while starting kafka producer:" + err.message);
});

const queueEvent = (event, callback) => {
const payloads = [
{ topic: config.kafka.queueName, messages: JSON.stringify(event, null, 
2) },
];
producer.send(payloads, (err, data) => {
if (err) {
logger.error(`Error while producing data: ${err.message}`);
callback(err);
} else {
callback(null, data);
}
});
};

module.exports = {
queueEvent
};

Configuration done for all consumers is same as shown below: 为所有使用者完成的配置如下所示:

const kafka = require('kafka-node');

const logger = require('../common/logger');
const config = require('../common/config');
const eventDao = require('../models/event');
const _ = require('lodash');

const { getDeviceHierarchy } = require('../common/apiUtils');

const options = { autoCommit: true, fetchMaxWaitMs: 1000, fetchMaxBytes: 
1024 * 1024 };

const HighLevelConsumer = kafka.HighLevelConsumer,
client = new kafka.Client(`${config.kafka.host}:${config.kafka.port}`),
consumer = new HighLevelConsumer(
client,
[
{ topic: config.kafka.queueName, partition: 0 }
],
options
);

I'm using docker image of Kafka and below is the settings I've done 我正在使用Kafka的docker image,下面是我完成的设置

docker run -d -p 2181:2181 -p 3030:3030 -p 8081-8083:8081-8083 -p 9581-9585:9581-9585 -p 9092:9092 -e ADV_HOST=localhost -e DISABLE=azure-documentdb,blockchain,bloomberg,cassandra,coap,druid,elastic,elastic5,ftp,hazelcast,hbase,influxdb,jms,kudu,mongodb,mqtt,redis,rethink,voltdb,yahoo,hdfs,jdbc,elasticsearch,s3,twitter -e CONNECT_HEAP=6G -e RUNNING_SAMPLEDATA=0 -e RUNTESTS=0 landoop/fast-data-dev:latest** docker run -d -p 2181:2181 -p 3030:3030 -p 8081-8083:8081-8083 -p 9581-9585:9581-9585 -p 9092:9092 -e ADV_HOST = localhost -e DISABLE = azure-documentdb,区块链,彭博,cassandra,帽,德鲁伊,弹性,弹性5,ftp,hazelcast,hbase,influxdb,jms,kudu,mongodb,mqtt,redis,重新思考,voltdb,雅虎,hdfs,jdbc,elasticsearch,s3,twitter -e CONNECT_HEAP = 6G -e RUNNING_SAMPLEDATA = 0 -e RUNTESTS = 0 landoop / fast-data-dev:latest **

Could you please confirm if the consumers in the multiple consumer scenario belong to the same consumer group? 您能否确认在多个消费者方案中的消费者是否属于同一消费者组?

If they do, then the observed behaviour is correct. 如果这样做,则观察到的行为是正确的。 Let me try and elaborate this a little bit: 让我尝试详细说明一下:

  1. In the scenario you've described, it sounds like both the consumers belong to the same consumer group. 在您描述的场景中,听起来两个消费者都属于同一个消费者组。 In this case, each consumer from the group can consume only from one partition. 在这种情况下,组中的每个使用者只能从一个分区中使用。 As we have only one partition here, the first consumer in line consumes it. 由于这里只有一个分区,因此排队的第一个使用者将使用该分区。

  2. If we have multiple consumer groups, with multiple consumers in each group subscribing to the same topic (with one partition). 如果我们有多个消费者组,则每个组中有多个消费者订阅同一主题(有一个分区)。 In this case, multiple consumers can consume from the same partition. 在这种情况下,多个使用者可以从同一分区进行消费。

I'm not familiar with the programming language you've used but I couldn't find the statement setting the " group.id " property in your Kafka consumer. 我不熟悉您使用的编程语言,但找不到在您的Kafka使用者中设置“ group.id ”属性的语句。 Could you please try setting this in your consumer code/config? 您可以尝试在用户代码/配置中设置此设置吗?

Also, could you check and confirm the version of Kafka you're using and if there is a default value in consumer.properties file in Kafka? 另外,您可以检查并确认您正在使用的Kafka的版本,以及Kafka中的consumer.properties文件中是否有默认值? because from version 0.9.0.0 the group.id property value has been made mandatory and not providing this should throw an error (Check this ticket ). 因为从版本0.9.0.0起,group.id属性值已被强制设置,并且不提供该值将引发错误(请检查此票证 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM