


kafka installation and use of Kafka-PHP extension, kafkakafka-php extension_PHP tutorial
kafka installation and use of Kafka-PHP extension, kafkakafka-php extension
If you use it, you must have some output, otherwise you will forget it after a while, so here it is Just record the installation process of trying out Kafka and trying out the PHP extension.
To be honest, if it is used for queues, Redis is more compatible with PHP. It's easy to use, haha, but Redis cannot have multiple consumers. However, Kafka does not officially support PHP, and PHP extensions are written by enthusiasts or users. Let’s start with the installation of Kafka. I take CentOS6.4 as an example, 64-bit.
1. First confirm whether jdk is installed
Use command
[root@localhost ~]# java -<span>version java version </span><span>"</span><span>1.8.0_73</span><span>"</span><span> Java(TM) SE Runtime Environment (build </span><span>1.8</span>.0_73-<span>b02) Java HotSpot(TM) </span><span>64</span>-Bit Server VM (build <span>25.73</span>-b02, mixed mode)
If you have the above information, just install it. Some of the jdk may not be compatible, so install it to the right one. If it is not installed, take a look at the jdk installation method below:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Go to this address to download the jdk8 version. I downloaded jdk-8u73-linux-x64.tar.gz, and then unzipped it to /usr/local/jdk/.
Then open the /etc/profile file
[root@localhost ~]# vim /etc/profile
Write the following code into the file
export JAVA_HOME=/usr/local/jdk/jdk1.<span>8</span><span>.0_73 export CLASSPATH</span>=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/<span>dt.jar export PATH</span>=$JAVA_HOME/bin:$PATH
Finally
[root@localhost ~]# source /etc/profile
The jdk will take effect at this time, you can use java -version to verify.
2. Next install Kafka
1. Download Kafka
Go to http://kafka.apache.org/downloads.html to download the corresponding version. I am using kafka_2.9.1-0.8.2.2.tgz.
2. After downloading, unzip it to your favorite directory
I extracted it to /usr/local/kafka/kafka_2.9.1-0.8.2.2
3. Run default Kafka
Start Zookeeper server
[root@localhost kafka_2.<span>9.1</span>-<span>0.8</span>.<span>2.2</span>]# <span>sh</span> bin/zookeeper-server-start.<span>sh</span> config/zookeeper.properties &
Start Kafka server
[root@localhost kafka_2.<span>9.1</span>-<span>0.8</span>.<span>2.2</span>]# <span>sh</span> bin/kafka-server-start.<span>sh</span> config/server.properties &
Run producer producer
[root@localhost kafka_2.<span>9.1</span>-<span>0.8</span>.<span>2.2</span>]# <span>sh</span> bin/kafka-console-producer.<span>sh</span> --broker-list localhost:<span>9092</span> --topic test
Run consumer
[root@localhost kafka_2.<span>9.1</span>-<span>0.8</span>.<span>2.2</span>]# <span>sh</span> bin/kafka-console-consumer.<span>sh</span> --zookeeper localhost:<span>2181</span> --topic test --from-beginning
In this way, if you input content on the producer side, the consumer will receive it immediately.
4. When there is a cross-machine producer or consumer connection
You need to configure the host.name of config/server.properties, otherwise the cross-machine connection will not be possible.
3. Kafka-PHP extension
After using it for a while, https://github.com/nmred/kafka-php can be used.
I installed it using composer, the following is an example:
producer.php
<?<span>php </span><span>require</span> 'vendor/autoload.php'<span>; </span><span>while</span> (1<span>) { </span><span>$part</span> = <span>mt_rand</span>(0, 1<span>); </span><span>$produce</span> = \Kafka\Produce::getInstance('kafka0:2181', 3000<span>); </span><span>//</span><span> get available partitions</span> <span>$partitions</span> = <span>$produce</span>->getAvailablePartitions('topic_name'<span>); </span><span>var_dump</span>(<span>$partitions</span><span>); </span><span>//</span><span> send message</span> <span>$produce</span>->setRequireAck(-1<span>); </span><span>$produce</span>->setMessages('topic_name', 0, <span>array</span>(<span>date</span>('Y-m-d H:i:s'<span>)); </span><span>sleep</span>(3<span>); }</span>
consumer.php
<span>require</span> 'vendor/autoload.php'<span>; </span><span>$consumer</span> = \Kafka\Consumer::getInstance('kafka0:2181'<span>); </span><span>$group</span> = 'topic_name'<span>; </span><span>$consumer</span>->setGroup(<span>$group</span><span>); </span><span>$consumer</span>->setFromOffset(<span>true</span><span>); </span><span>$consumer</span>->setTopic('topic_name', 0<span>); </span><span>$consumer</span>->setMaxBytes(102400<span>); </span><span>$result</span> = <span>$consumer</span>-><span>fetch(); </span><span>print_r</span>(<span>$result</span><span>); </span><span>foreach</span> (<span>$result</span> <span>as</span> <span>$topicName</span> => <span>$partition</span><span>) { </span><span>foreach</span> (<span>$partition</span> <span>as</span> <span>$partId</span> => <span>$messageSet</span><span>) { </span><span>var_dump</span>(<span>$partition</span>-><span>getHighOffset()); </span><span>foreach</span> (<span>$messageSet</span> <span>as</span> <span>$message</span><span>) { </span><span>var_dump</span>((<span>string</span>)<span>$message</span><span>); } </span><span>var_dump</span>(<span>$partition</span>-><span>getMessageOffset()); } }</span>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to download 360 Secure Browser on your computer? It is a very secure web browser software. This browser is very rich in functions and very simple to operate. Using 360 Secure Browser to browse the web can protect user privacy and security very well. Many people like to use this browser. Browser office, but many people still don’t know how to download and install 360 Secure Browser on their computers. This article will give you a detailed introduction to the installation process of the 360 Safe Browser PC version, hoping to help you solve the problem. Overview of the installation process under the computer version of 360 Secure Browser 1. On the computer’s main page, find “360 Software Manager” and enter (as shown in the picture). 2. Open 360 Software Manager and find the search box (as shown in the picture). 3. Click Search

To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

The Goinstall command is used to compile and install Go programs. It allows projects to be built and installed locally into the $GOPATH/bin directory. Options include: -v (verbose mode), -p (parallel build), -x (show running command), -target (set target operating system and architecture), which can be used to install dependencies and exclude tests.

To open the Python you just downloaded, follow these steps: Place the installer in an easily accessible location. Double-click the installer icon and follow the on-screen instructions. Once the installation is complete, enter "python" in a command prompt or terminal window to start a Python interactive shell.

BitgetLaunchpool is a dynamic platform designed for all cryptocurrency enthusiasts. BitgetLaunchpool stands out with its unique offering. Here, you can stake your tokens to unlock more rewards, including airdrops, high returns, and a generous prize pool exclusive to early participants. What is BitgetLaunchpool? BitgetLaunchpool is a cryptocurrency platform where tokens can be staked and earned with user-friendly terms and conditions. By investing BGB or other tokens in Launchpool, users have the opportunity to receive free airdrops, earnings and participate in generous bonus pools. The income from pledged assets is calculated within T+1 hours, and the rewards are based on

How to open the interface after Python completes downloading. After the Python program is executed, no interface will be displayed by default. However, interface display can be achieved by importing the GUI library and creating a GUI window. Practical Case The following is an example of using the Tkinter library to create a simple GUI interface: importtkinterastk#Create a root window root=tk.Tk()root.title("PythonGUI")#Create a text box label=tk.Label(root,text=" Hello, Python!")#Create button button=tk.Button(ro

PHP function return value types can be expressed as type description syntax, which clearly specifies the return value type of each function. Understanding return value types is critical to creating extensions that are compatible with the PHP core engine, avoiding unexpected conversions, improving efficiency, and enhancing code readability. Specifically, extension functions can define a return value type so that the PHP engine can optimize code execution based on that type and allow developers to explicitly handle the return value. In practice, extension functions can return PHP objects, and PHP code can handle the returned results according to the return value type.
