dongshipang8094 2019-03-04 08:22
浏览 151
已采纳

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

I wanted to push messages to multiple SQS queues parallel or one after another, but it should be dynamic and when I start the worker it should fetch the messages from both the queues and differentiate.
How can I achieve this in Lumen?
UPDATE
How to use multiple worker for different queues with different amazon SQS instances?

  • 写回答

1条回答 默认 最新

  • du9843 2019-03-04 08:31
    关注

    As far as I can see Lumen and Laravel use the exact same code to handle queues so here's something that might work, though I haven't tested it.

    Run the queue worker as:

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

    This will mean that jobs in queue1 are processed before jobs in queue2 (unfortunately this is the only way to listen to multiple queues)

    Then in your job:

    class MyJob implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    
       public function handle()
       {
           if ($this->job->getQueue() === 'queue1') {
              //Things
           } else {
              // different things
           }
       }
    

    If you need to use multiple connections you can't do that using a single worker, however you can use multiple workers at a time. First configure your connections e.g. in your 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',
        ],
    ]
    

    If you're using supervisor then setup your supervisor configuration, if not you'll have to start both workers manually. Here's a supervisor configuration you can use:

    [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
    

    Alter the paths and user settings according to your app.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题