开源软件名称(OpenSource Name):vyuldashev/laravel-queue-rabbitmq开源软件地址(OpenSource Url):https://github.com/vyuldashev/laravel-queue-rabbitmq开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):RabbitMQ Queue driver for LaravelSupport PolicyOnly the latest version will get new features. Bug fixes will be provided using the following scheme:
InstallationYou can install this package via composer using this command:
The package will automatically register itself. Add connection to 'connections' => [
// ...
'rabbitmq' => [
'driver' => 'rabbitmq',
'queue' => env('RABBITMQ_QUEUE', 'default'),
'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
'hosts' => [
[
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
'port' => env('RABBITMQ_PORT', 5672),
'user' => env('RABBITMQ_USER', 'guest'),
'password' => env('RABBITMQ_PASSWORD', 'guest'),
'vhost' => env('RABBITMQ_VHOST', '/'),
],
],
'options' => [
'ssl_options' => [
'cafile' => env('RABBITMQ_SSL_CAFILE', null),
'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
],
'queue' => [
'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
],
],
/*
* Set to "horizon" if you wish to use Laravel Horizon.
*/
'worker' => env('RABBITMQ_WORKER', 'default'),
],
// ...
], Optional ConfigOptionally add queue options to the config of a connection. Every queue created for this connection, get's the properties. When you want to prioritize messages when they were delayed, then this is possible by adding extra options.
'connections' => [
// ...
'rabbitmq' => [
// ...
'options' => [
'queue' => [
// ...
'prioritize_delayed' => false,
'queue_max_priority' => 10,
],
],
],
// ...
], When you want to publish messages against an exchange with routing-key's, then this is possible by adding extra options.
'connections' => [
// ...
'rabbitmq' => [
// ...
'options' => [
'queue' => [
// ...
'exchange' => 'application-x',
'exchange_type' => 'topic',
'exchange_routing_key' => '',
],
],
],
// ...
], In Laravel failed jobs are stored into the database. But maybe you want to instruct some other process to also do something with the message. When you want to instruct RabbitMQ to reroute failed messages to a exchange or a specific queue, then this is possible by adding extra options.
'connections' => [
// ...
'rabbitmq' => [
// ...
'options' => [
'queue' => [
// ...
'reroute_failed' => true,
'failed_exchange' => 'failed-exchange',
'failed_routing_key' => 'application-x.%s',
],
],
],
// ...
], Use your own RabbitMQJob classSometimes you have to work with messages published by another application. You can extend the build-in An example for the config: 'connections' => [
// ...
'rabbitmq' => [
// ...
'options' => [
'queue' => [
// ...
'job' => \App\Queue\Jobs\RabbitMQJob::class,
],
],
],
// ...
], An example of your own job class: <?php
namespace App\Queue\Jobs;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
class RabbitMQJob extends BaseJob
{
/**
* Fire the job.
*
* @return void
*/
public function fire()
{
$payload = $this->payload();
$class = WhatheverClassNameToExecute::class;
$method = 'handle';
($this->instance = $this->resolve($class))->{$method}($this, $payload);
$this->delete();
}
} Or maybe you want to add extra properties to the payload: <?php
namespace App\Queue\Jobs;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
class RabbitMQJob extends BaseJob
{
/**
* Get the decoded body of the job.
*
* @return array
*/
public function payload()
{
return [
'job' => 'WhatheverFullyQualifiedClassNameToExecute@handle',
'data' => json_decode($this->getRawBody(), true)
];
}
} Laravel UsageOnce you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to change anything else. If you do not know how to use Queue API, please refer to the official Laravel documentation: http://laravel.com/docs/queues Laravel Horizon UsageStarting with 8.0, this package supports Laravel Horizon out of the box. Firstly, install Horizon and then set Lumen UsageFor Lumen usage the service provider should be registered manually as follow in $app->register(VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class); Consuming MessagesThere are two ways of consuming messages.
TestingSetup RabbitMQ using docker-compose up -d rabbitmq To run the test suite you can use the following commands: # To run both style and unit tests.
composer test
# To run only style tests.
composer test:style
# To run only unit tests.
composer test:unit If you receive any errors from the style tests, you can automatically fix most, if not all of the issues with the following command: composer fix:style ContributionYou can contribute to this package by discovering bugs and opening issues. Please, add to which version of package you create pull request or issue. (e.g. [5.2] Fatal error on delayed job) |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论