kafka反序列化错误处理

本文介绍如何在处理SpringKafka的ConcurrentKafkaListenerContainerFactory中,当遇到序列化失败时自动定位错误、跳过记录并更新消费者偏移量,以避免日志满磁盘问题。
摘要由CSDN通过智能技术生成

当消费者发生反序列化失败时会导致消费者偏移量不会向后移动,而且海量的错误日志会将磁盘写满,因此需要针对此类错误进行手动处理

    ConcurrentKafkaListenerContainerFactory<String, MyObject> factory =
            new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory);

        factory.setCommonErrorHandler(new CommonErrorHandler(){
            @Override
            public void handleOtherException(Exception thrownException, Consumer<?, ?> consumer, MessageListenerContainer container, boolean batchListener) {
                String s = thrownException.getMessage().split("Error deserializing key/value for partition ")[1].split(". If needed, please seek past the record to continue consumption.")[0];
                String topics = s.split("-")[0];
                int offset = Integer.parseInt(s.split("offset ")[1]);
                int partition = Integer.parseInt(s.split("-")[1].split(" at")[0]);

                TopicPartition topicPartition = new TopicPartition(topics, partition);
                consumer.seek(topicPartition, offset + 1);
                consumer.commitAsync();
                LOG.error("Skipping " + topics + "-" + partition + " offset " + offset);
            }
        });

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值