Spring整合Kafka實現批量消費和手動提交offset

冰 河發表於2020-12-07

spring-kafka的官方文件介紹,可以知道自1.1版本之後,

@KafkaListener開始支援批量消費,只需要設定batchListener引數為true

把application.yml中的enable-auto-commit設定為false,設定為不自動提交

@Bean
public KafkaListenerContainerFactory<?> batchFactory(ConsumerFactory consumerFactory){
    ConcurrentKafkaListenerContainerFactory<Integer,String> factory =
    new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory);
    factory.setConcurrency(10);
    factory.getContainerProperties().setPollTimeout(1500);
    factory.setBatchListener(true);//設定為批量消費,每個批次數量在Kafka配置引數中設定
    factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);//設定手動提交ackMode
  return factory; }
//批量訊息
    @KafkaListener(topics = {"first_top"},containerFactory="batchFactory")
    public void consumerBatch(List<ConsumerRecord<?, ?>> records, Acknowledgment ack){
        log.info("接收到訊息數量:{}",record.size());
       //手動提交
      ack.acknowledge();
}

這裡containerFactory = “batchFactory”要指定為批量消費

 

相關文章