joblocal / laravel-sqs-sns-subscription-queue
一个简单的 Laravel 服务提供者,它添加了一个新的队列连接器来处理 SNS 订阅队列。
v2.6.5
2023-09-28 07:27 UTC
Requires
- php: >=7.1.3
- aws/aws-sdk-php: ^3.62
- illuminate/queue: ^5.6|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^5.6|^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ~3.8|^4.0|^5.0|^6.0|^7.0|^8.0
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3
- dev-master
- v2.6.5
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- 2.6.0
- v2.5.0
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3
- v2.2
- v2.1
- v2.0
- v1.0
- dev-bugfix/handle-topic-arn-and-subject-when-checking-routes
- dev-feature/delete-unwanted-messages-from-queue
- dev-bugfix/fix-spelling-mistake
- dev-feature/ignore-unlisted-messages-from-topic
- dev-phpunit-order-by-random
- dev-update-readme
- dev-feature/refactor-command-building
- dev-feature-variable-route-key
This package is auto-updated.
Last update: 2024-08-28 09:35:33 UTC
README
这是 Laravel 和 Lumen 中使用的 Illuminate/Queue 队列系统的一个简单扩展。
使用此连接器可以允许使用 SQS 消息,这些消息来自 SNS 订阅,并可以使用 Illuminate\Queue\Jobs\SqsJob 处理。
这在微服务架构中特别有用,其中多个服务通过其队列订阅一个共同的主题。
请注意,此包不会处理向 SNS 发布,请使用 AWS SDK 发布事件到 SNS。
需求
- Laravel(已测试 5.8 版本)
- 或 Lumen(已测试 5.8 版本)
用法
将 LaravelSqsSnsSubscriptionQueue 服务提供者添加到您的应用程序中。
Laravel
'providers' => [ // ... Joblocal\LaravelSqsSnsSubscriptionQueue\SqsSnsServiceProvider::class, ],
Lumen
$app->register(Joblocal\LaravelSqsSnsSubscriptionQueue\SqsSnsServiceProvider::class);
配置
您需要在 config/queue.php 中配置队列连接。
'connections' => [ 'sqs-sns' => [ 'driver' => 'sqs-sns', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'queue' => env('SQS_QUEUE', 'your-queue-url'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'routes' => [ // you can use the "Subject" field 'Subject' => 'App\\Jobs\\YourJob', // or the "TopicArn" of your SQS message 'TopicArn:123' => 'App\\Jobs\\YourJob', // to specify which job class should handle the job ], ], ],
一旦配置了 sqs-sns 队列连接器,您可以通过在 .env 文件中将队列驱动设置为 'sqs-sns' 来开始使用它。
作业类示例
use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; /** * Example Job class */ class Job implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; /** * @param string $subject SNS Subject * @param array $payload JSON decoded 'Message' */ public function __construct(string $subject, array $payload) { } }
消息转换
当 SNS 向 SQS 队列发布时,接收到的消息签名如下:
{ "Type" : "Notification", "MessageId" : "63a3f6b6-d533-4a47-aef9-fcf5cf758c76", "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic", "Subject" : "Testing publish to subscribed queues", "Message" : "Hello world!", "Timestamp" : "2017-03-29T05:12:16.901Z", "SignatureVersion" : "1", "Signature" : "...", "SigningCertURL" : "...", "UnsubscribeURL" : "..." }
Illuminate\Queue\Jobs\SqsJob 需要以下签名:
{ "job": "Illuminate\\Queue\\CallQueuedHandler@call", "data": { "commandName": "App\\Jobs\\YourJob", "command": "...", } }
安装
通过使用 Composer 安装 laravel-sqs-sns-subscription 是最好的方式。
要安装最新版本:
php composer.phar require joblocal/laravel-sqs-sns-subscription-queue