renatomaldonado/laravel-sqs-consume

v1.0.1 2023-05-29 20:45 UTC

This package is auto-updated.

Last update: 2024-08-30 01:20:51 UTC


README

这是一个用于Laravel(或Lumen)的定制SQS连接器,它支持基于https://github.com/dusterio/laravel-plain-sqs库的多个作业和任何消息类型。

但每个作业将负责一个队列的SQS。如果某个队列中存在消息,则所有作业都将执行。

依赖项

  • PHP >= 7.1.3
  • Laravel(或Lumen)>= 7.0

通过Composer安装

要安装,只需运行

composer require renatomaldonado/laravel-sqs-consume

Laravel 7中的用法

// Add in your config/app.php

'providers' => [
    '...',
    'Renatomaldonado\LaravelSqsConsume\Provider\LaravelSQSQueueServiceProvider',
];

Lumen 7中的用法

// Add in your bootstrap/app.php
$app->register(Renatomaldonado\LaravelSqsConsume\Provider\LumenSQSQueueServiceProvider::class);

配置

// Generate standard config file (Laravel only)
php artisan vendor:publish --provider="Renatomaldonado\LaravelSqsConsume\Provider\LaravelSQSQueueServiceProvider" 

// In Lumen, create it manually (see example below) and register it in bootstrap/app.php
$app->configure('sqs-consumer');

编辑config/sqs-consumer.php以适应您的需求。此配置将SQS队列与处理类匹配。

return [

    /**
     * Queues array for execute in each job
     */
    'queues' => ['queue-sqs-name-example', 'queue-sqs-name-example-two'],

    'handlers' => [
        'queue-sqs-name-example' => 'your-job',
        'queue-sqs-name-example-two' => 'your-job-two',
    ],

    'default-handler' => 'job-default'
];

如果'handlers'数组中找不到队列,则将SQS有效负载传递给默认处理程序。

将sqs-consumer连接添加到您的config/queue.php中,例如

        ...
        'sqs-consumer' => [
            'driver' => 'sqs-consumer',
            'key'    => env('AWS_KEY', ''),
            'secret' => env('AWS_SECRET', ''),
            'prefix' => 'https://sqs.ea-northheast-1.amazonaws.com/123456/',
            'queue'  => 'queue-sqs-name-example',
            'region' => 'ea-northheast-1',
        ],
        ...

在您的.env文件中,选择sqs-consumer作为您的新默认队列连接

QUEUE_CONNECTION=sqs-consumer

从SQS接收

如果第三方应用程序正在创建自定义格式的JSON消息或其他消息,只需在配置文件中添加一个处理程序,并按照以下方式实现处理程序类

use Illuminate\Contracts\Queue\Job as LaravelJob;

class ExempleJob extends Job
{
    protected $data;

    /**
     * @param LaravelJob $job
     * @param $data
     */
    public function handle(LaravelJob $job, $data)
    {
        // This is incoming JSON payload, already decoded to an array
        var_dump($data);

        // Raw JSON payload from SQS, if necessary
        var_dump($job->getRawBody());
    }
}