@KafkaListener(id = "eventConsumer", topics = "perception_event", groupId = "defaultConsumerGroup", containerFactory = "kafkaListenerContainerFactory")
public void consume(List<ConsumerRecord<String, String>> consumerRecordList) {
.......
}
1.kafka批量消费消息,使用containerFactory 监听消费失败消息
/**
* 消费失败消息最大重试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://blog.csdn.net/summer07071126/article/details/127317852
版权归原作者 疑似地上冰霜 所有, 如有侵权,请联系我们删除。
版权归原作者 疑似地上冰霜 所有, 如有侵权,请联系我们删除。