php-rdkafka测试笔记

4-09 94 views

参考:https://learnku.com/articles/15176/laravel-implementation-of-kafka-message-push-and-receive-processing

安装

安装librdkafka 库
git clone https://github.com/edenhill/librdkafka.git
./configure
make
sudo make install
安装rdkafka
sudo pecl install rdkafka
配置扩展到php.ini
extension=rdkafka.so

测试

<?php
#写代码测试
$conf = new \RdKafka\Conf();
// enable debug mode
$conf->set('log_level', LOG_DEBUG);
$conf->set('debug', 'all');
$rk = new \RdKafka\Producer($conf);
$rk->addBrokers("192.168.5.170:9092,192.168.5.99:9092");

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

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

$producer = new \RdKafka\Producer();
$producer->addBrokers("192.168.5.170:9092,192.168.5.99:9092");

$obj_topic = $producer->newTopic("php_topic");

//
$input_handler = fopen('php://stdin', 'r');

while (true) {
    echo "\nPlease input  messages:\n";
    $payload = trim(fgets($input_handler));

    // empty message will be quit
    if (empty($payload)) {
        break;
    }

    // send message
    $obj_topic->produce(RD_KAFKA_PARTITION_UA, 0, $payload);
}

echo "done\n";
exit(0);

使用方法

https://www.jianshu.com/p/e4bc3f56354d

php面试题

php魔术方法 __get( $property ) 当调用一个未定义的属性时访问此方法 __set( $property, $value ) 给一个未定义的属性赋值时调用 __isset( $proper...

阅读全文

推荐一个好用的php日志类库,monolog/monolog

github仓库地址 官方文档: https://github.com/Seldaek/monolog/blob/main/doc/01-usage.md 安装 $ composer require monolog/monolog 使用方法 <?php us...

阅读全文

php 二维数组根据某个键值倒叙、升序排序

<?php $array = [ [ 'title' => '标题5', 'views' => 500 ], [ 'title&...

阅读全文

欢迎留言