Kafka installation and use of kafka-php extensions, kafkakafka-php extension
Words to use will be a bit of output, or after a period of time and forget, so here is a record of the trial Kafka installation process and the PHP extension trial.
To tell you the truth, if you're using a queue, it's a redis. With the handy, hehe, just redis can not have multiple consumer. However, Kafka is not officially supported by PHP, and PHP extensions are written by enthusiasts or users. Let's start by talking about the installation of Kafka. I take CentOS6.4 as an example, 64 bits.
First, make sure the JDK is installed.
Using commands
[Root@localhost ~]# java-"1.8.0_73"1.8. 0_73- 25.73-b02, Mixed mode)
If you have the above information, then install it down, some may be the JDK is not up, then put on the right. If it is not installed, look at the following JDK installation methods:
Http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
To this address to download the JDK8 version, I downloaded the jdk-8u73-linux-x64.tar.gz, and then extracted to/usr/local/jdk/under.
And then open/etc/profile file
[Root@localhost ~]# Vim/etc/profile
Write the following code into the file.
Export JAVA_HOME=/USR/LOCAL/JDK/JDK1. 8 . 0_73export CLASSPATH=.: $JAVA _home/lib/tools.jar: $JAVA _home/lib/dt.jarexport PATH= $JAVA _home/ Bin: $PATH
At last
[Root@localhost ~]# Source/etc/profile
The JDK is now in effect and can be verified with java-version.
Two. Next install the Kafka
1. Download Kafka
To http://kafka.apache.org/downloads.html download the appropriate version, I am using kafka_2.9.1-0.8.2.2.tgz.
2. Download and unzip to your favorite directory
I'm extracting it to/usr/local/kafka/kafka_2.9.1-0.8.2.2.
3. Run the default Kafka
Start Zookeeper Server
[Root@localhost kafka_2. 9.1-0.8. 2.2 sh bin/zookeeper-server-start. sh config/zookeeper.properties &
Start Kafka Server
[Root@localhost kafka_2. 9.1-0.8. 2.2 sh bin/kafka-server-start. sh config/server.properties &
Run producer producer
[Root@localhost kafka_2. 9.1-0.8. 2.2 sh bin/kafka-console-producer. sh --broker-list localhost:9092 --topic test
Run consumer Consumer
[Root@localhost kafka_2. 9.1-0.8. 2.2 sh bin/kafka-console-consumer. sh --zookeeper localhost:2181 --topic test--from-beginning
In this way, the content is entered on the producer side, and consumer will be able to receive it immediately.
4. When a producer or consumer is connected to a cross-machine
You need to configure the Config/server.properties host.name, or the cross-machine is not connected.
Three. kafka-php extension
Using a lap, you can use the https://github.com/nmred/kafka-php.
I installed it using composer, here is an example:
producer.php
PHPrequire ' vendor/autoload.php '; while (1) { $partmt_rand(0, 1); $produce = \kafka\produce::getinstance (' kafka0:2181 ', +); // Get available partitions $partitions $produce->getavailablepartitions (' topic_name '); Var_dump ($partitions); // Send Message $produce->setrequireack ( -1); $produce Array (date(' y-m-d h:i:s ')); Sleep (3);}
consumer.php
require' Vendor/autoload.php ';$consumer= \kafka\consumer::getinstance (' kafka0:2181 ');$group= ' topic_name ';$consumer->setgroup ($group);$consumer->setfromoffset (true);$consumer->settopic (' topic_name ', 0);$consumer->setmaxbytes (102400);$result=$consumer-fetch ();Print_r($result);foreach($result as $topicName=$partition) { foreach($partition as $partId=$messageSet) { Var_dump($partition-Gethighoffset ()); foreach($messageSet as $message) { Var_dump((string)$message); } Var_dump($partition-Getmessageoffset ()); }}
http://www.bkjia.com/PHPjc/1100706.html www.bkjia.com true http://www.bkjia.com/PHPjc/1100706.html techarticle Kafka installation and the use of kafka-php extension, the kafkakafka-php extension said it will be a bit of output, or after a period of time and forget, so here is a record of the trial Kafka installation process ...