@KafkaListener每次都从头开始阅读

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@KafkaListener每次都从头开始阅读相关的知识,希望对你有一定的参考价值。

我使用以下示例来使用spring Kafka使用者来阅读消息。我的用例要求每次生成消息时,监听器每次都从头开始读取。

@KafkaListener(
    id = "grouplistener",
    topicPartitions = { 
        @TopicPartition(
            topic = "mycompactedtopic", partitionOffsets = @PartitionOffset(partition = "0", initialOffset = "0")
        )
    }
)

public void onReceiving(
    String payload, @Header(KafkaHeaders.OFFSET) Integer offset,
    @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
    @Header(KafkaHeaders.RECEIVED_TOPIC) String topic
) {
    log.info(
        "Processing topic = {}, partition = {}, offset = {}, payload= {}",
        topic, partition, offset, payload
    );
}

我似乎只能在应用程序启动时从头开始阅读,然后它通常会消耗前面的消息。

有没有办法强迫它每次都开始寻求开始?

答案

使用带有1个分区的压缩主题来保存配置列表。然后需要通过休息终点调用它,它应该显示完整的唯一配置列表

你应该实现这个的方法是使用Kafka Streams和KTable并在你的REST层后面建立interactive queries。不是需要回放自身以获得最新状态的系统的标准消费者。

Kafka Connect框架中已经存在一个示例,它具有配置主题,并且您只能访问GET /connectors/name/config的最新值,并且只有在重新启动它或扩展到更多实例时,它才会再次消耗所有消息。 Schema Registry也是一个例子,它存储了_schemas主题中所有模式的内部Hashmap,并具有用于读取,插入和删除的REST API。

实质上,当您获得给定密钥的新配置时,您可以使用全新的“替换”给定密钥的旧值,或者以某种方式将旧值“合并”为新数据。

另一答案

我认为您应该尝试编写ConsumerSeekAware Listener,并在每次阅读消息时寻求0偏移。听起来像疯狂的解决方法,但它可能会有所帮助。希望对你有帮助 :-)

class Listener implements ConsumerSeekAware {

 private final ThreadLocal<ConsumerSeekCallback> seekCallBack = new ThreadLocal<>();

   ----Override all methods that are needed----

@KafkaListener(...)
    public void listen(@Payload String message) {

            this.seekCallBack.get().seek(topic, partition, 0);
        }
    }
}
另一答案

@ Nimo1981所以这是一个使用普通Java的实现。我不确定它是否符合您的需求。所以基本上我提交0的偏移量,(意思是,即使我从Kafka主题中读到,我也会回到开头的偏移量。)我不确定你是否考虑过这个实现但请告诉我这是不是是你在找什么

省略CommitCountObj。这不是你的需要。所以默认情况下,offsetMap会有这样的下一个偏移记录,

offsetMap.put(new TopicPartition(record.topic(),record.partition()),new OffsetAndMetadata(record.offset()+ 1,“some commit success message”));

但是对于你的用例,我进行了一些修改,当消费者没有重新启动时,它运行良好

offsetMap.put(new TopicPartition(record.topic(),record.partition()),new OffsetAndMetadata(0,“no commit done”));

public class KafkaConsumerClass {

    private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(KafkaConsumerClass.class);
    private CommitCountClass commitCountobj = new CommitCountClass();

    public Consumer<String, List<FeedBackConsumerClass>> createConsumer() {
        Map<String, Object> consumerProps = new HashMap<String, Object>();
        consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:7070,localhost:7072");
        consumerProps.put(ConsumerConfig.CONNECTIONS_MAX_IDLE_MS_CONFIG, 50000);
        consumerProps.put(ConsumerConfig.CLIENT_ID_CONFIG, "first_group-client1");
        // consumerProps.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
        consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "first_group");
        // consumerProps.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, KafkaConsumerInterceptor.class);
        consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
        consumerProps.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 15000);
        consumerProps.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        consumerProps.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 1500);
        consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        return new KafkaConsumer<String, List<FeedBackConsumerClass>>(consumerProps);
    }

    public void consumeRecord() {
        log.info("Coming inside consumer consumer");
        ArrayList<String> topicList = new ArrayList<String>();
        topicList.add("topic1");
        commitCountobj.setCount(0);
        Consumer<String, List<FeedBackConsumerClass>> kafkaConsumer = createConsumer();
        kafkaConsumer.subscribe(topicList);
        log.info("after subscribing");

        Map<TopicPartition, OffsetAndMetadata> offsetMap = new HashMap<>();

        while (true) {

            ConsumerRecords<String, List<FeedBackConsumerClass>> recordList = kafkaConsumer.poll(Long.MAX_VALUE);
            // kafkaConsumer.seekToBeginning(kafkaConsumer.assignment());

            log.info("Inside while loop:" + recordList);
            if (!recordList.isEmpty()) {
                recordList.forEach(record -> {
                    int i = 0;
                    System.out.println(record.toString());
                    // we can make the call to the API here
                    // call the db here or any API and process the record
                    // then call the code to commit
                    // since the commit is switched off, it becomes a developers responsibility to do the auto commit
                    offsetMap.put(new TopicPartition(record.topic(), record.partition()),
                            new OffsetAndMetadata(0, "no metadata/offset commited"));
                    // here we are incrementing the offsetMap so that we are making sure we are storing the
                    // next set of offsets in the map
                    if (commitCountobj.getCount() % 1000 == 0) {
                        kafkaConsumer.commitAsync(offsetMap, new OffsetCommitCallback() {

                            @Override
                            public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets,
                                    Exception exception) {
                                // TODO Auto-generated method stub
                                if (exception != null) {
                                    // retry it now with a sync
                                    // possibility of error occuring here as well
                                    // so capture the exception and exit the consumer gracefully
                                    kafkaConsumer.commitSync();
                                    log.error(exception.getMessage());
                                }
                            }
                        });
                    }
                    commitCountobj.setCount(i++);
                });
            }

        }
    }

}

以上是关于@KafkaListener每次都从头开始阅读的主要内容,如果未能解决你的问题,请参考以下文章

AVPlayer SeekToTime 不工作。每次从头开始

不在电子表格中从头开始写入。 Excel VBA。

Kafka Kstream 和 Spring @KafkaListener 有何不同?

Deep Learning基础--理解LSTM网络

如何从头开始实施关联规则分析或市场篮子分析?

从头开始创建自己的Vue.js—第2部分(虚拟DOM基础)