Kafka學習筆記4--Kafka生產者的客戶端(PHP)開發

1、準備工做

雖然 Kafka 是用 Java/Scala 語言編寫的,但這不妨礙它對多語言的支持。能夠在 Kafka 官網的 CLIENTS 查看 Kafka 支持的語言,其中包括 C/C++、Python、Go 等語言。php

PHP 操做 Kafka 須要安裝 librdkafka 庫和 kafka 的 PHP 擴展。
1.安裝 librdkafka 庫git

git clone https://github.com/edenhill/librdkafka.git
./configure
 make
 sudo make install

2.安裝 php-kafka 擴展github

$ git clone https://github.com/arnaud-lb/php-rdkafka.git
$ cd librdkafka/
$ phpize
$ ./configure 
$ make
$ sudo make install

#在php.ini 文件中配置 rdkafka擴展
extension=rdkafka.so

#查看擴展是否生效
php -m | grep kafka

2、代碼實現

demo 來源於 https://github.com/arnaud-lb/php-rdkafka#examplesapache

正常的生產邏輯以下:
1.配置生產者客戶端參數及建立相應的生產者實例;bootstrap

/**
 * Create a producer
 */
$conf = new RdKafka\Conf();
$conf->set('log_level', LOG_DEBUG);
//$conf->set('debug', 'all');
$rk = new RdKafka\Producer($conf);
$rk->addBrokers("127.0.0.1");

2.構建主題;debug

/**
 * Create a topic instance from the producer
 */
$topic = $rk->newTopic("test");

3.發送消息;code

/**
 * Producing messages
 * The first argument is the partition. RD_KAFKA_PARTITION_UA stands for unassigned, and lets librdkafka choose the partition.
 * 第一個參數是分區,RD_KAFKA_PARTITION_UA 表示未分配,而且由 librdkafka 選擇分區。
 * The second argument are message flags and should be either 0 or RD_KAFKA_MSG_F_BLOCK to block produce on full queue.
 * 第二個參數是消息標誌,爲 0 或 RD_KAFKA_MSG_F_BLOCK,當隊列滿了時阻止生產消息。
 * The message payload can be anything.
 * 消息能夠是任何內容。
 */
$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message payload");

4.關閉生產者實例。orm

/**
 * Proper shutdown
 * This should be done prior to destroying a producer instance
 *   to make sure all queued and in-flight produce requests are completed before terminating.
 * 關閉生產者實例前需確保全部在隊列中和正在生產的生產請求都已完成。
 * Not calling flush can lead to message loss!
 * 不調用flush會致使消息丟失!
 */
$timeout_ms = 60000; // 1 minute
$rk->flush($timeout_ms);

檢驗消息是否發送成功
終端開啓一個消費者:server

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test

在另外一個窗口執行 php producer.php,
kafka1.pngblog

可看到消費者終端接收到消息。
kafka2.png

完整代碼以下:

<?php
/**
 * Created by PhpStorm.
 * User: liulu
 * Date: 2020/1/1
 * Time: 18:38
 */

/**
 * Create a producer
 */
$conf = new RdKafka\Conf();
$conf->set('log_level', LOG_DEBUG);
//$conf->set('debug', 'all');
$rk = new RdKafka\Producer($conf);
$rk->addBrokers("127.0.0.1");

/**
 * Create a topic instance from the producer
 */
$topic = $rk->newTopic("test");

/**
 * Producing messages
 * The first argument is the partition. RD_KAFKA_PARTITION_UA stands for unassigned, and lets librdkafka choose the partition.
 * 第一個參數是分區,RD_KAFKA_PARTITION_UA 表示未分配,而且由 librdkafka 選擇分區。
 * The second argument are message flags and should be either 0 or RD_KAFKA_MSG_F_BLOCK to block produce on full queue.
 * 第二個參數是消息標誌,爲 0 或 RD_KAFKA_MSG_F_BLOCK,當隊列滿了時阻止生產消息。
 * The message payload can be anything.
 * 消息能夠是任何內容。
 */
$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message payload");

/**
 * Proper shutdown
 * This should be done prior to destroying a producer instance
 *   to make sure all queued and in-flight produce requests are completed before terminating.
 * 關閉生產者實例前需確保全部在隊列中和正在生產的生產請求都已完成。
 * Not calling flush can lead to message loss!
 * 不調用flush會致使消息丟失!
 */
$timeout_ms = 60000; // 1 minute
$rk->flush($timeout_ms);

echo 'finished';exit;
相關文章
相關標籤/搜索