简体   繁体   English

如何让 Kafka 消费者从特定主题分区 Spring 引导读取

[英]How to make Kafka consumer reads from specific topic partition Spring Boot

I'm kind of new to Kafka and Spring Boot and trying to make my application to read from a specific partition of the topic.我对 Kafka 和 Spring 启动有点陌生,并试图让我的应用程序从主题的特定分区中读取。

 @KafkaListener(id = "singleLnr", groupId = "${kafka.consumer.group.id}",containerFactory = "singleFactory", topicPartitions = @TopicPartition(topic = "${kafka.topic.singleAttendance}", partitions = {"0"}))
public void consume2(ConsumerRecord attendanceInfo) {
    System.out.println(attendanceInfo);
}

Single factory code单厂代码

@Bean(name = "singleFactory")
public KafkaListenerContainerFactory singleFactory() {
    ConcurrentKafkaListenerContainerFactory<String, Map<String, String>> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setBatchListener(false);
    factory.setMessageConverter(converter());
    return factory;
}

This are also my consumer factory configuration这也是我的消费者工厂配置

 @Bean(name = "consumerFactory")
public ConsumerFactory<String, Map<String, String>> consumerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBootstrapAddress);
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
    props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 1000);
    props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 60000);
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaConsumerGroupId);
    return new DefaultKafkaConsumerFactory<>(props);
}

When I'm trying to run the program, it gives me a Error当我尝试运行程序时,它给了我一个错误

Offset commit failed on partition single.attendance-0 at offset 308: The coordinator is not aware of this member.在偏移量 308 处的分区 single.attendance-0 上的偏移量提交失败:协调器不知道该成员。

and warning和警告

failed: Commit cannot be completed since the group has already rebalanced and assigned the partitions to another member. failed:由于组已经重新平衡并将分区分配给另一个成员,因此无法完成提交。 This means that the time between subsequent calls to poll() was longer than the configured max.poll.interval.ms, which typically implies that the poll loop is spending too much time message processing.这意味着后续调用 poll() 之间的时间比配置的 max.poll.interval.ms 长,这通常意味着轮询循环花费了太多时间处理消息。 You can address this either by increasing max.poll.interval.ms or by reducing the maximum size of batches returned in poll() with max.poll.records.您可以通过增加 max.poll.interval.ms 或通过使用 max.poll.records 减少 poll() 返回的批次的最大大小来解决这个问题。

How can I make my consumer to read from specific partition?如何让我的消费者从特定分区读取? Could you please give at least a hint.请你至少给一个提示。

Kafka is assigning consumer for each partition by itself. Kafka 自己为每个分区分配消费者。 No need to configure it in @KafkaListener in this implementation.在此实现中无需在 @KafkaListener 中配置它。

@KafkaListener(id = "singleLnr", groupId = "${kafka.consumer.group.id}",containerFactory = "singleFactory", topics = "${kafka.topic.singleAttendance}")
    public void consume2(ConsumerRecord attendanceInfo) {
        System.out.println(attendanceInfo);
    }

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

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