ludovicose/transaction-outbox
Composer 安装命令:
composer require ludovicose/transaction-outbox
包简介
Laravel Pub-Sub package use transactional outbox pattern
README 文档
README
When an event is sent to the broker, the event is stored in the database, after which the event is sent to the broker. If the event was successfully sent to the broker, we mark that the event was successfully sent.
When subscribing an event, we listen to the event from the broker. When receiving an event, we store it in the database. After saving to the database, we send an internal event for processing.
The goal of the library is to decide the consistency of data between services, the order in which the message is sent, idempotency.
Currently, the broker is Redis. In the future we will add other brokers message.
Installation
You can install the package via composer:
composer require ludovicose/transaction-outbox
The package will automatically register itself.
You can publish the migration with:
php artisan vendor:publish --provider="Ludovicose\TransactionOutbox\PackageServiceProvider" --tag="migrations"
After the migration has been published you can create the table by running the migrations:
php artisan migrate
You can publish the config-file with:
php artisan vendor:publish --provider="Ludovicose\TransactionOutbox\PackageServiceProvider" --tag="config"
Usage
Publish Event
Create event file and implements by ShouldBePublish interface. If the ShouldBePublish interface is implemented, then the event automatically goes to the broker Set channel name. Channel name is the name of the event that will be sent to the broker
namespace App\Events; use Ludovicose\TransactionOutbox\Contracts\ShouldBePublish; class PostCreatedEvent implements ShouldBePublish { ... public function getChannel(): string { return 'channelName'; } ... }
To send an event to the broker, you need to run the command
event(new PostCreatedEvent($someData));
Subscribe
Add a channel name to the config file to understand which event to listen for. These channel names can be used as internal events.
return [ ... 'subscribe_channels' => [ 'channelName' ], ... ];
Run command in console t listen event in broker.
$ php artisan events:listen
Add a channel name to the EventServiceProvider file to listen for the event that came from the broker.
namespace App\Providers; ... class EventServiceProvider extends ServiceProvider { protected $listen = [ ... 'channelName' => [ SomeListener::class ] ... ]; }
In handle method you get the data that came from the broker
namespace App\Listeners; class SomeListener { public function handle($event) { // $event data in event } }
Resubmitting events
If the event did not get into the message broker, then you can resend it with the command below, indicating the start date and end date
$ php artisan events:repeat 2022-12-12
Http request
We can also log http requests. When sending a request, we save it with ourselves and upon receiving a response, we note that the request was successfully completed.
To register http requests, you must enable the option in the config file
return [ ... 'enable_request_log' => true ... ];
Resubmitting request
If the http request failed, then it can be resent with the command below, specifying the start date and end date
$ php artisan request:repeat 2022-12-12
Resend erroneous data from the queue
If we get an error while processing data from the queue, then this data goes to the queue (serviceName.errors).
To resend data from the queue (serviceName.errors) we need to run the command:
$ php artisan events:resend-errors
In the configuration file you can specify the name of the queue where the errors will be stored
return [ ... 'error_queue' => 'errors' ... ];
Clear event in DB
If you do not clean the database from the event, then the entries in the database can increase. To clean up old entries, you can run the command:
$ php artisan events:clear
In the config file, you can specify a day after which records will be deleted
return [ ... 'delete_last_event_in_day' => 10 ... ];
QUEUE
Add queue.php config rabbitmq
return [ ... 'connections' => [ ... 'rabbitmq' => [ 'driver' => 'rabbitmq', 'connection' => 'default', 'queue' => env('RABBITMQ_QUEUE', 'default'), ], ... ] ... ];
License
The MIT License (MIT). Please see License File for more information.
ludovicose/transaction-outbox 适用场景与选型建议
ludovicose/transaction-outbox 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.44k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2022 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「pub-sub」 「transactional-outbox」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ludovicose/transaction-outbox 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ludovicose/transaction-outbox 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ludovicose/transaction-outbox 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Asynchronous processing for Symfony using Push Queues
Event Flow helps you to create event driven applications with Laravel
A simple PHP pub-sub library
Nats jetstream client for Laravel
Laravel NATS - v2 basis stack (basis-company/nats), subscriber, NatsV2 JetStream helpers, queue driver, legacy JetStream, Artisan commands
Alfabank REST API integration
统计信息
- 总下载量: 20.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-17