Home Backend Development PHP Tutorial PHP and Apache Kafka integration for efficient message queuing and distribution

PHP and Apache Kafka integration for efficient message queuing and distribution

Jun 25, 2023 am 09:48 AM
php message queue apache kafka

With the continuous development of modern Internet applications, more and more applications need to handle large amounts of data communication. The traditional way of handling these data communications is to use polling or blocking I/O, but these methods can no longer meet the needs of modern applications because they are very inefficient. In order to solve this problem, the industry has developed a technology called message queue and distribution system.

In the message queue and distribution system, the producer of the message sends the message to the queue, and the consumer of the message obtains the message from the queue and performs corresponding operations. This approach can greatly improve the efficiency of data communication because it can avoid problems such as polling and blocking I/O.

In this article, we will discuss how to achieve efficient message queuing and distribution using PHP and Apache Kafka integration.

Introduction to Apache Kafka

Apache Kafka is a high-throughput, low-latency, scalable distributed messaging system. It can handle large volumes of messages and scale horizontally to accommodate higher loads. The main components of Apache Kafka include:

  1. Broker: Each node in the Kafka cluster is a broker, and they are responsible for the storage and forwarding of messages.
  2. Topic: Each message must be assigned to a topic, which is the logical concept of message production and consumption.
  3. Partition: Each topic can be divided into multiple partitions, and each partition contains multiple ordered messages.
  4. Producer: Message producer, sends messages to broker.
  5. Consumer: Message consumer, reads messages from the broker.
  6. Consumer Group: A group of consumers jointly consume messages in one or more partitions.
  7. Offset: The number of the message, used to uniquely identify a message.

PHP integrates Apache Kafka

In order to use Apache Kafka, we need to use the Kafka extension of PHP. This extension provides all the APIs required by PHP to operate Kafka.

First, we need to install the Kafka extension. We can install it from PECL:

pecl install kafka
Copy after login

After installing the extension, you can start using it. The following is a simple example of message production and consumption using PHP and Apache Kafka:

<?php
$brokers = 'kafka:9092';    // Kafka集群地址
$topic = 'test';            // Topic名称

// 创建一个Kafka生产者
$producer = new RdKafkaProducer();
$producer->setLogLevel(LOG_DEBUG);
$producer->addBrokers($brokers);

// 创建一个Kafka消费者
$conf = new RdKafkaConf();
$conf->set('group.id', 'myGroup');
$consumer = new RdKafkaConsumer($conf);
$consumer->addBrokers($brokers);

// 生产消息
$topicProducer = $producer->newTopic($topic);
for ($i = 0; $i < 10; $i++) {
    $topicProducer->produce(RD_KAFKA_PARTITION_UA, 0, 'Message ' . $i);
}

// 消费消息
$topicConsumer = $consumer->newTopic($topic);
$topicConsumer->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING);
while (true) {
    $message = $topicConsumer->consume(0, 1000);
    if (null === $message) {
        continue;
    }
    if ($message->err) {
        throw new Exception('Error occurred while consuming message');
    }
    echo $message->payload . PHP_EOL;
}
Copy after login

In this example, we first create a Kafka producer and a Kafka consumer. Then, in the producer, we sent 10 messages to the specified topic; in the consumer, we consumed the messages from the specified topic and output their contents.

At this point, we have successfully implemented simple message production and consumption using PHP and Apache Kafka. Next, we'll discuss how to implement more advanced functionality using PHP and Apache Kafka.

Advanced application examples

In actual applications, we usually need to implement some advanced functions, such as:

  1. Message distribution: send messages to specified consumers .
  2. Consumer group: allows multiple consumers to jointly consume messages in one or more topics.
  3. offset configuration: allows controlling the reading position of messages.

Here we will discuss how to implement these functions.

Message Distribution

In practical applications, we usually need to control the flow of messages. For example, we may want only certain consumers to consume certain messages. To achieve this functionality, we can create a queue for each consumer and then assign specific messages to specific queues.

The following is an example that uses two consumers to consume two different tasks.

<?php

$brokers = 'kafka:9092';    // Kafka集群地址
$topic = 'test';            // Topic名称

// 创建一个Kafka消费者组
$conf = new RdKafkaConf();
$conf->set('group.id', 'myGroup');
$consumer = new RdKafkaKafkaConsumer($conf);
$consumer->subscribe([$topic]);

// 创建两个Kafka生产者,一个生产者用于向消费者1发送消息,另一个生产者用于向消费者2发送消息
$producer1 = new RdKafkaProducer();
$producer1->addBrokers($brokers);
$producer1Topic = $producer1->newTopic($topic . '_1');

