pablicio/mirabel-rabbitmq
Composer 安装命令:
composer require pablicio/mirabel-rabbitmq
包简介
Library to facilitate the use of rabbitmq within php based on the php-amqplib library, bringing an abstraction of its use to make its use more simplified.
README 文档
README
mirabel-rabbitmq
Library to facilitate the use of rabbitmq within php based on the php-amqplib library, bringing an abstraction of its use to make it simpler.
Installing
composer require pablicio/mirabel-rabbitmq
How to configure in Laravel
Run the publisher and it will create the file in config/mirabel_rabbitmq.php
php artisan vendor:publish --provider="Pablicio\MirabelRabbitmq\MirabelRabbitmqServiceProvider"
Then just configure according to your environment.
<?php return [ 'connections' => [ 'rabbitmq-php' => [ 'host' => env('MB_RABBITMQ_HOST', 'localhost'), 'port' => env('MB_RABBITMQ_PORT', 5672), 'user' => env('MB_RABBITMQ_USER', 'guest'), 'password' => env('MB_RABBITMQ_PASSWORD', 'guest'), 'exchange' => env('MB_RABBITMQ_EXCHANGE', 'my-exchange'), 'exchange_type' => env('MB_RABBITMQ_EXCHANGE_TYPE', 'topic'), 'exchange_passive' => env('MB_RABBITMQ_EXCHANGE_PASSIVE', false), 'exchange_durable' => env('MB_RABBITMQ_EXCHANGE_DURABLE', true), 'exchange_auto_delete' => env('MB_RABBITMQ_EXCHANGE_DELETE', false), 'exchange_nowait' => env('MB_RABBITMQ_EXCHANGE_NOWAIT', false), 'exchange_arguments' => env('MB_RABBITMQ_EXCHANGE_ARGUMENTS', []), 'exchange_ticket' => env('MB_RABBITMQ_EXCHANGE_TICKET', null) ], ] ];
Usage examples
Creating a publisher class
<?php namespace App\Events; use Pablicio\MirabelRabbitmq\RabbitMQEventsConnection; class OrderReceivedEvent { use RabbitMQEventsConnection; const ROUTING_KEY = 'my-service.request-orders.received'; function __construct($payload) { $this->routingKey = self::ROUTING_KEY; $this->payload = $payload; } }
How to call the publisher
(new App\Events\OrderReceivedEvent('ReceivedPayload'))->publish()
Creating a listener class
<?php namespace App\Workers; use Pablicio\MirabelRabbitmq\RabbitMQWorkersConnection; class OrderReceivedWorker { use RabbitMQWorkersConnection; const QUEUE = 'my-service.request-test', routing_keys = [ 'my-service.request-orders.received' ], options = [ 'exchange_type' => 'topic' ], retry_options = [ 'x-message-ttl' => 1000, 'max-attempts' => 8 ]; public function work($msg) { try { print_r($msg->body); return $this->ack($msg); } catch (\Exception $e) { return $this->nack($msg); } } }
How to call the subscriber
(new App\Workers\OrderReceivedWorker)->subscribe();
Functions
| Worker Functions | Description | Return |
|---|---|---|
| work($msg) | Function performed by the callback to process the messages | null |
| ack($msg) | Accept message and remove from queue | 'ack' |
| nack($msg) | When there is an error, it sends the message to the retry queue, when the attempts are over, it sends it to the error queue | 'nack' |
| reject($msg) | Reject the message | 'reject' |
options params
| Param | Required | Default | Type |
|---|---|---|---|
| exchange_type | No | 'topic' | String |
| exchange_passive | No | false | Boolean |
| exchange_durable | No | true | Boolean |
| exchange_auto_delete | No | false | Boolean |
| exchange_internal | No | false | Boolean |
| exchange_no_wait | No | false | Boolean |
| exchange_arguments | No | [] | Array |
| exchange_ticket | No | null | Object |
| queue_passive | No | false | Boolean |
| queue_durable | No | true | Boolean |
| queue_exclusive | No | false | Boolean |
| queue_auto_delete | No | false | Boolean |
| queue_nowait | No | false | Boolean |
| qos_prefetch_size | No | 0 | Integer |
| qos_prefetch_count | No | 1 | Integer |
| qos_a_global | No | null | Boolean |
| consume_consumer_tag | No | '' | String |
| consume_no_local | No | false | Boolean |
| consume_no_ack | No | false | Boolean |
| consume_exclusive | No | false | Boolean |
| consume_nowait | No | false | Boolean |
| consume_ticket | No | false | Object |
| x-dead-letter-exchange | No | '' | String |
| x-dead-letter-routing-key | No | $retryQueue | String |
The options array is required to declare. case [], we will assume the settings of .env
retry_options params
| Param | Required | Default | Type |
|---|---|---|---|
| retry_exchange_type | No | 'topic' | String |
| retry_exchange_passive | No | false | Boolean |
| retry_exchange_durable | No | true | Boolean |
| retry_exchange_auto_delete | No | false | Boolean |
| retry_exchange_internal | No | false | Boolean |
| retry_exchange_no_wait | No | false | Boolean |
| retry_exchange_arguments | No | [] | Array |
| retry_exchange_ticket | No | null | Object |
| retry_queue_passive | No | false | Boolean |
| retry_queue_durable | No | true | Boolean |
| retry_queue_exclusive | No | false | Boolean |
| retry_queue_auto_delete | No | false | Boolean |
| retry_queue_nowait | No | false | Boolean |
| x-dead-letter-exchange | No | '' | String |
| x-dead-letter-routing-key | No | $queue | String |
| x-message-ttl | No | 0 | Integer |
| max-attempts | No | 1 | Integer |
If you pass the options array empty, we assume the .env settings, if you don't want to use retry, just remove the retry_options array.
Disclaimerm we are not responsible for its use
This library is still under development, so it does not work correctly in many scenarios, it is for testing and learning, if you use it you do so at your own risk. I will return to it very soon to evolve, but I need the community's help for this, if you want PRs to feel like owners, my contact email is pabliciotjg@gmail.com
Todo
- Add observability attributes as parameters in the publish() method such as traker_id and more metadatas.
- Become agnostic to other frameworks
- Add unit tests
- Improve the documentation
pablicio/mirabel-rabbitmq 适用场景与选型建议
pablicio/mirabel-rabbitmq 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 263 次下载、GitHub Stars 达 8, 最近一次更新时间为 2023 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pablicio/mirabel-rabbitmq 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pablicio/mirabel-rabbitmq 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 263
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2023-04-01
