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.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题