【发布时间】:2021-02-18 19:31:12
【问题描述】:
我正在尝试在 spring-boot 中实现一个 Kafka 重试使用者,并使用 SeekToCurrentErrorHandler 进行重试。
我已将退避策略设置为重试 5 次。
我的问题是,假设第一次重试,异常是“数据库不可用”,第二次尝试 db 可用,但在另一个步骤出现另一个故障,如超时,在这种情况下,重试计数回到零并重新开始,或者将继续尝试仅从第一次重试中剩余的尝试。
我的要求是每次异常与之前抛出的异常不同时将重试计数重置为零。
如何在 Kafka 中实现这一点?
这是我的消费者配置。
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Anky> kafkaRetryListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, Anky> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.getContainerProperties().setAckMode(AckMode.MANUAL_IMMEDIATE);
SeekToCurrentErrorHandler errorHandler = new SeekToCurrentErrorHandler((record, exception) -> {
System.out.println(
"RetryPolicy** limit has been exceeded! You should really handle this better." + record.key());
}, new FixedBackOff(0L, 5L));
errorHandler.addNotRetryableException(IllegalArgumentException.class);
errorHandler.setCommitRecovered(true);
factory.setErrorHandler(errorHandler);
// to keep the consumers alive the failure gets reported to broker so that
// consumers remain alive
factory.setStatefulRetry(true);
factory.setConcurrency(2);
return factory;
}
@KafkaListener(topics = "${retry.topic}", groupId = "${consumer.groupId}", containerFactory = "kafkaRetryListenerContainerFactory")
public void onRetryMessage(ConsumerRecord<String, Anky> record, Acknowledgment acknowledgment)
throws SystemException {
LOGGER.debug("are you here in retry?***");
//process message has both DB calls as well as rest calls so it can fail due to a database error and can also fail due to HTTP failure
processMessage(record, acknowledgment, false, offerIntakeContext);
}
}
【问题讨论】:
标签: spring-boot apache-kafka kafka-consumer-api spring-kafka