首页 > 解决方案 > 如何在 Lumen 中使用多个 SQS(队列服务)实例?

问题描述

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

标签: phplaravellumen

解决方案


据我所知,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
       }
   }

如果您需要使用多个连接,则无法使用单个工作人员来执行此操作,但是您可以一次使用多个工作人员。首先配置您的连接,例如在您的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',
    ],
]

如果您使用的是主管,请设置您的主管配置,否则您必须手动启动两个工作人员。这是您可以使用的主管配置:

[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

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


推荐阅读