fatihtuzlu/php-apache-kafka
Composer 安装命令:
composer require fatihtuzlu/php-apache-kafka
包简介
You can manage your multidata with this package
README 文档
README
PHP Kafka CDC is a package developed by Fatih Tuzlu for managing multidata with Apache Kafka in PHP applications. It provides functionalities for both consuming and producing messages using Kafka.
Remember
Do not forget to install rdkafka library
Macos
brew install librdkafka
Debian
$ apt install librdkafka-dev
RedHat, CentOS, Fedora
$ yum install librdkafka-devel
Windows
# Install vcpkg if not already installed
$ git clone https://github.com/Microsoft/vcpkg.git
$ cd vcpkg
$ ./bootstrap-vcpkg.sh
$ ./vcpkg integrate install
# Install librdkafka
$ vcpkg install librdkafka
For Detail
You can visit https://github.com/confluentinc/librdkafka
Installation
You can install PHP Kafka CDC via Composer. Run the following command in your terminal:
composer require fatihtuzlu/php-apache-kafka
Requirements
PHP Kafka CDC requires the following dependencies:
idealo/php-rdkafka-ffi: ^0.5.0
php: ^8.0.0
Usage
Consumer
To consume messages from Kafka topics, you can use the ConsumerManager class. Here's a basic example of how to set up a consumer:
use Fatihtuzlu\PHPKafkaCDC\ConsumerManager; // Initialize consumer manager $consumer = new ConsumerManager( $topic, // Kafka topic to consume from $groupId, // Consumer group ID $bootstrapServers // Kafka bootstrap servers ); // Fetch messages from Kafka $message = $consumer->fetchMessages($topic, $partition, $offset, $timeMs);
Producer
To produce messages to Kafka topics, you can use the ProducerManager class. Here's a basic example of how to set up a producer:
use Fatihtuzlu\PHPKafkaCDC\ProducerManager; // Initialize producer manager $producer = new ProducerManager( $topic, // Kafka topic to produce to $bootstrapServers // Kafka bootstrap servers ); // Send a message to Kafka $producer->sendMessages($message, $flush = 1000, $debug = false);
Configuration
Debugging
You can enable debugging for your Kafka connection by calling the debug() method:
$producer->debug();
Additional Brokers
$producer->addBrokers($brokerList);
Examples
Sample Consumer
use Fatihtuzlu\PHPKafkaCDC\ConsumerManager; // Kafka broker and topic information $bootstrapServers = "localhost:9092"; $topic = "my_topic"; $groupId = "my_group"; // Initialize the consumer $consumer = new ConsumerManager($topic, $groupId, $bootstrapServers); // Fetch messages $message = $consumer->fetchMessages($topic, 0, RD_KAFKA_OFFSET_BEGINNING); // Process the received message if ($message !== null) { echo "Received message: " . $message->payload . "\n"; } else { echo "No messages available.\n"; }
Sample Producer
use Fatihtuzlu\PHPKafkaCDC\ProducerManager; // Kafka broker and topic information $bootstrapServers = "localhost:9092"; $topic = "my_topic"; // Initialize the producer $producer = new ProducerManager($topic, $bootstrapServers); // Send a message $messageToSend = "Hello, Kafka!"; $producer->sendMessages($messageToSend); echo "Message sent to Kafka: " . $messageToSend . "\n";
In these examples, the consumer retrieves messages from Kafka, while the producer sends messages to Kafka. my_topic represents the target topic in Kafka. Make sure to update the configuration settings according to your own Kafka cluster.
fatihtuzlu/php-apache-kafka 适用场景与选型建议
fatihtuzlu/php-apache-kafka 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 fatihtuzlu/php-apache-kafka 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fatihtuzlu/php-apache-kafka 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-10