@KafkaListener(id = "eventConsumer", topics = "perception_event", groupId = "defaultConsumerGroup", containerFactory = "kafkaListenerContainerFactory")
public void consume(List<ConsumerRecord<String, String>> consumerRecordList) {
.......
}
1.kafka批量消费消息,使用containerFactory 监听消费失败消息 文章来源:https://www.toymoban.com/news/detail-515022.html
/**
* 消费失败消息最大重试15次,存入到死信队列中
*
* @param configurer kafkaConsumerFactory kafkaTemplate
* @return factory
*/
@Bean("kafkaListenerContainerFactory")
public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
ConcurrentKafkaListenerContainerFactoryConfigurer configurer, ConsumerFactory<Object, Object> kafkaConsumerFactory, KafkaTemplate<Object, Object> kafkaTemplate) {
ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
configurer.configure(factory, kafkaConsumerFactory);
//最大重试15次
RetryingBatchErrorHandler retryingBatchErrorHandler = new RetryingBatchErrorHandler(new FixedBackOff(500L, 15L),
createConsumerRecordRecoverer());
factory.setBatchErrorHandler(retryingBatchErrorHandler);
return factory;
}
/**
* 最终消费失败打印日志即可
*/
private ConsumerRecordRecoverer createConsumerRecordRecoverer() {
return (consumerRecord, exception) -> {
log.error("consumer event last fail:{}, exception:{}", SensitiveUtils.phone(consumerRecord.toString()), exception.toString());
};
}
2.使用RetryingBatchErrorHandler 指定批量消费时失败消息的重试次数和时间,如果不是批量消费,则使用RetryingErrorHandler来指定重试次数和间隔时间文章来源地址https://www.toymoban.com/news/detail-515022.html
到了这里,关于kafka消费失败重试机制的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!