3

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

4

1 回答 1

0

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

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

消费的方法标记为Retryable,所以如果有异常处理会重试,如果重试不成功则调用相应的恢复方法。

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
    }
  
 }
于 2021-09-21T07:19:00.800 回答