php5.X連線kafka

地球沒有花發表於2018-07-26

kafka擴充套件:https://github.com/arnaud-lb/php-rdkafka

測試版本:php-5.5.25/30

 

安裝:

1、先安裝librdkafka:

git clone https://github.com/edenhill/librdkafka/

cd librdkafka

./configure && make && make install

2、安裝php-rdkafka擴充套件:

git clone https://github.com/arnaud-lb/php-rdkafka

cd php-rdkafka

phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make all -j 5 && make install

3、測試程式碼,用的是他的程式碼改了改,加了些sasl鑑權的東西:

<?php

$conf = new RdKafka\Conf();

// Set the group id. This is required when storing offsets on the broker
$conf->set('group.id', 'vcs');
$conf->set('security.protocol','SASL_PLAINTEXT');
$conf->set('sasl.mechanism','PLAIN');
$conf->set('sasl.username','hello');
$conf->set('sasl.password','world');

$rk = new RdKafka\Consumer($conf);
$rk->addBrokers("mq.cloud.com:9012");
var_dump($rk);
//$conf->set('group.id', 'vcs');

$topicConf = new RdKafka\TopicConf();
$topicConf->set('auto.commit.interval.ms', 100);

// Set the offset store method to 'file'
$topicConf->set('offset.store.method', 'file');
$topicConf->set('offset.store.path', sys_get_temp_dir());

// Alternatively, set the offset store method to 'broker'
// $topicConf->set('offset.store.method', 'broker');

// 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');

$topic = $rk->newTopic("tv.tpvcs.setvcs", $topicConf);

// Start consuming partition 0
$topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);
while (true) {
        $message = $topic->consume(0, 120*10000);
        switch ($message->err) {
                case RD_KAFKA_RESP_ERR_NO_ERROR:
                        var_dump($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;
        }
}

執行就好

 

相關文章