PHP安装kafka扩展

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012314976/article/details/58622203

安装

安装librdkafka

下载地址:https://github.com/edenhill/librdkafka

wget https://github.com/edenhill/librdkafka/archive/v0.9.4.tar.gz -O librdkafka-0.9.4.tar.gz
tar -zxvf librdkafka-0.9.4.tar.gz
cd librdkafka-0.9.4
yum install gcc-c++
./configure
make
make install

安装php-rdkafka

下载地址:https://github.com/arnaud-lb/php-rdkafka

wget https://github.com/arnaud-lb/php-rdkafka/archive/3.0.1.tar.gz -O php-rdkafka-3.0.1.tar.gz
tar -zxvf php-rdkafka-3.0.1.tar.gz
cd php-rdkafka-3.0.1
phpize
./configure --with-php-config=php-config
make
make install

配置php.ini并重启php-fpm

在php.ini中添加

extension=rdkafka.so

PHP消费

参考https://github.com/arnaud-lb/php-rdkafka

$rk = new RdKafka\Consumer();
$rk->setLogLevel(LOG_DEBUG);
$rk->addBrokers("127.0.0.1");

$topic = $rk->newTopic("test");
$topic->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING);

while (true) {
    // The first argument is the partition (again).
    // The second argument is the timeout.
    $msg = $topic->consume(0, 1000);
    if($msg==NULL){
        sleep(1);
    }
    else{
        echo '#'.$msg->payload."#\n";
    }
}

参考

猜你喜欢

转载自blog.csdn.net/u012314976/article/details/58622203
今日推荐