sobirjonovs / laravel-rabbit
RabbitMQ工作简易工具
2.9.8
2024-08-10 13:47 UTC
Requires
- php: ^7.4|^8.0
- illuminate/support: >8
- php-amqplib/php-amqplib: ^3.1
- dev-master
- 2.9.8
- 2.9.7
- 2.9.6
- 2.9.5
- 2.9.4
- 2.9.3
- 2.9.2
- 2.9.1
- 2.9.0
- 2.8.7
- 2.8.6
- 2.8.5
- 2.8.4
- 2.8.3
- 2.8.2
- 2.8.1
- 2.8.0
- 2.7.0
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.1
- 2.4.0
- 2.3.9
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.9
- 2.2.8
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.4.0
- 1.3.0
- 1.2.6
- 1.2.5
- 1.2.1
- 1.2.0
- 1.1.5
- 1.1.3
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.0
- dev-patch-1
- dev-devmaster
This package is auto-updated.
Last update: 2024-09-10 14:00:49 UTC
README
安装
composer require sobirjonovs/laravel-rabbit
安装包后,使用rabbit:install
Artisan命令发布其资产。
php artisan rabbit:install
设置
config/amqp.php
- RabbitMQ设置config/rabbit_events.php
- 编写负责从另一个微服务发出的事件的方法app/Providers/RabbitServicePRovider
- 编写在运行时声明的队列
示例
1. 消费消息
<?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. 发布消息
<?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. 同时发布和消费消息
<?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 ?>
开发
您可以在config/rabbit_events.php
中注册您的事件,如下所示
<?php use App\Rabbitmq\Dto\Products\ProductServiceObject; use App\Rabbitmq\Services\ProductService; return [ 'createProduct' => [ 'class' => ProductService::class, 'method' => 'createProduct', 'dto' => ProductServiceObject::class ] ];
现在您需要一个新服务类和DTO(数据传输对象)类。
要创建一个具有函数的新服务类,请使用以下命令
php artisan make:rabbit-service ProductService createProduct
这将创建一个名为ProductService
的新服务类,其中包含名为createProduct
的函数。
它还创建了一个DTO类,该类将在createProduct
函数中使用。
生成的文件将位于以下目录中
- 服务类:App/Services/ProductService.php
- 数据传输对象(DTO):App/Services/Dto/ProductServiceObject.php
如果您想更改服务类和DTO类的命名空间,您可以在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', ];
附加
当应用程序接收消息时,如果发生异常。
该消息将被发送到dead-letter-queue
,即config('amqp.dead_letter_queue')
。
如果消息未通过验证,则将消息发送到invalid-queue-letter
,即config('amqp.invalid_letter_queue')
。
如果您在'config/amqp.php'文件中未定义invalid_letter_queue
,则消息将被删除。
注意:这两种选项仅在发布者和订阅者模式中工作。
<?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, ];
队列
您需要在config/amqp.php
中配置您的队列。
... 'config' => [ 'default_queue' => 'default', 'is_multi_queue' => false, 'queues' => ['default', 'foo', 'bar'], ],