kafka-php生产者客户端

PHP 操作 Kafka 需要安装 librdkafka 库和 kafka 的 PHP 扩展。
1.安装 librdkafka 库
2.安装 php-kafka 扩展
二、代码实现
生产逻辑如下:

  1. 配置生产者客户端参数及创建相应的生产者实例
/**
 * 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.构建主题;

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

3.发送消息

/**
 * 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.关闭生产者实例。

/**
 * 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);

完整代码如下

<?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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值