sobirjonovs / laravel-rabbit
Easy tool for working with RabbitMQ
Installs: 1 516
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 2
Forks: 3
Open Issues: 1
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-11-10 14:21:23 UTC
README
Installation
composer require sobirjonovs/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'], ],