承接 nightwriter/laravel-events-to-sns 相关项目开发

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

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

nightwriter/laravel-events-to-sns

最新稳定版本:1.0.2

Composer 安装命令:

composer require nightwriter/laravel-events-to-sns

包简介

Laravel package to push events to AWS SNS

README 文档

README

Total Downloads Latest Stable Version License

Laravel events to SNS

This library allow us to send Laravel events to an SNS topic, and receive them through a SQS queue.

Install

$ composer require nightwriter/laravel-events-to-sns

Add the provider to config/app.php

'providers' => [
    Ralbear\EventsToSns\EventsToSnsProvider::class
 ]

Configuration

First step is create this new connection configuration in config/queue.php

'connections' => [
    'sqs-sns' => [
        'driver' => 'sqs-sns',
        'key' => env('SQS_SNS_ACCESS_KEY_ID') ?? env('AWS_ACCESS_KEY_ID') ?? '',
        'secret' => env('SQS_SNS_SECRET_ACCESS_KEY') ?? env('AWS_SECRET_ACCESS_KEY') ?? '',
        'region' => env('SQS_SNS_DEFAULT_REGION') ?? env('AWS_DEFAULT_REGION') ?? '',
        'base_ARN' => env('SQS_SNS_BASE_ARN') ?? '',
        'valid_topics' => explode(',',env('SQS_SNS_VALID_TOPICS')) ?? [],
        'prefix' => env('SQS_SNS_PREFIX') ?? env('SQS_PREFIX') ?? '',
        'queue' => env('SQS_SNS_QUEUE') ?? env('SQS_QUEUE') ?? '',
        'env_postfix' => env('SQS_SNS_ENV') ?? env('APP_ENV') ?? '',
        'event_class_postfix' => 'Event'
    ],
]

AWS credentials

If we use the same AWS account for SNS than for other AWS services on the application, we can use the default env keys for the credentials.

AWS_ACCESS_KEY_ID=<MAIN ACCESS KEY ID>
AWS_SECRET_ACCESS_KEY=<SECRECT ACCESS KEY>
AWS_DEFAULT_REGION=us-west-1

If we need specific credentials for SNS, use this env keys:

SQS_SNS_ACCESS_KEY_ID=<SNS ACCESS KEY ID>
SQS_SNS_SECRET_ACCESS_KEY=<SNS SECRET ACCESS KEY>
SQS_SNS_DEFAULT_REGION=eu-west-1

Topics

The way this library is designed, define SNS topics based on three parts.

  • A: Use the env variable:
SQS_SNS_BASE_ARN=arn:aws:sns:eu-west-1:123456789
  • B: Defined in your event:
public function getTopic()
{
   return 'service-a-topic';
}

The event level topics we use, should be defined as a comma separated value on this env variable:

SQS_SNS_VALID_TOPICS=service-a-topic,service-b-topic
  • D: Use the env variable if need a different value than APP_ENV:
SQS_SNS_ENV=local

This SQS_SNS_ENV allow us to have custom topics for each environment, if we for example generate new environments for test specific features, we can set here the feature name.

Examples

Event example

<?php

namespace App\Events;

use App\Models\Order;
use Ralbear\EventsToSns\Contracts\ShouldBeInSns;
use Ralbear\EventsToSns\Traits\SendToSns;

class OrderCreatedEvent implements ShouldBeInSns
{
    use SendToSns;

    public $order;

    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    public function uniqueId()
    {
        return $this->order->id;
    }

    public function getTopic()
    {
        return 'service-a-topic';
    }
}

Run worker

Run the worker:

$ php artisan queue:worker sqs-sns

Job example

<?php

namespace App\Jobs;

use Illuminate\Queue\Jobs\SqsJob;

class OrderCreatedJob
{
    public function handle(SqsJob $job, $data)
    {
        //Do something nice with your $data
        
        $job->delete();
    }
}

Test

To run test:

$ composer test

ToDo's

  • Improve tests and coverage

License

Laravel events to SNS is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-21