weldon/laravel-rabbit
Composer 安装命令:
composer require weldon/laravel-rabbit
包简介
Easy tool for working with RabbitMQ
README 文档
README
Installation
composer require weldon/laravel-rabbit
After installing the package, publish its assets using the rabbit:install Artisan command.
php artisan rabbit:install
Settings
config/amqp.php- RabbitMQ settingsconfig/rabbit_events.php- Write methods what they are responsible for the events dispatched from another microserviceapp/Providers/RabbitServicePRovider- Write queues what to be declared at runtime
Examples
1. Consuming messages
<?php use App\Rabbitmq\Rabbit\Client; use PhpAmqpLib\Message\AMQPMessage; $client = app(Client::class); $client->consume('queue-one', function (AMQPMessage $message) { /** * Acknowledge a message */ $message->ack(true); /** * @var Client $this */ $this->dispatchEvents($message); })->wait(); // or waitForever(); ?>
2. Publishing message
<?php use App\Rabbitmq\Rabbit\Client; use PhpAmqpLib\Message\AMQPMessage; $client = app(Client::class); $client->setMessage([ 'method' => $method, 'params' => $object->all() ])->publish('queue-one'); ?>
3. Publishing and consuming a message at once
<?php use App\Rabbitmq\Rabbit\Client; use PhpAmqpLib\Message\AMQPMessage; $client = app(Client::class); $result = $client->setMessage([ 'method' => 'methodName', 'params' => ['param1' => 'value1'] ])->request()->getResult(); # you can pass a queue name inside a request method, otherwise it uses the default queue ?>
Development
You can register your events in config/rabbit_events.php like that
<?php use App\Rabbitmq\Dto\Products\ProductServiceObject; use App\Rabbitmq\Services\ProductService; return [ 'createProduct' => [ 'class' => ProductService::class, 'method' => 'createProduct', 'dto' => ProductServiceObject::class ] ];
Now you need a new service and DTO (Data Transfer Object) classes.
To create a new service class with a function, use the following command:
php artisan make:rabbit-service ProductService createProduct
This will create a new service class named ProductService with a function named createProduct.
It also creates DTO class which will accepted in createProduct function.
The generated files will be located in the following directories:
- Service class: App/Services/ProductService.php
- Data Transfer Object (DTO): App/Services/Dto/ProductServiceObject.php
If you want to change namespace of service and DTO classes, you can replace them in config/amqp.php.
<?php . . . /** * Namespace of data service classes * * It will be merged to root namespace which is App/config('amqp.service_namespace') */ 'service_namespace' => 'Services', /** * Namespace of data transfer object * * It will be merged to root namespace which is App/config('amqp.service_namespace')/config('amqp.dto_namespace') */ 'dto_namespace' => 'Dto', ];
Additional
While application is accepting message, if exception is happened.
The message will be sent to dead-letter-queue which is config('amqp.dead_letter_queue').
The message may not pass from validation, in it the message will be sent to invalid-queue-letter which is config('amqp.invalid_letter_queue').
If you don't define invalid_letter_queue in your 'config/amqp.php' file. The message will be deleted.
Note that: The both options only work in publisher and subscriber mode.
<?php . . . /** * When error is out in service class, that message will be sent to config('amqp.dead-letter-queue') * * This works only subscriber and publisher mode */ 'dead_letter_queue' => 'dead-letter-queue', /** * When message cannot pass validation, the message will be sent to this queue * If name of this queue is null, the message will be deleted * * This works only subscriber and publisher mode */ 'invalid_letter_queue' => null, ];
Queues
You need to configure your queues in config/amqp.php.
... 'config' => [ 'default_queue' => 'default', 'is_multi_queue' => false, 'queues' => ['default', 'foo', 'bar'], ],
weldon/laravel-rabbit 适用场景与选型建议
weldon/laravel-rabbit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 201 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「client」 「rabbitmq」 「rabbit」 「laravel rabbitmq」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 weldon/laravel-rabbit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 weldon/laravel-rabbit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 weldon/laravel-rabbit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
AMQP module for Zend Framework 2 to integrate RabbitMQ
Work with RabbitMQ in Laravel.
PHP's implementation of the library providing data synchronization between microservices
Microservice RPC through message queues.
PHP AMQP Binding Library
Integrates php-amqplib with RabbitMq and Nette Framework
统计信息
- 总下载量: 201
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-21