简体   繁体   English

一个Spring的KafkaConsumer侦听器可以侦听来自同一分区的多个消息吗?

[英]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.

相关问题 侦听器在Spring JMS中侦听消息后无法从主题中发出消息 - Unable to deque messages from Topic after the listener listens the messages in spring jms 如何配置侦听器以处理Rabbitmq Spring中来自同一队列的多个交换的消息 - How to configure a listener to handle messages from multiple exchanges for the same queue in rabbitmq spring Spring 具有多个分区和多个侦听器的 Kafka - Spring Kafka with multiple Partition & multiple listener 如果代理地址不正确、主题中没有消息或主题丢失,如何为 KafkaConsumer 设置超时? - How can I set timeout for KafkaConsumer if the broker's address is incorrect, there are no messages in topic, or the topic is missing? 如何添加侦听多个按钮的动作侦听器 - How to add action listener that listens to multiple buttons KafkaConsumer.commitSync/commitAsync 是否可以提交未分配分区的偏移量 - Can an offset of an unassigned partition be committed by KafkaConsumer.commitSync/commitAsync Kafka 9 KafkaConsumer multiple 似乎没有并行处理消息 - Kafka 9 KafkaConsumer multiple doesn't seem to process messages in parallel KafkaListener 监听多个主题时,能否优先考虑单个主题? - Can you give priority to a single topic when KafkaListener listens to multiple topics? 由于无法设置0并发,如何在spring amqp中为同一队列在一个服务器上设置一定数量的侦听器,而在另一服务器上没有侦听器? - How to set certain number of listener on one server and no listener on other server for same queue in spring amqp as we can not set 0 concurrency? spring rabbitmq - 同时消费多条消息 - spring rabbitmq - consume multiple messages at the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM