mostafaabdelbaset / laravel-queue-rabbitmq
Laravel Queue 的 RabbitMQ 驱动器
v7.1.5
2019-03-28 18:13 UTC
Requires
- php: ^7.1.3
- ext-json: *
- enqueue/amqp-lib: 0.8.*
- illuminate/database: 5.6.*|5.7.*
- illuminate/queue: 5.6.*|5.7.*
- illuminate/support: 5.6.*|5.7.*
- queue-interop/amqp-interop: ^0.7
Requires (Dev)
- illuminate/events: 5.6.*|5.7.*
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-09-29 05:08:40 UTC
README
安装
您可以使用以下命令通过 composer 安装此包:
composer require mostafaabdelbaset/laravel-queue-rabbitmq
该包将自动通过 Laravel 自动发现进行注册。
在 config/queue.php
中设置连接
'connections' => [ // ... 'rabbitmq' => [ 'driver' => 'rabbitmq', 'dsn' => env('RABBITMQ_DSN', null), /* * Could be one a class that implements \Interop\Amqp\AmqpConnectionFactory for example: * - \EnqueueAmqpExt\AmqpConnectionFactory if you install enqueue/amqp-ext * - \EnqueueAmqpLib\AmqpConnectionFactory if you install enqueue/amqp-lib * - \EnqueueAmqpBunny\AmqpConnectionFactory if you install enqueue/amqp-bunny */ 'factory_class' => Enqueue\AmqpLib\AmqpConnectionFactory::class, 'host' => env('RABBITMQ_HOST', '127.0.0.1'), 'port' => env('RABBITMQ_PORT', 5672), 'vhost' => env('RABBITMQ_VHOST', '/'), 'login' => env('RABBITMQ_LOGIN', 'guest'), 'password' => env('RABBITMQ_PASSWORD', 'guest'), 'queue' => env('RABBITMQ_QUEUE', 'default'), 'options' => [ 'exchange' => [ 'name' => env('RABBITMQ_EXCHANGE_NAME'), /* * Determine if exchange should be created if it does not exist. */ 'declare' => env('RABBITMQ_EXCHANGE_DECLARE', true), /* * Read more about possible values at https://rabbitmq.cn/tutorials/amqp-concepts.html */ 'type' => env('RABBITMQ_EXCHANGE_TYPE', \Interop\Amqp\AmqpTopic::TYPE_DIRECT), 'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false), 'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true), 'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false), 'arguments' => env('RABBITMQ_EXCHANGE_ARGUMENTS'), ], 'queue' => [ /* * Determine if queue should be created if it does not exist. */ 'declare' => env('RABBITMQ_QUEUE_DECLARE', true), /* * Determine if queue should be binded to the exchange created. */ 'bind' => env('RABBITMQ_QUEUE_DECLARE_BIND', true), /* * Read more about possible values at https://rabbitmq.cn/tutorials/amqp-concepts.html */ 'passive' => env('RABBITMQ_QUEUE_PASSIVE', false), 'durable' => env('RABBITMQ_QUEUE_DURABLE', true), 'exclusive' => env('RABBITMQ_QUEUE_EXCLUSIVE', false), 'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false), 'arguments' => env('RABBITMQ_QUEUE_ARGUMENTS'), ], ], /* * Determine the number of seconds to sleep if there's an error communicating with rabbitmq * If set to false, it'll throw an exception rather than doing the sleep for X seconds. */ 'sleep_on_error' => env('RABBITMQ_ERROR_SLEEP', 5), /* * Optional SSL params if an SSL connection is used * Using an SSL connection will also require to configure your RabbitMQ to enable SSL. More details can be founds here: https://rabbitmq.cn/ssl.html */ 'ssl_params' => [ 'ssl_on' => env('RABBITMQ_SSL', false), '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), ], ], // ... ],
用法
一旦您完成配置,您就可以使用 Laravel Queue API。如果您使用过其他队列驱动器,则不需要更改其他任何内容。如果您不知道如何使用 Queue API,请参阅官方 Laravel 文档:[https://laravel.net.cn/docs/queues](https://laravel.net.cn/docs/queues)
使用其他 AMQP 转发器
该包使用基于 php-amqplib 的 enqueue/amqp-lib 转发器。您可以使用任何兼容 amqp interop 的转发器,例如 enqueue/amqp-ext
或 enqueue/amqp-bunny
。以下是如何将转发器更改为 enqueue/amqp-bunny
的示例。
首先,安装所需的转发器包
composer require enqueue/amqp-bunny:^0.8
更改 config/queue.php
中的工厂类
// ... 'connections' => [ 'rabbitmq' => [ 'driver' => 'rabbitmq', 'factory_class' => Enqueue\AmqpBunny\AmqpConnectionFactory::class, ], ],
测试
您可以使用以下命令运行测试:
vendor/bin/phpunit
贡献
您可以通过发现错误和打开问题来为此包做出贡献。请添加您创建的 pull request 或问题的包版本。例如:[5.2] 延迟作业上的致命错误)