ratheeps/laravel-pub-sub-messaging 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ratheeps/laravel-pub-sub-messaging

Composer 安装命令:

composer require ratheeps/laravel-pub-sub-messaging

包简介

A simple Laravel service provider which adds a new queue connector to handle SNS subscription queues.

README 文档

README

Simple extension to the Illuminate/Queue queue system used in Laravel and Lumen.

Using this connector allows SQS messages originating from a SNS subscription to be worked on with Illuminate\Queue\Jobs\SqsJob.

This is especially useful in a miroservice architecture where multiple services subscribe to a common topic with their queues and publish an event to SNS.

Amazon SQS & SNS Extended Client Library

The Amazon SQS Extended Client Library for Laravel enables you to manage Amazon SQS message payloads with Amazon S3. This is especially useful for storing and retrieving messages with a message payload size greater than the current SQS limit of 256 KB, up to a maximum of 2 GB. Specifically, you can use this library to:

  • Specify whether message payloads are always stored in Amazon S3 or only when a message's size exceeds a max size (defaults to 256 KB).
  • Send a message that references a single message object stored in an Amazon S3 bucket.
  • Get the corresponding message object from an Amazon S3 bucket.
- Note: This package under development not ready for production -

Requirements

  • Laravel (tested with version >=7.0)

Installation

  1. First create a disk that will hold all of your large SQS payloads.

We highly recommend you use a private bucket when storing SQS payloads. Payloads can contain sensitive information and should never be shared publicly.

  1. Run composer require ratheeps/laravel-pub-sub-messaging to install the package.

  2. Then, add the following queue settings to your queue.php file.

<?php
return [
    'connections' => [
        'pub-sub-messaging-sqs' => [
            'driver' => 'pub-sub-messaging-sqs',
            'key' => env('PUB_SUB_MESSAGING_AWS_ACCESS_ID'),
            'secret' => env('PUB_SUB_MESSAGING_AWS_SECRET_ACCESS_KEY'),
            'prefix' => env('PUB_SUB_MESSAGING_SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
            'queue' => env('PUB_SUB_MESSAGING_SQS_QUEUE', 'default'),
            'suffix' => env('PUB_SUB_MESSAGING_SQS_SUFFIX'),
            'region' => env('PUB_SUB_MESSAGING_AWS_DEFAULT_REGION', 'us-east-1'),
            'after_commit' => false,
            'disk_options' => [
                'always_store' => true,
                'cleanup' => false,
                'disk' => env('PUB_SUB_MESSAGING_DISK', 'pub_sub_messaging_s3'),
                'prefix' => '',
            ],
        ],
    ],
];

4 Then, add the following disk settings to your filesystems.php file.

<?php
return [
    'disks' => [
        'pub_sub_messaging_s3' => [
            'driver' => 's3',
            'key' => env('PUB_SUB_MESSAGING_AWS_ACCESS_ID'),
            'secret' => env('PUB_SUB_MESSAGING_AWS_SECRET_ACCESS_KEY'),
            'region' => env('PUB_SUB_MESSAGING_AWS_DEFAULT_REGION'),
            'bucket' => env('PUB_SUB_MESSAGING_AWS_BUCKET'),
            'url' => env('PUB_SUB_MESSAGING_AWS_URL'),
            'endpoint' => env('PUB_SUB_MESSAGING_AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        ],
    ],
];
  1. You can optionally publish the config file with php artisan vendor:publish --provider="Ratheeps\PubSubMessaging\PubSubMessagingServiceProvider" --tag="config" this command Then, modify the following pub sub settings to your pub-sub-messaging.php file.
<?php
return [
    'default_topic' => env('PUB_SUB_MESSAGING_DEFAULT_TOPIC'),
    'default_auth_driver' => null,
    // map the jobs to subscribe SNS topics to handle the consuming events
    'map' => [
//        \App\Jobs\TestSQSJob::class => 'arn:aws:sns:ap-southeast-1:931616835216:modelEvent',
    ],
    'published_attributes' => [
        'id',
        'created_at',
        'updated_at'
    ],
    'sns' => [
        'key' => env('PUB_SUB_MESSAGING_AWS_ACCESS_KEY'),
        'secret' => env('PUB_SUB_MESSAGING_AWS_SECRET_ACCESS_KEY'),
        'region' => env('PUB_SUB_MESSAGING_AWS_DEFAULT_REGION', 'us-east-1'),
        'disk_options' => [
            // Indicates when to send messages to S3., Allowed values are: ALWAYS, NEVER, IF_NEEDED.
            'store_payload' => 'IF_NEEDED',
            'disk' => env('PUB_SUB_MESSAGING_DISK', 'pub_sub_messaging_s3'),
            'prefix' => ''
        ]
    ]
];
  1. You'll need to configure .env file
PUB_SUB_MESSAGING_AWS_ACCESS_ID=
PUB_SUB_MESSAGING_AWS_SECRET_ACCESS_KEY=
PUB_SUB_MESSAGING_AWS_DEFAULT_REGION=ap-south-1

PUB_SUB_MESSAGING_DEFAULT_TOPIC=
PUB_SUB_MESSAGING_AWS_BUCKET=
PUB_SUB_MESSAGING_DISK=pub_sub_messaging_s3

QUEUE_CONNECTION=pub-sub-messaging-sqs
PUB_SUB_MESSAGING_SQS_QUEUE=
  1. Boot up your queues and profit without having to worry about SQS's 256KB limit :)

Consuming Event

Create a job like below to handle the event and then map that job to SNS topic in the pub-sub-messaging.php config file

<?php
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class TestSQSJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $passedInData;

    /**
     * Create a new job instance.
     *
     * @param array $data
     */
    public function __construct(array $data)
    {
        // $data is array containing the msg content from SQS
        $this->passedInData = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info(json_encode($this->passedInData));
        // Check laravel.log, it should now contain msg string.
    }
}

Published event

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Ratheeps\PubSubMessaging\Traits\SNSPublisher;

class Post extends Model
{
    use SNSPublisher;

    /**
     * @var array
     * Optional (default value is [] )
     * Witch are the attributes should only from SNS message
     */
    static $publishedAttributes = [
        'id',
        'created_at',
        'updated_at'
    ];

    /**
     * @var array
     * Optional  (default value is [created','updated','deleted','restored'] )
     * Witch events should send to SNS
     */
    static $publishEvents = ['created', 'updated'];

    /**
     * @var string
     * Optional (default value is load from config )
     * Publish SNS topic
     */
    static $publishTopic = 'SampleTopic';
    /**
     * Or
     * static $publishTopic = [
     *  'created' => 'SampleTopic'
     * ];
     */
}

Diagram

This diagram will be describing how your microservices are communicating with help of this package Diagrams

References

Feedback

  • Give us feedback here.
  • If you'd like to contribute a new feature or bug fix, we'd love to see Github pull requests from you.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固