Centos version: Centos6.4, PHP version: PHP7.
In the previous article using IP as 192.168.9.154 machine is installed and turned on Kafka was a simple test, it acts as Kafka server.
This article open a new IP address is 192.16.9.157 machine to turn PHP extensions.
Locate the expansion Download github, here it is the php-rdkafka, although there is a php extension is php-kafka, but php-rdkafka than php-kafka powerful.
https://github.com/arnaud-lb/php-rdkafka // php-rdkafka Download
You need to install a library, librdkafka to the system before installing php-rdkafka.
https://github.com/edenhill/librdkafka // librdkafka address
After two packages are downloaded, first extract the installation of librdkafka
# unzip librdkafka-master.zip # cd librdkafka-master # ./configure # make && make install
Then compile and install php-rdkafka
unzip php-rdkafka-master.zip # cd php-rdkafka-master # phpize # ./configure --with-php-config=/usr/local/php/bin/php-config # make && make install
Installing shared extensions: / usr / local / php / lib / php / extensions / no-debug-non-zts-20170718 / // so address # vim /usr/local/php/etc/php.ini // Add the following Code = Extension / usr / local / PHP / lib / PHP / Extensions / non-NO-Debug-ZTS-20,170,718 / rdkafka.so # FPM-Service PHP-PHP restart the restart //
OK
hp Operating kafka
Before running the first open our zookeeper and kafka last article on how to open
-
Run Producer
Kafka used to live default port 9092vim producer.php
<?php $rk = new RdKafka\Producer(); $rk->setLogLevel(LOG_DEBUG); $rk->addBrokers("ip:9092"); $topic = $rk->newTopic("test"); $topic->produce(RD_KAFKA_PARTITION_UA, 0, "要发送的消息");
Run consumervim consumer.php
<?php $rk = new RdKafka\Consumer(); $rk->setLogLevel(LOG_DEBUG); $rk->addBrokers("ip"); $topic = $rk->newTopic("test"); $topic->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING); while(true){ sleep(1); $msg = $topic->consume(0, 1000); if ($msg) { echo $msg->payload, "\n"; } }
Open two windows to run a consumer a run producer
php consumer.php
php producer.php
You will find that we have a simple use kafka.