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
09Jul2023
PHP client library recommendations and usage instructions for the MQTT protocol MQTT (MessageQueuingTelemetryTransport) is a lightweight message transmission protocol that is widely used in fields such as the Internet of Things and sensor networks. In PHP development, in order to facilitate communication with the MQTT server using the MQTT protocol, we can choose to use some PHP client libraries to simplify this process. In this article, we will recommend several commonly used PHP client libraries and provide usage instructions.
30Sep2016
Looking for a php/python library management program (similar to Baidu library, managing doc/pdf and other libraries)~~ It mainly needs to have search functions, especially file classification retrieval/file tag retrieval functions, no need for online conversion, online browsing!
28Oct2024
SOAP Client Libraries for PythonIntroduction:When exploring SOAP technologies in Python, selecting an appropriate client library is crucial. This...
22Apr2024
Function libraries and third-party libraries in PHP can extend the functionality of applications. The function library provides predefined functions that can be included through the include statement. Third-party libraries are available from sources such as Packagist, GitHub, and installed using Composer. Implement automatic loading of classes through autoloaders, such as automatic loading of the Guzzle library. Learn how to use the Dompdf third-party library to generate PDF files through practical cases, including loading the library, loading HTML content, and outputting PDF files. The integration of function libraries and third-party libraries greatly expands the functionality of PHP applications and improves development efficiency and project performance.
17Apr2024
The steps for writing a function library in PHP are as follows: Create a PHP file (such as myFunctions.php) to store the functions. Use the function keyword to define functions in a file. Include libraries in other scripts using require_once or include_once statements. Once a function library is included, its functions can be used.
25Oct2024
Diverse Python SOAP Client Libraries: Navigating the Documentation LabyrinthFor novice Python developers exploring SOAP and its client libraries,...
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