solcloud/consumer
Composer 安装命令:
composer require solcloud/consumer
包简介
RabbitMQ base worker for queue consuming
README 文档
README
Consumer needs AMQPChannel channel as only dependency.
Channel setup
If you have channel instance in your project already than you can skip this, otherwise lets setup rabbitmq connection, we recommend to use container for this.
$config = new \Solcloud\Consumer\QueueConfig(); $config ->setHost('solcloud_rabbitmq') ->setVhost('/') #->setHeartbeatSec(5) ->setUsername('dev') ->setPassword('dev') ; $connectionFactory = new \Solcloud\Consumer\QueueConnectionFactory($config); $connection = $connectionFactory->createSocketConnection(); #(new \PhpAmqpLib\Connection\Heartbeat\PCNTLHeartbeatSender($connection))->register(); // if heartbeat and pcntl_async_signals() is available
Create channel from connection or use your own channel
/** @var \PhpAmqpLib\Channel\AMQPChannel $channel */ $channel = $connection->channel();
Worker
Create worker (consumer) class for your business logic and inject $channel dependency. You can extend AbstractConsumer for lightweight abstraction or use "solcloud standard" BaseConsumer. We will use BaseConsumer in this example
$worker = new class($channel) extends \Solcloud\Consumer\BaseConsumer { protected function run(): void { // Your hard work here echo "Processing message: " . $this->data->id . PHP_EOL; } };
Start consuming message from queue using blocking method wait
$worker->consume($consumeQueueName); while ($worker->hasCallback()) { try { // While we have callback lets enter event loop with some timeout $worker->wait(rand(8, 11)); } catch (\PhpAmqpLib\Exception\AMQPTimeoutException $ex) { echo $ex->getMessage() . PHP_EOL; } } $worker->closeChannel();
Message publishing
For message publish you can use $worker directly or use rabbitmq management plugin or different scripts
$worker->publishMessage( $worker->createMessageHelper([], ["id" => 1]), '', $consumeQueueName ); // OR open rabbitmq management and publish: {"meta":[],"data":{"id":1}}
Logging
Worker can log to Psr\Log\LoggerInterface compatible logger.
$worker->setLogger(new YourPsrLogger()); $worker->getLogger()->info('Something');
Examples
For complete example for this readme see example.php
solcloud/consumer 适用场景与选型建议
solcloud/consumer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 230 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「rabbitmq」 「AMQP」 「worker」 「AMQPLib」 「consumer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 solcloud/consumer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 solcloud/consumer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 solcloud/consumer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Queue
Work with RabbitMQ in Laravel.
Microservice RPC through message queues.
PHP AMQP Binding Library
The Listener component of the RabbitEvents library.
A Laravel package to monitor queue jobs.
统计信息
- 总下载量: 230
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-27