php rdkafka扩展发送和接收消息
2017-04-24 10:29
411 查看
发送消息
接收消息
<?php try { $rcf = new RdKafka\Conf(); $rcf->set('group.id', 'test'); $cf = new RdKafka\TopicConf(); $cf->set('offset.store.method', 'broker'); $cf->set('auto.offset.reset', 'smallest'); $rk = new RdKafka\Producer($rcf); $rk->setLogLevel(LOG_DEBUG); $rk->addBrokers("127.0.0.1"); $topic = $rk->newTopic("test", $cf); for($i = 0; $i < 1000; $i++) { $topic->produce(0,0,'test' . $i);//没有setMessge接口了,使用produce 参考:https://libraries.io/github/mentionapp/php-rdkafka } } catch (Exception $e) { echo $e->getMessage(); }
接收消息
<?php try { $rcf = new RdKafka\Conf(); $rcf->set('group.id', 'test'); $cf = new RdKafka\TopicConf(); /* $cf->set('offset.store.method', 'file'); */ $cf->set('auto.offset.reset', 'smallest'); $cf->set('auto.commit.enable', true); $rk = new RdKafka\Consumer($rcf); $rk->setLogLevel(LOG_DEBUG); $rk->addBrokers("127.0.0.1"); $topic = $rk->newTopic("test", $cf); //$topic->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING); while (true) { $topic->consumeStart(0, RD_KAFKA_OFFSET_STORED); $msg = $topic->consume(0, 1000); var_dump($msg); if ($msg->err) { echo $msg->errstr(), "\n"; break; } else { echo $msg->payload, "\n"; } $topic->consumeStop(0); sleep(1); } } catch (Exception $e) { echo $e->getMessage(); }
相关文章推荐
- 用PHP尝试RabbitMQ(amqp扩展)实现消息的发送和接收
- 用PHP尝试RabbitMQ(amqp扩展)实现消息的发送和接收
- 用PHP尝试RabbitMQ(amqp扩展)实现消息的发送和接收
- 用PHP尝试RabbitMQ(amqp扩展)实现消息的发送和接收
- 用PHP尝试RabbitMQ(amqp扩展)实现消息的发送和接收
- 用PHP尝试RabbitMQ(amqp扩展)实现消息的发送和接收
- PHP处理微信中带Emoji表情的消息发送和接收(Unicode字符转码编码)
- PHP处理微信中带Emoji表情的消息发送和接收(Unicode字符转码编码)
- PHP处理微信中带Emoji表情的消息发送和接收(Unicode字符转码编码)
- 关于Java连接虚拟机中的Kafka时,无法发送、接收消息的问题
- 微信公众平台-接收发送被动响应消息-PHP示例
- java kafka消息的发送与接收
- java向linux的kafka发送消息 并接收消息实例
- PHP微信公众平台接口(四)——消息的接收和发送
- linux下php安装php-kafka扩展和php-rdkafka扩展
- PHP处理微信中带Emoji表情的消息发送和接收(Unicode字符转码编码)
- spring boot整合spring-kafka实现发送接收消息实例代码
- librdkafka安装和php扩展php-rdkafka安装