在Spring Cloud Stream Kafka Binder中消费批量时重试最多3次



我在kafka中使用批处理,其中重试在spring cloud stream kafka binder中不支持批处理模式,有一个选项给你,你可以配置一个SeekToCurrentBatchErrorHandler(使用ListenerContainerCustomizer)来实现类似的功能来在binder中重试。

我尝试了同样的,但与SeekToCurrentBatchErrorHandler,但它的重试超过时间设置这是3次。

  1. 我该怎么做呢?我想要重试整个批次。

  2. 如何将整批邮件发送到dlq topic?例如,对于记录侦听器,我习惯将deliveryAttempt(重试)匹配为3,然后发送到DLQ主题,检查侦听器。

我已经检查了这个链接,这正是我的问题,但一个例子将是很大的帮助,这个库spring-cloud-stream-kafka-binder,我可以实现。请举例说明,我是新手。

目前我有以下代码:

@Configuration
public class ConsumerConfig {
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> customizer() {
return (container, dest, group) -> {
container.getContainerProperties().setAckOnError(false);

SeekToCurrentBatchErrorHandler seekToCurrentBatchErrorHandler 
= new SeekToCurrentBatchErrorHandler();
seekToCurrentBatchErrorHandler.setBackOff(new FixedBackOff(0L, 2L));
container.setBatchErrorHandler(seekToCurrentBatchErrorHandler);
//container.setBatchErrorHandler(new BatchLoggingErrorHandler());
};
}
}

Listerner:

@StreamListener(ActivityChannel.INPUT_CHANNEL)
public void handleActivity(List<Message<Event>> messages,
@Header(name = KafkaHeaders.ACKNOWLEDGMENT) Acknowledgment 
acknowledgment,
@Header(name = "deliveryAttempt", defaultValue = "1") int 
deliveryAttempt) {
try {
log.info("Received activity message with message length {}", messages.size());
nodeConfigActivityBatchProcessor.processNodeConfigActivity(messages);
acknowledgment.acknowledge();
log.debug("Processed activity message {} successfully!!", messages.size());
} catch (MessagePublishException e) {
if (deliveryAttempt == 3) {
log.error(
String.format("Exception occurred, sending the message=%s to DLQ due to: ",
"message"),
e);
publisher.publishToDlq(EventType.UPDATE_FAILED, "message", e.getMessage());
} else {
throw e;
}
}
}

看到@Gary的响应后,添加了带有RetryingBatchErrorHandler的ListenerContainerCustomizer @Bean,但无法导入类。附上截图。

无法导入RetryingBatchErrorHandler

my spring cloud dependencies

使用RetryingBatchErrorHandler将整个批发送到DLT

https://docs.spring.io/spring-kafka/docs/current/reference/html/retrying-batch-eh

使用一个RecoveringBatchErrorHandler,你可以抛出一个BatchListenerFailedException来告诉它哪个记录在批处理中失败。

https://docs.spring.io/spring-kafka/docs/current/reference/html/recovering-batch-eh

在这两种情况下提供一个DeadLetterPublishingRecoverer错误处理程序;禁用dlt

编辑

这里有一个例子;它使用较新的函数式风格,而不是已弃用的@StreamListener,但同样的概念也适用(但您应该考虑改用函数式风格)。

@SpringBootApplication
public class So69175145Application {
public static void main(String[] args) {
SpringApplication.run(So69175145Application.class, args);
}
@Bean
ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> customizer(
KafkaTemplate<byte[], byte[]> template) {
return (container, dest, group) -> {
container.setBatchErrorHandler(new RetryingBatchErrorHandler(new FixedBackOff(5000L, 2L),
new DeadLetterPublishingRecoverer(template,
(rec, ex) -> new TopicPartition("errors." + dest + "." + group, rec.partition()))));
};
}
/*
* DLT topic won't be auto-provisioned since enableDlq is false
*/
@Bean
public NewTopic topic() {
return TopicBuilder.name("errors.so69175145.grp").partitions(1).replicas(1).build();
}
/*
* Functional equivalent of @StreamListener
*/
@Bean
public Consumer<List<String>> input() {
return list -> {
System.out.println(list);
throw new RuntimeException("test");
};
}
/*
* Not needed here - just to show we sent them to the DLT
*/
@KafkaListener(id = "so69175145", topics = "errors.so69175145.grp")
public void listen(String in) {
System.out.println("From DLT: " + in);
}
}
spring.cloud.stream.bindings.input-in-0.destination=so69175145
spring.cloud.stream.bindings.input-in-0.group=grp
spring.cloud.stream.bindings.input-in-0.content-type=text/plain
spring.cloud.stream.bindings.input-in-0.consumer.batch-mode=true
# for DLT listener
spring.kafka.consumer.auto-offset-reset=earliest
[foo]
2021-09-14 09:55:32.838ERROR...
...
[foo]
2021-09-14 09:55:37.873ERROR...
...
[foo]
2021-09-14 09:55:42.886ERROR...
...
From DLT: foo

相关内容

  • 没有找到相关文章

最新更新