tedicela / sqs-simple
PHP 包,用于以简单的方式消费 AWS SQS 队列
0.0.10
2022-05-06 08:14 UTC
Requires
- php: >=5.5.9
- aws/aws-sdk-php: ^3.87
README
PHP 包,用于以简单的方式消费 AWS SQS 队列
描述
受 Rabbitmq PHP SDK 启发,我创建了此 PHP 库,它使 AWS 简单队列服务(SQS)的使用变得简单。只需几行代码,你就可以创建一个监听队列消息的工作者(长期运行的工作者)。你可以设置参数来调整成本(对 SQS 的请求)。同样,将消息发布到队列也很简单。
要求
- php >=5.5.9
通过 Composer 安装
你可以使用 Composer 将此库添加到你的项目中。如果你还没有安装 Composer 并且想要安装它,请在此处下载 Composer 并按照安装指南进行操作。
要将 sqs-simple 添加到你的项目中,只需在命令行中执行以下操作
composer require tedicela/sqs-simple
使用案例
如何将消息发布到 SQS 队列
AWS SQS 会对你在该服务上进行的每个请求收费。因此,你可以调整 SqsMessenger 属性以获得最可靠的服务和更低的成本。
示例(请查看注释以获取解释)
<?php require 'vendor/autoload.php'; use SqsSimple\SqsMessenger; $AwsConfig = [ 'AWS_KEY'=>'', //You should put your AWS_KEY 'AWS_SECRET_KEY'=>'', //You should put your AWS_SECRET_KEY 'AWS_REGION'=>'eu-west-1', //You should put your AWS_REGION 'API_VERSION'=>'2012-11-05' ]; $messenger = new SqsMessenger($AwsConfig); /* if a publish message request fails then it will retry again */ // $messenger->RetryTimesOnFail = 2; /* seconds to wait after failed request to retry again */ // $messenger->WaitBeforeRetry = 1; //seconds $queue = "<Your queueUrl>"; $message = "This is a message for SQS"; $messageAttributes = []; $delaySeconds = 10; // (Not FIFO Queue type) - The time in seconds that the delivery of all messages in the queue will be delayed. An integer from 0 to 900 (15 minutes). The default for this attribute is 0 (zero). $messageGroupId = ''; // (FIFO Queue type) - The tag that specifies that a message belongs to a specific message group. Messages that belong to the same message group are always processed one by one, in a strict order relative to the message group (however, messages that belong to different message groups might be processed out of order). $messageDeduplicationId = ''; // (FIFO Queue type) - The token used for deduplication of sent messages. If a message with a particular message deduplication ID is sent successfully, any messages sent with the same message deduplication ID are accepted successfully but aren't delivered during the 5-minute deduplication interval. The queue should either have ContentBasedDeduplication enabled or MessageDeduplicationId provided explicitly. $messenger->publish( $queue, $message, $messageAttributes, $delaySeconds, $messageGroupId, $messageDeduplicationId);
创建一个工作者以监听 SQS 队列中的消息(长期运行的工作者)
如上所述,AWS SQS 会对你进行的每个请求收费,因此即使在 SqsWorker 中,你也可以调整其属性以获得最佳效果并降低成本。调整 SqsWorker 的属性非常重要,因为工作者是比发布者发出更多请求的过程。
示例(请查看注释以获取解释)
<?php require 'vendor/autoload.php'; use SqsSimple\SqsWorker; /* You can pass the AWS configuration into constructor but this is optional. If you don't pass this configuration you should set an authenticated SqsClient Object like in the comment below */ $AwsConfig = [ 'AWS_KEY'=>'', //You should put your AWS_KEY 'AWS_SECRET_KEY'=>'', //You should put your AWS_SECRET_KEY 'AWS_REGION'=>'eu-west-1', //You should put your AWS_REGION 'API_VERSION'=>'2012-11-05', ]; $worker = new SqsWorker($AwsConfig); /* You can set an already authenticated SqsClient Object if set this you don't need to pass the AwsConfig above */ // $worker->SqsClient = $ExistingSqsClient; /* seconds to wait before checking again if no messages was found */ // $worker->Sleep = 10; /* how many seconds with wait for messages in SQS to be available in one check */ // $worker->WaitTimeSeconds = 20; /* how many messages to get from queue when the worker checks */ // $worker->MaxNumberOfMessages = 1; /* After the worker get a message and starts elaborating it how many seconds the message should not be available to other workers */ // $worker->VisibilityTimeout = 3600; $queueUrl = "<Your queueUrl>"; $worker->listen($queueUrl, function($message){ echo "\n this is the working process\n"; var_dump($message); /* You should return TRUE when you want to remove the message from queue (ACK) or return FALSE when your job failed and you want to return the message back to queue(NACK) to retry later or by another worker */ return true; }, function($exceptionMessage, $errorCounter){ // $errorHandlerCallback - this is optional as parameter /* $exceptionMessage is the $e->getMessage() $errorCounter - how many times in a row errors occured, if it's 5 then listening will stop */ });
如何贡献
如果你有兴趣添加新功能,可以打开一个问题(我会尽快添加),或者分叉此项目并创建一个 Pull request(我会很高兴接受它)。