定制 kfzteile24/queue-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kfzteile24/queue-bundle

Composer 安装命令:

composer require kfzteile24/queue-bundle

包简介

Provides an abstraction on top of AWS' PHP SDK Sqs- and SnsClient to make it DI friendly.

README 文档

README

This Bundle has been created by refactoring the petsdeli/queue-bundle

Installation

  1. Run the following command to install the bundle
composer require kfzteile24/queue-bundle
  1. Configure the clients you want to use in your application
# app/config/config.yml

kfz24_queue:
    clients:
        notify:
            type:                        "sns"
            region:                      "eu-central-1"
            resource:                    "arn:aws:sns:eu-central-1:123456789012:topic"
            access_key:                  "AKIAABCDEFGHIJKLMNOP"
            secret_access_key:           "s3CR3t4Cc3S5K3y"
        one_consumer:
            type:                        "sqs"
            region:                      "eu-central-1"
            resource:                    "https://sqs.eu-central-1.amazonaws.com/123456789012/one-queue"
            access_key:                  "AKIAABCDEFGHIJKLMNOP"
            secret_access_key:           "s3CR3t4Cc3S5K3y"
        another_consumer:
            type:                        "sqs"
            region:                      "eu-central-1"
            resource:                    "https://sqs.eu-central-1.amazonaws.com/123456789012/another-queue"
            access_key:                  "AKIAABCDEFGHIJKLMNOP"
            secret_access_key:           "s3CR3t4Cc3S5K3y"
            large_payload_client:
                region:                  "eu-central-1"
                endpoint:                "http://s3-eu-central-1.amazonaws.com/consumer_bucket"
                bucket:                  "consumer_bucket"
                access_key:              "AKIAABCDEFGHIJKLMNOP"
                secret_access_key:       "s3CR3t4Cc3S5K3y"
                use_path_style_endpoint: "true" | "false"

Usage

Get your configured services from the container

/** @var \Kfz24\QueueBundle\Client\Aws\SnsClient $client */
$client = $container->get('kfz24.queue.client.notify');

$client->send([
    'type' => 'notification', 
    'data' => [1, 2, 3]
]);

or inject them in your services as you see fit.

    <service id="app.consumer_command" class="AppBundle\Command\Consumer">
        <argument type="service" id="kfz24.queue.client.one_consumer" />
        
        <tag name="console.command" />
    </service>

What's this about?

The QueueBundle provides an abstraction on top of AWS' PHP SDK SqsClient and SnsClient and makes them DI friendly. The purpose of the abstraction is to allow for more flexibility than when using the original client implementations directly.

This is particularly the case when you decide to change your queue setup from e.g. a point-to-point queue between 1 producer and 1 consumer to a point-to-multipoint queue setup because you might need a 2nd, 3rd, … consumer processing the same messages. This can easily been achieved by creating an SNS topic and subscribe as many queues as you like to it, instead of sending messages directly to a queue.

Using the native clients has drawbacks in situations like that. They don't share a common interface and expose their respective API methods as class methods. For instance the calls to send a message to either of them look different:

$snsClient = new \Aws\Sns\SnsClient([…]);
$sqsClient = new \Aws\Sqs\SqsClient([…]);

$result = $snsClient->publish([
    'Message' => 'My Message'
]);

$result = $sqsClient->sendMessage([
    'MessageBody' => 'My Message'
]);

This alone turns an architectural decision of having one or more consumers into a refactoring nightmare. But you would also need awareness of the architecture of your queues on the consumer side. As a matter of facts, the same message looks quite differently depending on whether it was posted directly to the SQS queue or was forwarded there through an SNS topic. Let's assume a message

{"foo": "bar"}

When sent directly to a queue the MessageBody looks as you would expect it:

{"foo": "bar"}

But if the message was queued through an SNS topic an SNS envelop is added turning the MessageBody into:

{
    "Type" : "Notification",
    "MessageId" : "4743aa35-e3cd-4562-bd9e-b25778778206",
    "TopicArn" : "arn:aws:sns:eu-central-1:123456789012:sns-test",
    "Message" : "{\"foo\": \"bar\"}",
    "Timestamp" : "2017-05-23T09:58:23.595Z",
    "SignatureVersion" : "1",
    "Signature" : "MDEyMzQ1Njc4OTAwMTIzNDU2Nzg5MDAxMjM0NTY3ODkwMDEyMzQ1Njc4OTAwMTIzNDU2N …",
    "SigningCertURL" : "https://sns.eu-central-1.amazonaws.com/SimpleNotificationService …",
    "UnsubscribeURL" : "https://sns.eu-central-1.amazonaws.com/?Action=Unsubscribe&Subsc …",
    "MessageAttributes" : {
        "AWS.SNS.MOBILE.MPNS.Type" : {"Type":"String","Value":"token"},
        "AWS.SNS.MOBILE.MPNS.NotificationClass" : {"Type":"String","Value":"realtime"},
        "AWS.SNS.MOBILE.WNS.Type" : {"Type":"String","Value":"wns/badge"}
    }
}

Therefore this bundle's SqsClient implementation detects whether the MessageBody contains an SNS envelop and if so first validates its integrity and then automatically unwraps its content. This is completely transparent to you.

kfzteile24/queue-bundle 适用场景与选型建议

kfzteile24/queue-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.9k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 kfzteile24/queue-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 27.9k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 26
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-07