$producer2 = new RdKafkaProducer();
$producer2->addBrokers($brokers);
$producer2Topic = $producer2->newTopic($topic . '_2');

// 消费消息
while (true) {
    $message = $consumer->consume(1000);
    if (null === $message) {
        continue;
    }
    if ($message->err) {
        throw new Exception('Error occurred while consuming message');
    }

    echo 'Received message: ' . $message->payload . PHP_EOL;

    // 根据消息内容分配给不同的生产者
    if ($message->payload === 'task1') {
        $producer1Topic->produce(RD_KAFKA_PARTITION_UA, 0, $message->payload);
    } elseif ($message->payload === 'task2') {
        $producer2Topic->produce(RD_KAFKA_PARTITION_UA, 0, $message->payload);
    }
}
Copy after login

In this example, we use two producers to distribute messages to two different consumers. When a consumer receives a message, we can assign it to a specific producer based on the message content. This method can help us control the flow of messages and avoid redundant processing of messages.

Consumer Group

In ordinary Kafka consumers, different consumers in the same group consume the same topic and they will receive the same message. This is because Kafka automatically balances partitions and ensures that each partition is processed by only one consumer.

In PHP, we can use group.id to group consumers to realize the function of consumer groups.

The following is an example of a Kafka consumer group that can process messages within the same group in parallel:

<?php

$brokers = 'kafka:9092';    // Kafka集群地址
$topic = 'test';            // Topic名称

// 创建一个Kafka消费者组
$conf = new RdKafkaConf();
$conf->set('group.id', 'myGroup');
$conf->set('metadata.broker.list', $brokers);
$conf->set('enable.auto.commit', 'false');
$consumer = new RdKafkaKafkaConsumer($conf);

// 添加需要订阅的topic
$consumer->subscribe([$topic]);

// 处理消息
while (true) {
    $message = $consumer->consume(1000);
    if (null === $message) {
        continue;
    }
    if ($message->err) {
        throw new Exception('Error occurred while consuming message');
    }

    echo 'Received message: ' . $message->payload . PHP_EOL;

    // 处理完消息后手动提交offset
    $consumer->commit();
}
Copy after login

In this example, we create a Kafka consumer group and send it to Added topics that require subscription. We can then process messages within the same group in parallel.

Note: In a consumer group, multiple consumers consume one or more partitions together. When consuming data, you need to pay attention to the issue of multi-thread processing of the same data.

Offset configuration

In Kafka, each partition has an independent offset. The consumer can control where in the partition it reads and thus which messages it reads. The consumer can start reading from the last message or the latest message.

In PHP, we can use offset to control the reading position of messages. The following is an example of Offset configuration:

<?php

$brokers = 'kafka:9092';    // Kafka集群地址
$topic = 'test';            // Topic名称

// 创建一个Kafka消费者
$conf = new RdKafkaConf();
$conf->set('group.id', 'myGroup');
$consumer = new RdKafkaKafkaConsumer($conf);

// 订阅topic
$topicConf = new RdKafkaTopicConf();
$topicConf->set('auto.offset.reset', 'earliest');
$topic = $consumer->newTopic($topic, $topicConf);
$topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);

// 消费消息
while (true) {
    $message = $topic->consume(0, 1000);
    if (null === $message) {
        continue;
    }
    if ($message->err) {
        throw new Exception('Error occurred while consuming message');
    }

    echo 'Received message: ' . $message->payload . PHP_EOL;
}
Copy after login

In this example, we use auto.offset.reset to set the offset configuration. This configuration tells the consumer to start consuming messages from the earliest offset.

In actual applications, different offsets can be configured according to needs. For example, after the producer fails to process some messages, we may need to restart reading messages from the point where the failed message was previously processed.

Conclusion

In this article, we discussed how to achieve efficient message queuing and distribution using PHP and Apache Kafka integration. We first introduced the basics of Apache Kafka and then discussed how to use the Kafka extension for PHP to implement the production and consumption of messages. Finally, we discussed how to implement some advanced features such as message distribution, consumer groups, and offset configuration.

Using PHP and Apache Kafka integration allows us to implement efficient message queuing and distribution, thereby improving application response speed and throughput. If you are developing an application that needs to handle large amounts of data communication, Apache Kafka and the Kafka extension for PHP may be a good choice.

The above is the detailed content of PHP and Apache Kafka integration for efficient message queuing and distribution. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles