ervinomueller / laravel-sqs-sns-subscription-queue
一个简单的Laravel服务提供者,它添加了一个新的队列连接器来处理SNS订阅队列。
2.4.4
2020-11-06 16:54 UTC
Requires
- php: >=7.1.3
- ext-json: *
- aws/aws-sdk-php: ^3.62
- illuminate/queue: ^5.6|^6.0|^7.0|^8.0
- illuminate/support: ^5.6|^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ~3.8|^4.0|^5.0|^6.0
- phpunit/phpunit: ^8
- squizlabs/php_codesniffer: ^3
This package is auto-updated.
Last update: 2024-09-07 01:23:07 UTC
README
是Laravel和Lumen中使用的Illuminate/Queue队列系统的一个简单扩展。
使用此连接器可以使来自SQS的消息与SNS订阅一起使用Illuminate\Queue\Jobs\SqsJob。
这在微服务架构中尤其有用,其中多个服务订阅一个常见主题并使用自己的队列。
请注意,此包不会处理SNS的发布,请使用AWS SDK将事件发布到SNS。
需求
- Laravel(测试版本5.8)
- 或Lumen(测试版本5.8)
安装
通过使用Composer安装laravel-sqs-sns-subscription是最佳方式。
要安装最新版本
php composer.phar require ervinomueller/laravel-sqs-sns-subscription-queue
用法
将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', 'your-public-key'), 'secret' => env('AWS_SECRET_ACCESS_KEY', 'your-secret-key'), 'queue' => env('QUEUE_URL', '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 ], //optional - you can override default job when no job are found on routes array above 'default-job' => 'App\\Jobs\\YourDefaultJob', ], ],
一旦配置了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": "...",
}
}