在 laravel 队列中使用多个连接

Use multiple connection in laravel queue

使用laravel 5.5,我们需要同时使用Redis和SQS队列。 Redis 用于我们的内部消息传递,SQS 用于来自第 3 方的消息。

config/queue.php 有各种连接信息。第一个键是默认连接。该默认值是 queue:work artisan 命令使用的默认值。

'default' => 'redis',

'connections' => [
    'sqs' => [            
        'driver' => 'sqs',
        'key'    => env('ACCESS_KEY_ID', ''),
        'secret' => env('SECRET_ACCESS_KEY', ''),
        'prefix' => 'https://sqs.us-west-1.amazonaws.com/account-id/',
        'queue'  => 'my-sqs-que'),
        'region' => 'us-west-1',
    ],


    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => env('REDIS_QUE' , 'default'),
        'retry_after' => 90,
    ],

问题是我们如何为 queue:work 使用不同的队列连接。

如果提供--queue=my-sqs-que,默认连接设置为redis,laravel在redis下查找,显然没有找到my-sqs-que

将默认设置为 sqs 将禁止处理我们的内部消息。

可以在运行queue:work时指定连接,见Specifying the Connection and Queue:

You may also specify which queue connection the worker should utilize. The connection name passed to the work command should correspond to one of the connections defined in your config/queue.php configuration file:

php artisan queue:work redis

您还需要为每个队列设置相应的连接。

However, any given queue connection may have multiple "queues" which may be thought of as different stacks or piles of queued jobs.