【问题标题】:Spring Cloud Stream Kafka retries 10 times the maxAttemptsSpring Cloud Stream Kafka 重试 10 次 maxAttempts
【发布时间】:2021-09-17 00:33:06
【问题描述】:

我一直在尝试为 Spring cloud stream kafka 实现重试逻辑,这样如果在生成主题 sample-topic 的事件时抛出异常,它会再重试两次。
我在 application.properties 文件中添加了以下配置

spring.cloud.stream.bindings.processSampleEvent.destination=sample-topic
spring.cloud.stream.bindings.processSampleEvent.content-type=application/json
spring.cloud.stream.bindings.processSampleEvent.consumer.maxAttempts=2

我已经编写了列表器代码,它只记录接收到的消息并抛出 NullPointerException,以便我可以测试重试。

@StreamListener(ListenerBind.SAMPLE_CHANNEL)
  public void processSampleEvent(String productEventDto) {
    System.out.println("Entering listener: " + productEventDto);
    throw new NullPointerException();
}

但是当通过向sample-topic 生成事件进行测试时,我看到在日志中该事件已经重试了 20 次,但我在属性中指定只尝试两次,而且当我换了 3 次,它重试了 30 次。
我对 Spring 云流很陌生,对此的任何帮助都会非常有帮助。 提前谢谢???

【问题讨论】:

    标签: spring-boot spring-kafka spring-cloud-stream spring-cloud-stream-binder-kafka


    【解决方案1】:

    侦听器容器中的默认错误处理程序现在是 SeekToCurrentErrorHandler,有 10 次传递尝试。

    您可以禁用活页夹中的重试,并使用所需的重试语义配置STCEH,或者在活页夹中使用重试并将默认错误处理程序替换为简单的LoggingErrorHandler

    要配置容器的错误处理程序,请添加ListenerContainerCustomizer<AbstractKafkaListenerContainerFactory> @Bean

    【讨论】:

    • 我观察到maxAttempts重试10次的行为不适用于enableDlq=true。你能解释一下背后的原因吗?
    • 因为错误是通过将记录发送到死信主题来“恢复”的,并且没有向容器抛出异常,所以它的错误处理程序没有被调用。
    • 我明白了,这是有道理的。尽管如此,这个 Kafka binder 的重试 10 次行为是否与 SCSt maxAttempts 的定义相矛盾?有没有计划让它们保持一致?
    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多