arquivei/php-kafka-consumer
Composer 安装命令:
composer create-project arquivei/php-kafka-consumer
包简介
A consumer of Kafka in PHP
README 文档
README
An Apache Kafka consumer in PHP. Subscribe to topics and define callbacks to handle the messages.
Requirements
In order to use this library, you'll need the php-rdkafka PECL extension. Please notice that the extension requires the librdkafka C library.
Minimum requirements:
| Dependency | version |
|---|---|
| librdkafka | v1.5.3 |
| PHP | 7.4 + |
| ext-rdkafka | 3.0 + |
| Laravel | 6 + |
Install
Using composer:
composer require arquivei/php-kafka-consumer
Usage
<?php require_once 'vendor/autoload.php'; use Kafka\Consumer\ConsumerBuilder; use Kafka\Consumer\Entities\Config\Sasl; class DefaultConsumer { public function __invoke(string $message): void { print 'Init: ' . date('Y-m-d H:i:s') . PHP_EOL; sleep(2); print 'Finish: ' . date('Y-m-d H:i:s') . PHP_EOL; } } $consumer = ConsumerBuilder::create('broker:port', 'php-kafka-consumer-group-id', ['topic']) ->withSasl(new Sasl('username', 'pasword', 'mechanisms')) ->withCommitBatchSize(1) ->withSecurityProtocol('security-protocol') ->withHandler(new DefaultConsumer()) // or any callable ->build(); $consumer->consume();
Or by using the legacy API:
<?php require_once 'vendor/autoload.php'; use Kafka\Consumer\Contracts\Consumer; use Kafka\Consumer\Entities\Config; use Kafka\Consumer\Entities\Config\Sasl; class DefaultConsumer extends Consumer { public function handle(string $message): void { print 'Init: ' . date('Y-m-d H:i:s') . PHP_EOL; sleep(2); print 'Finish: ' . date('Y-m-d H:i:s') . PHP_EOL; } } $config = new Config( new Sasl( 'username', 'password', 'mechanisms' ), ['topic'], 'broker:port', 1, 'php-kafka-consumer-group-id', new DefaultConsumer(), 'PLAINTEXT', 'topic-dlq', 1, 6 ); $consumer = new \Kafka\Consumer\Consumer($config); $consumer->consume();
Usage with Laravel
You need to add the php-kafka-consig.php in config path:
<?php return [ 'topic' => 'topic', 'broker' => 'broker', 'groupId' => 'group-id', 'securityProtocol' => 'security-protocol', 'sasl' => [ 'mechanisms' => 'mechanisms', 'username' => 'username', 'password' => 'password', ], ];
Use the command to execute the consumer:
$ php artisan arquivei:php-kafka-consumer --consumer="App\Consumers\YourConsumer" --commit=1
Middlewares
Middlewares are simple callables that receive two arguments: the message being handled and the next handler. Some possible use cases for middlewares: message transformation, filtering, logging stuff, or even transaction handling, your imagination is the limit.
<?php use Kafka\Consumer\ConsumerBuilder; $consumer = ConsumerBuilder::create('broker:port', 'php-kafka-consumer-group-id', ['topic']) ->withHandler(function ($message) {/** ... */}) // You may add any number of middlewares, they will be executed in the order provided ->withMiddleware(function (string $rawMessage, callable $next): void { $decoded = json_decode($rawMessage, true); $next($decoded); }) ->withMiddleware(function (array $message, callable $next): void { if (! isset($message['foo'])) { return; } $next($message); }) ->build(); $consumer->consume();
Build and test
If you want to contribute, there are a few utilities that will help.
First create a container:
docker compose up -d --build
If you have make, you can use pre defined commands in the Makefile
make build
Then install the dependencies:
docker compose exec php-fpm composer install
or with make:
make composer install
You can run tests locally:
docker compose exec php-fpm ./vendor/phpunit/phpunit/phpunit tests
or with make:
make test
and check for coverage:
docker compose exec php-fpm phpdbg -qrr ./vendor/bin/phpunit --whitelist src/ --coverage-html coverage/
or with make:
make coverage
arquivei/php-kafka-consumer 适用场景与选型建议
arquivei/php-kafka-consumer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 78.68k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2019 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「kafka」 「consumer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arquivei/php-kafka-consumer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arquivei/php-kafka-consumer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arquivei/php-kafka-consumer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Simple Kafka library
Consumer of sqs message
Laravel Kafka Queue Driver
Simple queues implementation in PHP through database.
RabbitMQ to sql consumer, using doctrine/dbal and php-amqplib/rabbitmq-bundle
Laminas API Tools Client API Service Consumer
统计信息
- 总下载量: 78.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 16
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-01