我面临Kafka Java消费者API的一些问题。
我在一个主题中有5个分区,所有消息都在分区5中。现在,尝试创建一个包含4个线程的高级消费者。看起来,由于消息在分区5中,如果线程数小于5,我就无法使用消息。如果线程数为5,我可以使用消息。
但文件说,
如果你有比你有线程更多的分区,一些线程会 从多个分区接收数据
这种方式不起作用。我错过了任何配置吗?
代码: -
public KafkaConsumerGroup(final MessagingApplicationContext messaingCtx, final MessageProcessor msgProcessor) {
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(createConsumerConfig());
this.topic = messaingCtx.getKafkaConfig().getString(TOPIC_STR);
this.readerService = messaingCtx.getRederService();
this.msgProcessor = msgProcessor;
}
public void run(final int threadsCount) {
final Map<String, Integer> topicsMap = new HashMap<String, Integer>();
topicsMap.put(topic, threadsCount);
final Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicsMap);
final List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
int threadNumber = 0;
for (final KafkaStream<byte[], byte[]> stream : streams) {
this.readerService.submit(new KafkaConsumer(stream, threadNumber, msgProcessor));
threadNumber++;
}
}
private static ConsumerConfig createConsumerConfig() {
final Properties props = new Properties();
props.put("zookeeper.connect", "********");
props.put("group.id", "testGrp");
props.put("zookeeper.session.timeout.ms", 1000);
props.put("zookeeper.sync.time.ms", 200);
props.put("auto.commit.interval.ms", 1000);
props.put("auto.offset.reset", "smallest");
props.put("consumer.timeout.ms", -1);
props.put("fetch.message.max.bytes", 4194304);
props.put("rebalance.backoff.ms", 1000);
props.put("rebalance.max.retries", 1);
return new ConsumerConfig(props);
}