[英]Can a single Spring's KafkaConsumer listener listens to multiple messages from same/one partition?
Provided a topic having n partitions. 提供了具有n个分区的主题。 Is there any way by which Spring's KafkaConsumer listener listens to multiple messages from same/one partition in one go?
Spring的KafkaConsumer侦听器是否可以通过一种方式一次侦听来自相同/一个分区的多个消息?
I tried ConcurrentKafkaListenerContainerFactory
by setting setBatchListener(true);
我通过设置
setBatchListener(true);
尝试了ConcurrentKafkaListenerContainerFactory
setBatchListener(true);
but consumer has started consuming multiple messages from different partitions instead of one. 但是使用者已经开始使用来自不同分区的多个消息,而不是一个。
public class BatchReceiverConfig {
@Value("${kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "batch4");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
// maximum records per batch receive
props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "10");
return props;
}
@Bean
public ConsumerFactory<String, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs());
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setConcurrency(2);
// enable batch listeners
// factory.setBatchListener(true);
return factory;
}
@Bean
public BatchReceiver receiver() {
return new BatchReceiver();
}
}
/* Listener */
@KafkaListener(id = "batch-listener", topics = TOPIC_TEST_BATCH)
public void receive(List<String> data,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) List<Integer> partitions,
@Header(KafkaHeaders.OFFSET) List<Long> offsets) {
LOGGER.info("start of batch receive");
for (int i = 0; i < data.size(); i++) {
LOGGER.info("received message='{}' with partition-offset='{}'", data.get(i),
partitions.get(i) + "-" + offsets.get(i));
// handle message
latch.countDown();
}
LOGGER.info("end of batch receive");
}
假设组中只有一个使用者,则可以将该ConcurrentKafkaListener
concurrencyLevel
设置为n
。
If you use group management, Kafka will assign partitions to your consumers. 如果您使用群组管理,Kafka将为您的使用者分配分区。 Each consumer (or thread when using the concurrency) will get 0, 1, or more partitions, depending on how many consumers and partitions there are.
每个使用者(或使用并发时的线程)将获得0、1或更多分区,具体取决于有多少使用者和分区。
If you want a particular consumer to get messages from only one partition, you have to assign the partitions yourself instead of letting Kafka do the assignment. 如果您希望特定用户仅从一个分区获取消息,则必须自己分配分区,而不是让Kafka进行分配。
If you use the concurrent container; 如果使用并发容器; you would have to assign the same number of partitions as the concurrency, so each thread only gets one partition.
您必须分配与并发相同数量的分区,因此每个线程仅获得一个分区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.