softonic/laravel-transactional-event-publisher
Composer 安装命令:
composer require softonic/laravel-transactional-event-publisher
包简介
Softonic Laravel Transactional Event Publisher
README 文档
README
Laravel package to handle atomicity between Eloquent model operations and domain event message generation.
Requirements:
- PHP >= 8.5
- Laravel 12.x
Main features
- Ensure every action has a domain event sent using an atomic transaction between Eloquent model operation, event generation and sent.
- Events sent to a AMQP system sync or async.
- Command to send all the events until now.
Installation
You can require the last version of the package using composer
composer require softonic/laravel-transactional-event-publisher
Configuration
It is possible to configure the basic AMQP information, you can check it in vendor/softonic/transactional-event-publisher/config/transactional-event-publisher.php
If you need further customization, you can publish the configuration.
php artisan vendor:publish --provider="Softonic\TransactionalEventPublisher\ServiceProvider" --tag=config
We provide Softonic\TransactionalEventPublisher\EventStoreMiddlewares\DatabaseMiddleware
and Softonic\TransactionalEventPublisher\EventStoreMiddlewares\AmqpMiddleware middlewares to store and send events.
Database middleware
This middleware just stores the events in a table in database. It can be useful if you want to expose the events as a REST endpoint or check your events history.
To configure this middleware you need to publish the migrations
php artisan vendor:publish --provider="Softonic\TransactionalEventPublisher\ServiceProvider" --tag=migrations
and execute them
php artisan migrate
Amqp middleware
This middleware publishes the events to an AMQP system. You just need to configure the AMQP connection using the configuration file or environmental variables. As you can see, in the configuration you won't be able to define a queue. This is because the library just publishes the message to an exchange and is the events collector responsibility to declare the needed queues with the needed bindings.
Publishing events in batches to improve performance
We provide a command to continuously publish events in batches.
You can find its signature in Softonic\TransactionalEventPublisher\Console\Commands\EmitEvents.
It will publish the events in batches of 100 by default, or you can change it with the option --batchSize.
You just need to create a job that will run indefinitely with the command php artisan event-sourcing:emit.
Sending all the events stored in database
By default, the command php artisan event-sourcing:emit will send all the events stored in database using a MySQL unbuffered connection.
Otherwise, a Mysql buffered Connection it is used to delete the events from database after they have been sent.
You can specify the connection to use with the option --dbConnection for the buffered connection and --dbConnectionUnbuffered for the unbuffered connection.
Unbuffered connection example from config/database.php
return [ 'connections' => [ 'mysql-unbuffered' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'options' => [ PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false, ], ], ] ];
Registering Models
To choose what models should send domain events, you need to attach the \Softonic\TransactionalEventPublisher\ModelObserver observer class.
Example:
...
use App\Models\Post as MyModel;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Softonic\TransactionalEventPublisher\Observers\ModelObserver;
class EventServiceProvider extends ServiceProvider
{
public function boot()
{
parent::boot();
MyModel::observe(ModelObserver::class);
}
...
}
Custom middlewares
The middlewares should implement the Softonic\TransactionalEventPublisher\Interfaces\EventStoreMiddlewareInterface interface.
Its purpose is to store the domain event provided, so you can implement any storage for domain events.
Custom messages
The transactional-event.messageBuilder class must implement EventMessageBuilderInterface and transactional-event.middleware class must implement EventStoreMiddlewareInterface.
The builder should return a EventMessageInterface value object. It just needs to implement the toArray and jsonSerialize methods with all the attributes that you need.
Considerations
This package begins a database transaction in the following Eloquent Model events:
- creating
- updating
- deleting
And commit the database transaction when the event store middleware stores the event message successfully. On the other hand, if the event store couldn't store the event message would be a database rollback for the two operations (Eloquent model write + event message storing). Take into account if an error occurs between the event of creating/updating/deleting and created/updated/deleted the transaction would remain started until the connection had been closed.
Testing
softonic/laravel-transactional-event-publisher has a PHPUnit test suite and a coding style compliance test suite using PHP CS Fixer following PSR-12.
To run the tests, run the following command from the project folder:
$ docker compose run --rm tests
To run PHPUnit only:
$ docker compose run --rm phpunit
To run PHPStan static analysis:
$ docker compose run --rm phpstan
To check code style:
$ docker compose run --rm php composer run checkstyle
To fix code style issues:
$ docker compose run --rm fixcs
To open a terminal in the dev environment:
$ docker compose run --rm --entrypoint=bash php
For debugging with Xdebug:
$ docker compose run --rm debug
License
The Apache 2.0 license. Please see LICENSE for more information.
softonic/laravel-transactional-event-publisher 适用场景与选型建议
softonic/laravel-transactional-event-publisher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.38k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「cqrs」 「event store」 「softonic」 「event publisher」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 softonic/laravel-transactional-event-publisher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 softonic/laravel-transactional-event-publisher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 softonic/laravel-transactional-event-publisher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Interfaces, Traits and Nominal Classes used by Domain Entities Implementing Domain Events
Implementation of the transactional outbox pattern on top of rekalogika/domain-event
Symfony bundle for broadway/broadway.
This is an Event Emitter equivalent to Node.js' Event Emitter.
Publish / Subscribe / Event Manager
An implementation of such kind of patterns as: Command Bus, Query Bus and Event Bus; in a single package; driven by a Dependency Injection Container
统计信息
- 总下载量: 19.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-03-16