Kafka is only a small bond. It is often used for sending and transferring data. In the official case of Kafka, there is no relevant implementation version of PHP in fact. Now the online circulating Kafka of the relevant PHP library, are some of the programming enthusiasts write their own class library, so there will certainly not be too unified interface standards.
The following is an example of a class library that demonstrates the use of the associated Kafka PHP extension library. A comprehensive comparison of several Kafka PHP library, South Jiangsu feel the following this open source class library, nmred/kafka-php, relatively concise and convenient.
You first install the composer Class Library nmred/kafka-php.
Composer The basic use of the problem, you can view my composer related articles. https://newsn.net/tag/composer/.
Composer require "nmred/kafka-php"-VVV
Of course, you can also use mirroring to speed up downloads.
{" config": { "secure-http": false, "Preferred-install": "Dist", "sort-packages": True }, "Repositories": { "packagist": {" type": "Composer", "url": "Https://packagist.phpcomposer.com" } }, "require": { "nmred/kafka-php": "v0.2.0.7"} }
Determine port and topic, view Kafka version number
I chose the local port is 9092,topic is test1, while viewing my local Kafka version is 0.11.0.0. These are all used in the code.
Producer Code (ASYNC)
<?phprequire ' vendor/autoload.php ';d ate_default_timezone_set (' PRC ');//Use monolog\logger;//use Monolog\Handler \stdouthandler;////Create the logger//$logger = new Logger (' My_logger ');///Now add some handlers//$logger->pushh Andler (New Stdouthandler ()); $config = \kafka\producerconfig::getinstance (); $config Setmetadatarefreshintervalms (10000), $config->setmetadatabrokerlist (' 127.0.0.1:9092 '); $config Setbrokerversion (' 0.11.0.0 '); $config->setrequiredack (1); $config->setisasyn (false); $config Setproduceinterval, $producer = new \kafka\producer (function () {return Array (Array (' topic ' =&G T ' Test1 ',//note corresponds to topic ' key ' = ' testkey ', ' value ' = ' test....message. ', ') ;});/ /$producer->setlogger ($logger); $producer->success (function ($result) {var_dump ($result);}); $producer->error (function ($errorCode) {var_dump ($errorCode);}); $producer->send (TRUE);
Producer Code (synchronous)
<?phprequire ' vendor/autoload.php ';d ate_default_timezone_set (' PRC ');//Use monolog\logger;//use Monolog\Handler \stdouthandler;//Create the logger//$logger = new Logger (' My_logger ');//Now add some handlers//$logger->pushhandler (New Stdouthandler ()); $config = \kafka\producerconfig::getinstance (); $config->setmetadatarefreshintervalms ( 10000); $config->setmetadatabrokerlist (' 127.0.0.1:9092 '); $config->setbrokerversion (' 0.11.0.0 '); $config- >setrequiredack (1); $config->setisasyn (false); $config->setproduceinterval; $producer = new \kafka\ Producer ();//$producer->setlogger ($logger); for ($i = 0; $i <; $i + +) { $result = $producer->send (Array ( C1/>array ( ' topic ' = ' test1 ', ' value ' = ' test1....message. ', ' key ' = ' key '. $i, ) ) ; Var_dump ($result);}
The code for both producers can be received with the following shell commands.
Kafka-console-consumer--bootstrap-server localhost:9092--topic test1--from-beginning
Consumer Code
Require ' vendor/autoload.php ';d ate_default_timezone_set (' PRC ');//Use monolog\logger;//use Monolog\handler\ stdouthandler;//Create the logger//$logger = new Logger (' My_logger ');//Now add some handlers//$logger->pushhandler ( New Stdouthandler ()); $config = \kafka\consumerconfig::getinstance (); $config->setmetadatarefreshintervalms ( 10000); $config->setmetadatabrokerlist (' 127.0.0.1:9092 '); $config->setgroupid (' test '); $config Setbrokerversion (' 0.11.0.0 '); $config->settopics (Array (' test1 '));//$config->setoffsetreset (' earliest '); $ Consumer = new \kafka\consumer ();//$consumer->setlogger ($logger); $consumer->start (function ($topic, $part, $ Message) { var_dump ($message);});
This consumer code can send data through the following shell command.
Kafka-console-producer--broker-list localhost:9092--topic test1
It is worth noting that this consumer's code can be executed in a Web page. The page displays the relevant data in real time. The PHP side is estimated to be an infinitely long endless state.
Of course, we should remember to open zookeeper and Kafka, in order to do these experiments. For installation of related Kafka, please click here to view. https://newsn.net/tag/kafka/.
The class library address that is covered in this article is: https://github.com/weiboad/kafka-php. This open source library, seems to be a Chinese work, so there is an English document. See here. Https://github.com/weiboad/kafka-php/blob/master/README_CH.md.