如何在 Lumen 中使用多个 SQS(队列服务)实例?

How to use multiple SQS(queue service) instance in Lumen?

我想将消息并行或一个接一个地推送到多个 SQS 队列,但它应该是动态的,当我启动 worker 时,它应该从两个队列中获取消息并进行区分。
我怎样才能在 Lumen 中实现这一点?
更新
如何为具有不同亚马逊 SQS 实例的不同队列使用多个工作人员?

据我所知,Lumen 和 Laravel 使用完全相同的代码来处理队列,所以这里有一些可能有用的东西,尽管我还没有测试过。

运行 队列工作者为:

 php artisan queue:work --queue=queue1,queue2 

这意味着 queue1 中的作业在 queue2 中的作业之前被处理(不幸的是,这是监听多个队列的唯一方法)

那么在你的工作中:

class MyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


   public function handle()
   {
       if ($this->job->getQueue() === 'queue1') {
          //Things
       } else {
          // different things
       }
   }

如果您需要使用多个连接,您不能使用单个 worker 来实现,但是您可以一次使用多个 worker。首先配置您的连接,例如在你的 config/queue.php

'connections' => [
      'sqs' => [
        'driver' => 'sqs',
        'key' => 'your-public-key',
        'secret' => 'your-secret-key',
        'prefix' => 'https://sqs.us-east-1.amazonaws.com/your-account-id',
        'queue' => 'your-queue-name',
        'region' => 'us-east-1',
    ],
    'sqs2' => [
        'driver' => 'sqs',
        'key' => 'your-other-public-key',
        'secret' => 'your-other-secret-key',
        'prefix' => 'https://sqs.us-east-1.amazonaws.com/your-other-account-id',
        'queue' => 'your-other-queue-name',
        'region' => 'us-east-1',
    ],
]

如果您使用的是 supervisor,则设置您的 supervisor 配置,否则您将不得不手动启动两个 worker。这是您可以使用的主管配置:

[program:laravel-sqs-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan queue:work sqs --queue=queue1
autostart=true
autorestart=true
user=www-data 
numprocs=1
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log

[program:laravel-sqs2-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan queue:work sqs2 --queue=queue2
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log

根据您的应用更改路径和用户设置。