rdkafka php how to install

藏色散人
Release: 2023-03-09 16:44:02
Original
2855 people have browsed it

php rdkafka installation method: first download and install librdkafka; then install the php-rdkafka extension; finally write "extension=rdkafka.so" in php.ini.

rdkafka php how to install

The operating environment of this article: windows7 system, php7.0 version, DELL G3 computer

php-rdkafka extension installation

php has two ways to call kafka

  • ##php-rdkafka
Document address: https://arnaud-lb .github.io/php-rdkafka/phpdoc/book.rdkafka.html

rdkafka installation needs to depend on librdkafka, so we need to install librdkafka first
Download address http://pecl.php.net/package/rdkafka

git clone https://github.com/edenhill/librdkafka.git
cd librdkafka
./configure
make && make install
Copy after login
Install the php-rdkafka extension

git clone https://github.com/arnaud-lb/php-rdkafka.git
cd php-rdkafka
phpize
./configure --with-php-config=/usr/local/php7.0/bin/php-config
make && make install
Copy after login
Then write

extension = rdkafka.so
Copy after login


rdkafka php how to install

# in php.ini




rdkafka php how to install

rdkafka php how to install

    kafka-php extension package
  • Document address: https://github.com/weiboad/kafka-php

##Simple example
  • Generator
<?php $rk = new RdKafka\Producer();
$rk->setLogLevel(LOG_DEBUG);
$rk->addBrokers("192.168.2.152");

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

for ($i = 0; $i produce(RD_KAFKA_PARTITION_UA, 0, "发送信息: $i");
    $rk->poll(0);
}

while ($rk->getOutQLen() > 0) {
    $rk->poll(50);
}

?>
Copy after login
Consumer

<?php $conf = new RdKafka\Conf();

$conf->set('group.id', 'myConsumerGroup');

$rk = new RdKafka\Consumer($conf);
$rk->addBrokers("192.168.2.150:9092");

$topicConf = new RdKafka\TopicConf();
$topicConf->set('auto.commit.interval.ms', 100);
$topicConf->set('offset.store.method', 'file');
$topicConf->set('offset.store.path', sys_get_temp_dir());
$topicConf->set('auto.offset.reset', 'smallest');

$topic = $rk->newTopic("shop", $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 "等待接收信息\n";
            break;
        case RD_KAFKA_RESP_ERR__TIMED_OUT:
            echo "超时\n";
            break;
        default:
            throw new \Exception($message->errstr(), $message->err);
            break;
    }
}

?>
Copy after login


rdkafka php how to install

##Recommended learning: "

PHP Video Tutorial
"

The above is the detailed content of rdkafka php how to install. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template