php-rdkafka手動提交偏移量

在項目中使用php-rdkafka的高級消費者時,發現設置了:

$topicConf->set('enable.auto.commit', 'false');

沒有效果,還是會自動提交offset,查了各種資料,正確的應該是這樣設置:

$conf->set('enable.auto.commit', 'false');

相關說明見文檔:https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md

附上示例:

<?php
 
$conf = new RdKafka\Conf();
 
// Set a rebalance callback to log partition assignments (optional)
$conf->setRebalanceCb(function (RdKafka\KafkaConsumer $kafka, $err, array $partitions = null) {
    switch ($err) {
        case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
            echo "Assign: ";
            var_dump($partitions);
            $kafka->assign($partitions);
            break;
 
         case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
             echo "Revoke: ";
             var_dump($partitions);
             $kafka->assign(NULL);
             break;
 
         default:
            throw new \Exception($err);
    }
});
 
// Configure the group.id. All consumer with the same group.id will consume
// different partitions.
$conf->set('group.id', 'myConsumerGroup');
 
// Initial list of Kafka brokers
$conf->set('metadata.broker.list', '127.0.0.1:9092');
 
//設置使用手動提交offset
$conf->set('enable.auto.commit', 'false');
 
$topicConf = new RdKafka\TopicConf();
 
// Set where to start consuming messages when there is no initial offset in
// offset store or the desired offset is out of range.
// 'smallest': start from the beginning
$topicConf->set('auto.offset.reset', 'smallest');
 
// Set the configuration to use for subscribed/assigned topics
$conf->setDefaultTopicConf($topicConf);
 
$consumer = new RdKafka\KafkaConsumer($conf);
 
// Subscribe to topic 'test'
$consumer->subscribe(['test']);
 
while (true) {
    $message = $consumer->consume(120*1000);
    switch ($message->err) {
        case RD_KAFKA_RESP_ERR_NO_ERROR:
            var_dump($message);
            //消費完成後手動提交offset
            //$consumer->commit($message);     
            break;
        case RD_KAFKA_RESP_ERR__PARTITION_EOF:
            echo "No more messages; will wait for more\n";
            break;
        case RD_KAFKA_RESP_ERR__TIMED_OUT:
            echo "Timed out\n";
            break;
        default:
            throw new \Exception($message->errstr(), $message->err);
            break;
    }
}
 
————————————————
版權聲明:本文爲CSDN博主「青季」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/skypeng57/article/details/87715268

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章