简体   繁体   English

如何处理kafka生产者异常

[英]How to handle kafka producer exceptions

I am trying to understand how spring boot KafkaTemplate works with async producer and handle exceptions.我试图了解 spring 引导 KafkaTemplate 如何与异步生产者一起工作并处理异常。 I want to handle all kinds of errors including.network errors.我想处理各种错误,包括网络错误。 Tried with retry configs but its retrying more than the number I provided to尝试重试配置,但重试次数超过我提供给的次数

@Service
public class UserInfoService {
     private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoService.class);

     @Autowired
     private KafkaTemplate kafkaTemplate;

     public void sendUserInfo(UserInfo data) {
         final ProducerRecord<String, UserInfo> record = new ProducerRecord<>("usr-test-data", "test-app", data);
         
         try {
             ListenableFuture<SendResult<String, UserInfo>> future = kafkaTemplate.send(record);
             future.addCallback(new ListenableFutureCallback<SendResult<String, UserInfo>>() {
                 @Override
                 public void onFailure(Throwable ex) {
                     handleFailure(ex);
                 }

                 @Override
                 public void onSuccess(SendResult<String, UserInfo> result) {
                     handleSuccess(result);
                 }
             });
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
         
     }

     private void handleSuccess(SendResult<String, UserInfo> result) {
         LOGGER.info("Message sent successfully with offset: {}", result.getRecordMetadata().offset());
     }
     private void handleFailure(Throwable ex) {
         LOGGER.info("Unable to send message- Error: {}",  ex.getMessage());
     }
 }

Tried to limit number of retries with configProps.put(ProducerConfig.RETRIES_CONFIG, "3");尝试使用configProps.put(ProducerConfig.RETRIES_CONFIG, "3"); hoping this will eventually throw exception and I can catch.希望这最终会抛出异常并且我可以捕捉到。 But it still tries more than 3 times and seems not working.但它仍然尝试了 3 次以上,但似乎没有用。 Here is my complete config class:这是我的完整配置 class:

@Configuration
public class KafkaProducerConfig {
     @Value(value = "${spring.kafka.bootstrap-servers}")
     private String bootstrapAddress;

     @Bean
     public ProducerFactory<String, String> producerFactory() {
         Map<String, Object> configProps = new HashMap<>();
         configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
         configProps.put(ProducerConfig.ENABLE_IDEMPOTENCE_DOC, "true");
         configProps.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, CountingProducerInterceptor.class.getName());
         configProps.put(ProducerConfig.ACKS_CONFIG, "all");
         configProps.put(ProducerConfig.RETRIES_CONFIG, "3");
         configProps.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 10000);
         configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
         configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
         return new DefaultKafkaProducerFactory<>(configProps);
     }

     @Bean
     public KafkaTemplate<String, String> kafkaTemplate() {
         return new KafkaTemplate<>(producerFactory());
     }
 }

Would like to know what I can catch in future.OnFailure and in the parent try catch block.想知道我可以在 future.OnFailure 和父 try catch 块中捕获什么。

A Future does not complete within the lifecycle of a try-catch. You need to throw the exception within the body of the onFailure. Future 没有在 try-catch 的生命周期内完成。您需要在 onFailure 的主体内抛出异常。

In my experience, Kafka.network errors cannot easily be caught根据我的经验,Kafka.network 错误不容易被捕获

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM