繁体   English   中英

spring-kafka 消费者批处理错误处理与 spring 引导版本 2.3.7

[英]spring-kafka consumer batch error handling with spring boot version 2.3.7

我正在尝试执行 spring kafka 批处理错误处理。 首先我有几个问题。

  1. 侦听器和容器错误处理程序有什么区别,这两类错误有哪些?

  2. 您能否帮助一些示例以更好地理解?

这是我们的设计:

  1. 每隔一定时间轮询一次
  2. 以批处理模式消费消息
  3. 基于key推送到本地缓存(应用缓存)(避免重复事件)
  4. 批处理完成后,将所有值一一推送到另一个主题。
  5. 操作 3 完成后清除缓存并手动确认偏移量。

这是我的错误处理计划:

public ConcurrentKafkaListenerContainerFactory<String, String> myListenerPartitionContainerFactory(String groupId) {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory(groupId));
        factory.setConcurrency(partionCount);
        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL);
        factory.getContainerProperties().setIdleBetweenPolls(pollInterval);
        factory.setBatchListener(true);

        return factory;
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> myPartitionsListenerContainerFactory() 
    {
        return myListenerPartitionContainerFactory(groupIdPO);
    }


@Bean
public RecoveringBatchErrorHandler(KafkaTemplate<String, String> errorKafkaTemplate) {
    DeadLetterPublishingRecoverer recoverer =
            new DeadLetterPublishingRecoverer(errorKakfaTemplate);
    RecoveringBatchErrorHandler errorHandler =
            new RecoveringBatchErrorHandler(recoverer, new FixedBackOff(2L, 5000)); // push error event to the error topic
}


@KafkaListener(id = "mylistener", topics = "someTopic", containerFactory = "myPartitionsListenerContainerFactory"))
public void listen(List<ConsumerRecord<String, String>> records, @Header(KafkaHeaders.MESSAGE_KEY) String key, Acknowledgement ack) {
    Map hashmap = new Hashmap<>();
    records.forEach(record -> {
        try {
            //key will be formed based on the input record - it will be id.
            hashmap.put(key, record);  
        }
        catch (Exception e) {
            throw new BatchListenerFailedException("Failed to process", record);
        }
         
    });
    // Once success each messages to another topic.
    try {
      hashmap.forEach( (key,value) -> {  push to another topic })
      hashmap.clear();
      ack.acknowledge();
    } catch(Exception ex) {
        //handle producer exceptions
    }
}

方向是好的还是需要做任何改进? 还需要实现什么类型的容器和侦听器处理程序?

@Gary Russell ..你能帮忙吗?

侦听器错误处理程序适用于错误处理程序可以向发送者返回有意义的回复的请求/回复情况。

您需要抛出异常来触发容器错误处理程序,并且您需要在原始批处理中的索引中知道以告诉它哪条记录失败。

如果您正在使用这样的手动确认,您可以使用 nack() 方法来指示哪个记录失败(并且在这种情况下不要抛出异常)。

暂无
暂无

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

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