b2pweb/bdf-prime-events
Composer 安装命令:
composer require b2pweb/bdf-prime-events
包简介
Prime library for listen MySQL events from replication protocol
README 文档
README
Prime extension for listen MySQL replication events on Prime entities to track insert, update and delete operations.
Installation
Install with composer :
composer require b2pweb/bdf-prime-events
Configuration on Symfony
Register into config/bundles.php :
<?php return [ // ... Bdf\PrimeEvents\Bundle\PrimeEventsBundle::class => ['all' => true], Bdf\PrimeBundle\PrimeBundle::class => ['all' => true], ];
Configure indexes into config/packages/prime_events.yaml :
prime_events: # Configure replication connection parameter here, by connection name my_connection: user: other_user # Define a custom username/password which as REPLICATION CLIENT and REPLICATION SLAVE permissions password: other_pass logPositionFile: '%kernel.project_dir%/var/events' # The file for store the last consumed event, to allow restart consumer without loose events
Enable autoconfigure on application to let Symfony container configure the listeners :
services: _defaults: autowire: true autoconfigure: true App\Entity\Listener\: resource: './src/Entity/Listener'
Configure MySQL
See krowinski/php-mysql-replication for enable replication protocol on the MySQL server.
Usage
Prime entities are use for events, see Create your mapper to define an entity.
Simple usage / without Symfony
Simply create an EntityEventsConsumer, define listeners, and run the consumer :
use Bdf\PrimeEvents\EntityEventsConsumer; use MySQLReplication\Config\ConfigBuilder; $consumer = new EntityEventsConsumer( $prime, // The ServiceLocator instance __DIR__.'/mysql_last_event.log', // File for store the last consumed event, to allow restart without loosing events function (ConfigBuilder $config) { $config // Define custom connection configuration // Note: by default, the connection user and password is used // So it's not required to redefine it if the user has the replication permissions ->withUser('replication_user') ->withPassword('replication_pass') // Define the slave id. define this value is required if you want to run multiple // consumers on the same database ->withSlaveId(123) ; } ); // Configure listener for MyEntity $consumer->forEntity(MyEntity::class) ->inserted(function (MyEntity $entity) { /* $entity has been inserted */}) ->updated(function (MyEntity $before, MyEntity $now) { /* The entity has been updated. $before is its value before the update, and $now the current value */ }) ->deleted(function (MyEntity $entity) { /* $entity has been deleted */}) ; // Other entities may be configure... // Consume all events // Note: consume() will only consume 1 event while ($running) { $consumer->consume(); } // Stop the consumer and save the last consumed events $consumer->stop();
Usage with Symfony
Symfony will autoconfigure the listeners if there implements EntityEventsListenerInterface :
use Bdf\PrimeEvents\Factory\EntityEventsListenerInterface; /** * @implements EntityEventsListenerInterface<MyEntity> */ class MyEntityListeners implements EntityEventsListenerInterface { /** * {@inheritdoc} */ public function entityClass() : string { return MyEntity::class; } /** * {@inheritdoc} * @param MyEntity $entity */ public function onInsert($entity) : void { // $entity has been inserted } /** * {@inheritdoc} * @param MyEntity $oldEntity * @param MyEntity $newEntity */ public function onUpdate($oldEntity, $newEntity) : void { // The entity has been updated. // $before is its value before the update, and $now the current value } /** * {@inheritdoc} * @param MyEntity $entity */ public function onDelete($entity) : void { // $entity has been deleted } }
To consume events, simply launch prime:events:consume command :
bin/console prime:events:consume my_connection --limit 10000 --memory 500m
b2pweb/bdf-prime-events 适用场景与选型建议
b2pweb/bdf-prime-events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 332 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 b2pweb/bdf-prime-events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 b2pweb/bdf-prime-events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 332
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-02