【问题标题】:How to implement retry and recover logic with Spring Reactive Kafka如何使用 Spring Reactive Kafka 实现重试和恢复逻辑
【发布时间】:2021-11-14 17:15:19
【问题描述】:

我们正在使用https://github.com/reactor/reactor-kafka 项目来实现 Spring Reactive Kafka。但是我们想利用 Kafka 重试和恢复反应式 Kafka 的逻辑。 谁能提供一些示例代码?

【问题讨论】:

标签: apache-kafka project-reactor reactive-kafka


【解决方案1】:

由于您使用 spring 生态系统进行重试和恢复,您可以使用 spring-retry 查看文档spring -retry。网上有足够的参考资料。

下面的示例示例正在使用来自 kafka 主题的消息并进行处理。

消耗的方法被标记为可重试,所以万一有 异常处理将重试,如果重试不成功则 会调用相应的恢复方法。

public class KafkaListener{
  
 
  @KafkaListener(topic="books-topic", id ="group-1")
  @Retryable(maxAttempts = 3, value = Exception.class))
  public void consuming(String message){
   //  To do message processing 
   //  Whenever there is exception thrown from this method
   //   - it will retry 3 times in total
   //   - Even after retry we get exception then it will be handed of to below 
   //     recover method recoverConsuming 
  
   }

   @Recover
   public void recoverConsuming(Exception exception, String message){
     // Recovery logic 
     // you can implement your recovery scenario
    }
  
 }

【讨论】:

  • @KafkaListener 没有反应。
  • 是的,kafkalistener 不是反应式的。 [spring-reactive/vertx.io 是响应式框架] 我的观点是你可以使用 spring retry
猜你喜欢
  • 2022-09-25
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 2020-02-08
  • 2021-10-16
  • 2013-08-26
相关资源
最近更新 更多