Kafka installation and use of Kafka-PHP extension, kafkakafka-php Extension _ PHP Tutorial

Source: Internet
Author: User
Kafka installation and use of Kafka-PHP extension, kafkakafka-php extension. Kafka installation and the use of Kafka-PHP extensions, kafkakafka-php extensions are a little output when they are used, or you will forget it after a while, so here we will record how to install Kafka and how to use kafka-PHP extension and kafkakafka-php extension.

If it is used, it will be a little output, or you will forget it after a while, so here we will record the installation process of the Kafka trial and the php extension trial.

To be honest, if it is used in the queue, it is better than PHP, or Redis. It's easy to use, but Redis cannot have multiple consumers. However, Kafka does not officially support PHP. PHP extensions are written by fans or users. The following describes how to install Kafka. I use CentOS6.4 as an example, 64-bit.

I. check whether jdk is installed.

Use commands

[root@localhost ~]# java -versionjava version "1.8.0_73"Java(TM) SE Runtime Environment (build 1.8.0_73-b02)Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)

If you have the above information, install the SDK. if jdk is not correct, install it on the correct one. If no installation is available, follow the jdk installation method below:

Http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Download jdk88.0 from this address. download jdk-8u73-linux-x64.tar.gz and decompress it to/usr/local/jdk.

Open the/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

Last

[root@localhost ~]# source /etc/profile

The jdk takes effect now. you can use java-version for verification.

II. install Kafka

1. download Kafka

To begin.

2. download and decompress the package to your favorite directory.

I decompress 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

[root@localhost kafka_2.9.1-0.8.2.2]# sh bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

Run 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 consumer will be able to receive the input content from the producer side immediately.

4. when there is a cross-host producer or consumer connection

You need to configure the host. name of config/server. properties. Otherwise, the host cannot be connected across hosts.

3. Kafka-PHP extension

After a circle is used, https://github.com/nmred/kafka-php can be used.

I installed it using composer. The following is an example:

Producer. php

 getAvailablePartitions('topic_name');    var_dump($partitions);    // send message    $produce->setRequireAck(-1);    $produce->setMessages('topic_name', 0, 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());    }}

If Kafka is used, it will produce a little output. Otherwise, I forgot to use Kafka for installation...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.