承接 ssigwart/aws-high-availability-sqs 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ssigwart/aws-high-availability-sqs

Composer 安装命令:

composer require ssigwart/aws-high-availability-sqs

包简介

Library to make high availability SQS calls.

README 文档

README

This library makes it easy to write SQS messages with high availability. It includes the following features:

  • Send any size message to SQS. If a message is too large, it will be stored in S3 and a reference to it will be stored in SQS.
  • Send a message to one of multiple backup queues if the primary queue is unavailable.

Setup

You should set up at least 2 SQS queues and 2 S3 buckets in separate regions. The SQS queues are the messages you want to process. The S3 buckets will be used for oversized queue messages that are sent.

S3 Lifecycle Rules

When deleting SQS messages that were backed by an S3 file, the code will attempt to delete the S3 file as well. However, in some cases, that S3 files may not be deleted. For example, it it advised to catch and ignore AwsHighAvailabilitySqsDeleteS3FileDeletionFailureException exceptions when calling AwsHighAvailabilitySqsReceiver::deleteMessage. Also, any messages that expire from the queue or end up going to the dead letter queue will note have the file deleted. Therefore, it is advised to set up lifecycle rules on the S3 bucket. You can find instructions on setting up object expiration at https://docs.aws.amazon.com/AmazonS3/latest/userguide/lifecycle-expire-general-considerations.html. 30 days is a safe bet.

Usage

The APIs require you to pass in an \Aws\Sdk object. In the examples below, $awsSdk is used for this object.

Sending an SQS Message

  1. Create a list of available SQS queues to send to.
    • This is an SqsAvailableQueues object, which includes a primary queue and optional alternative queues.
  2. Create a list of S3 buckets to back large messages.
    • This is and S3AvailableUploadFileBucketAndKeyLocations object.
    • You can add anything that implements S3FileBucketAndKeyProviderInterface to the list of locations.
    • The simplest option is to use S3FileBucketAndKey, which implements this interface.
  3. Set up SqsMessageSendingMetadata with metadata for the message to be sent.
  4. Create an AwsHighAvailabilitySqsSender object and call sendMessageWithS3LargeMessageBacking.
    • You can use the returned object to get the queue used and the message ID
// Set up SQS queues
$primaryQueue = new SqsAvailableQueue('us-east-1', 'https://sqs.us-east-1.amazonaws.com/123456789012/queue_in_us_east_1');
$backupQueue = new SqsAvailableQueue('us-east-2', 'https://sqs.us-east-2.amazonaws.com/123456789012/queue_in_us_east_2');
$availableQueues = new SqsAvailableQueues($primaryQueue);
$availableQueues->addAvailableQueue($backupQueue);

// Set up S3 locations
$primaryLocation = new S3FileBucketAndKey('us-east-1', 'phpunit-test-us-east-1', 'us-east-1/path/to/dir/');
$backupLocation = new S3FileBucketAndKey('us-east-2', 'phpunit-test-us-east-2', 'us-east-2/path/to/dir/');
$s3Locations = new S3AvailableUploadFileBucketAndKeyLocations($primaryLocation);
$s3Locations->addAlternativeLocation($backupLocation);

// Send messages
$sqsSender = new AwsHighAvailabilitySqsSender($awsSdk);
$result = $sqsSender->sendMessageWithS3LargeMessageBacking($availableQueues, $s3Locations, $queueMsgBody, null);
print 'Selected Queue: ' . $result->getSelectedQueue()->getQueueUrl() . PHP_EOL;
print 'Message ID: ' . $result->getSqsMessageId() . PHP_EOL;

SqsMessageSendingMetadata Options

The SqsMessageSendingMetadata class allows you to customize the following when sending messages:

  • SQS delivery delay.
  • SQS message attributes.

Receiving SQS Messages

  1. Create an SqsAvailableQueue object with the queue you want to receive from.
  2. Set up SqsMessageReceivingMetadata with metadata for receiving messages.
  3. Create an AwsHighAvailabilitySqsReceiver object and call receivedMessagesWithS3LargeMessageBacking.
    • The returned object includes the SQS messages, which you can get using getSqsMessages.
$primaryQueue = new SqsAvailableQueue('us-east-1', 'https://sqs.us-east-1.amazonaws.com/123456789012/queue_in_us_east_1');

// Set up metadata
$metadata = new SqsMessageReceivingMetadata();
$metadata->setMaxNumMessages(7);
$metadata->setVisibilityTimeout(300);
$metadata->setWaitTime(15);

$sqsReceiver = new AwsHighAvailabilitySqsReceiver($awsSdk);
$sqsMessagesResult = $sqsReceiver->receivedMessagesWithS3LargeMessageBacking($primaryQueue, $metadata);
print 'Number of Messages: ' . $sqsMessagesResult->getNumMessages() . PHP_EOL;
foreach ($sqsMessagesResult->getSqsMessages() as $sqsMessage)
{
	print 'Messages ID: ' . $sqsMessage->getMessageId() . PHP_EOL;
	print 'Receipt Handle: ' . $sqsMessage->getReceiptHandle() . PHP_EOL;
	print 'Message Body: ' . $sqsMessage->getMessage() . PHP_EOL;
}

SqsMessageReceivingMetadata Options

The SqsMessageReceivingMetadata class allows you to customize the following when receiving messages:

  • Maximum number of messages to received (default: 10)
  • SQS visibility timeout for messages received.
  • SQS wait time when receiving messages.

Deleting SQS Messages

  1. Create an SqsAvailableQueue object with the queue you want to delete from.
  2. Create an AwsHighAvailabilitySqsReceiver object and call deleteMessage.
    • For any failures, a AwsHighAvailabilitySqsDeleteException will be thrown.
    • However, if a AwsHighAvailabilitySqsDeleteS3FileDeletionFailureException exception is thrown, the SQS message will have been successfully deleted, so it's advisable to ignore the exception besides possibly logging it.
$primaryQueue = new SqsAvailableQueue('us-east-1', 'https://sqs.us-east-1.amazonaws.com/123456789012/queue_in_us_east_1');
$sqsReceiver = new AwsHighAvailabilitySqsReceiver($awsSdk);
try {
	$sqsReceiver->deleteMessage($primaryQueue, $sqsMessage);
} catch (AwsHighAvailabilitySqsDeleteS3FileDeletionFailureException $e) {
	error_log($e->getMessage());
} catch (AwsHighAvailabilitySqsDeleteException $e) {
	// Handle error. Possibly try to delete it again or add application code to prevent the message from being processed again later.
}

ssigwart/aws-high-availability-sqs 适用场景与选型建议

ssigwart/aws-high-availability-sqs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「aws」 「sqs」 「HA」 「high-availability」 「multi-region」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ssigwart/aws-high-availability-sqs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ssigwart/aws-high-availability-sqs 我们能提供哪些服务?
定制开发 / 二次开发

基于 ssigwart/aws-high-availability-sqs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-26