3

我使用卡夫卡與春季啓動1個結果:卡夫卡生產TimeoutException異常:到期

卡夫卡監製類

@Service 
public class MyKafkaProducer { 

    @Autowired 
    private KafkaTemplate<String, String> kafkaTemplate; 

    private static Logger LOGGER = LoggerFactory.getLogger(NotificationDispatcherSender.class); 

    // Send Message 
    public void sendMessage(String topicName, String message) throws Exception { 
     LOGGER.debug("========topic Name===== " + topicName + "=========message=======" + message); 
     ListenableFuture<SendResult<String, String>> result = kafkaTemplate.send(topicName, message); 
     result.addCallback(new ListenableFutureCallback<SendResult<String, String>>() { 
      @Override 
      public void onSuccess(SendResult<String, String> result) { 
       LOGGER.debug("sent message='{}' with offset={}", message, result.getRecordMetadata().offset()); 
      } 

      @Override 
      public void onFailure(Throwable ex) { 
       LOGGER.error(Constants.PRODUCER_MESSAGE_EXCEPTION.getValue() + " : " + ex.getMessage()); 
      } 
     }); 
    } 
} 

卡夫卡配置:

spring.kafka.producer.retries=0 
spring.kafka.producer.batch-size=100000 
spring.kafka.producer.request.timeout.ms=30000 
spring.kafka.producer.linger.ms=10 
spring.kafka.producer.acks=0 
spring.kafka.producer.buffer-memory=33554432 
spring.kafka.producer.max.block.ms=5000 
spring.kafka.bootstrap-servers=192.168.1.161:9092,192.168.1.162:9093 

假設我已經發送了1000次topi信息的10倍c my-test-topic

8出10次,我成功地讓所有的信息在我的消費者,但有時我得到這個下面錯誤

2017-10-05 07:24:11, [ERROR] [my-service - LoggingProducerListener - onError:76] Exception thrown when sending a message with key='null' and payload='{"deviceType":"X","deviceKeys":[{"apiKey":"X-X-o"}],"devices...' to topic my-test-topic

org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for my-test-topic-4 due to 30024 ms has passed since batch creation plus linger time

+0

您是從生產者還是消費者描述的這個錯誤? – adarshr

+0

我在生產者上收到這個錯誤 –

+0

所以,你的批處理對於這樣一個「低」的'request.timeout.ms'來說太慢了。儘量讓批量大小稍低 –

回答

0

有3種可能性:

  1. 增加request.timeout.ms - 這是卡夫卡的時間等待整個批次在緩衝區中準備就緒。所以在你的情況下,如果緩衝區中的消息少於100 000,超時將發生。更多的信息在這裏:https://stackoverflow.com/a/34794261/2707179
  2. 減少batch-size - 與以前的點相關,它會發送更多的批次,但他們將包括更少的消息。
  3. 根據郵件大小,也許您的網絡無法趕上高負荷?檢查您的吞吐量是否不是瓶頸。
相關問題