This is a client that can send and receive messages. Let me demonstrate the sending and receiving operations for you. Friends who need it can download it and try it out.
Send message
<?phptry { $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();
Receive message
<?phptry { $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(); }
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article

25 Oct 2024
Diverse Python SOAP Client Libraries: Navigating the Documentation LabyrinthFor novice Python developers exploring SOAP and its client libraries,...

28 Oct 2024
SOAP Client Libraries for PythonIntroduction:When exploring SOAP technologies in Python, selecting an appropriate client library is crucial. This...

09 Nov 2024
Distinguishing "Memcache" and "Memcached" in PHPPHP offers two memcached libraries: memcache and memcached. Understanding their differences helps...

19 Nov 2024
Memcache vs Memcached: Choosing the Right PHP Memcached LibraryIntroductionPHP offers two seemingly similar memcached libraries: memcache and...

18 Nov 2024
PHP Email Address Validation Libraries UncoveredEmail address validation plays a crucial role in data validation, but creating a...

13 Dec 2024
Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...


Hot Tools

PHP library for dependency injection containers
PHP library for dependency injection containers

A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking

Small PHP library for optimizing images
Small PHP library for optimizing images
