php5.X连接kafka

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;
        }
}

执行就好

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值