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

时间:2019-03-04 08:22:38

标签: php laravel lumen

我想将消息并行或一个接一个地推送到多个SQS队列,但是它应该是动态的,当我启动worker时,应该从两个队列中提取消息并进行区分。
如何在流明中实现这一目标?
更新
如何将多个worker用于具有不同Amazon SQS实例的不同队列?

1 个答案:

答案 0 :(得分:3)

据我所知,Lumen和Laravel使用完全相同的代码来处理队列,因此尽管我尚未对其进行测试,但这还是可行的。

以以下方式运行队列工作器:

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

这将意味着在队列2中的作业之前先处理队列1中的作业(不幸的是,这是侦听多个队列的唯一方法)

然后在您的工作中:

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

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