Laravel 和多个 SQS(队列)配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24475058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:43:15  来源:igfitidea点击:

Laravel and Multiple SQS (queue) configs

laravellaravel-4amazon-sqs

提问by egekhter

Is there a method on the Queue class that can specify a specific connection as defined in the queue config? There's a similar option for MySql, where you can define 'mysql2', and then call:

Queue 类上是否有可以指定队列配置中定义的特定连接的方法?MySql 有一个类似的选项,你可以在其中定义 'mysql2',然后调用:

DB::connection('mysql2')->table('etc')->get();

Is there a similar option for queues?

队列有类似的选项吗?

Something like:

就像是:

Queue::connection('sqs2')->push('MyQueue', array('message' => $message));

回答by egekhter

Apparently I answered my own question above without even realizing it. You can have multiple queues and specify which one you want to push the message to by using a connection method.

显然,我在上面回答了我自己的问题,甚至没有意识到。您可以有多个队列,并使用连接方法指定要将消息推送到哪个队列。

Here's what my partial config looks like for anybody that's interested:

对于任何感兴趣的人来说,这是我的部分配置的样子:

    'default' => 'sqs',

   'connections' => array(

    'sync' => array(
        'driver' => 'sync',
    ),

    'beanstalkd' => array(
        'driver' => 'beanstalkd',
        'host'   => 'localhost',
        'queue'  => 'default',
    ),

    'sqs' => array(
        'driver' => 'sqs',
        'key'    => 'xxxxxxxxxxxx',
        'secret' => 'yyyyyyyyyyyyyy',
        'queue'  => 'https://sqs.us-west-2.amazonaws.com/zzzzzzzzz',
        'region' => 'us-west-2',
    ),

    'sqs2' => array(
        'driver' => 'sqs',
        'key'    => 'uuuuuuuuuuuuu',
        'secret' => 'vvvvvvvvvvvvvvvv',
        'queue'  => 'https://sqs.us-west-2.amazonaws.com/wwwwwwwwwww',
        'region' => 'us-west-2',
    ),

回答by Lu32

You only configure one connection to SQS, then unlike other drivers like redis or database, if you want to send a job in a different queue name, that queue should exist previously, create it in the amazon console or with an SDK/api, then pass it when dispatching the job like this:

您只配置一个到 SQS 的连接,然后与 redis 或数据库等其他驱动程序不同,如果您想以不同的队列名称发送作业,则该队列之前应该存在,在亚马逊控制台中或使用 SDK/api 创建它,然后在像这样调度作业时传递它:

dispatch(new MyJob)->onConnection('sqs')->onQueue('https://sqs.us-east-1.amazonaws.com/{account_id}/{queue_name}');

In this way you only configure one connection, and have multiple queues in that connection

这样你只配置了一个连接,并且在那个连接中有多个队列

For last for proccesing the Job like in egekhter response,

对于像 egekhter 响应中那样处理 Job 的最后一步,

php artisan queue:work sqs --queue='https://sqs.us-east-1.amazonaws.com/{account_id}/{queue_name}'

you also can configure multiple connections but it seems unnecessary since all of them are in the same account and credentials, and it seems the perfect use case for multiple queues in the same conexion.

您还可以配置多个连接,但似乎没有必要,因为它们都在同一个帐户和凭据中,而且这似乎是同一 conexion 中多个队列的完美用例。