Kafka之PHP扩展开发

原创
2017/03/27 17:11
阅读数 1.3K

PHP扩展安装

要使用PHP作为kafka的producer,consumer者,必须使用php的扩展rdkafka,而rdkafka是依据librdkafka作为底层实现开发
出来的php扩展,那么在使用前我们需要先安装 
1.rdkafka的服务底层依赖: https://github.com/edenhill/librdkafka
2.rdkafka PHP拓展地址: https://github.com/arnaud-lb/php-rdkafka

librdkafka安装

git clone https://github.com/edenhill/librdkafka.git
cd librdkafka
./configure
make
make install

rdkafka安装

git clone https://github.com/arnaud-lb/php-rdkafka.git
cd php-rdkafka
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make all -j 5
make install
# 在php.ini加入如下信息
vim /usr/local/php/etc/php.ini
extension=rdkafka.so
/usr/local/php/bin/php -m
查看使用有 rdkafka

php-kafka代码

produce编写

$rk = new RdKafka\Producer();
$rk->setLogLevel(LOG_DEBUG);
$rk->addBrokers("192.168.201.73");

$topic = $rk->newTopic("boyaa");

$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message payload");

consumer编写

$conf = new RdKafka\Conf();

$conf->set('group.id', 'elk_consumer'); #定义消费组
$conf->set('broker.version.fallback', '0.8.2.2');  #重要 设置版本代理
$rk = new RdKafka\Consumer($conf); 

$rk->addBrokers("192.168.201.73");

$topicConf = new RdKafka\TopicConf();

$topicConf->set('auto.offset.reset', 'smallest');
$topicConf->set('auto.commit.enable', 'true');
$topicConf->set('offset.store.method', 'file');
$topicConf->set('offset.store.path', '/data/wwwroot/test/data');
$topicConf->set('offset.store.sync.interval.ms', 0);

$topic = $rk->newTopic("boyaa", $topicConf);

// Start consuming partition 0
$topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);

while (true) {
    $message = $topic->consume(0, 2000);
    switch ($message->err) {
        case RD_KAFKA_RESP_ERR_NO_ERROR:
            $message->payload;  #消息体 可处理相应业务
            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;
    }
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部