jimchen/laravel-trigger

基于 MySQLReplication 的 MySQL 触发器

v1.0.0 2020-05-24 02:21 UTC

This package is auto-updated.

Last update: 2024-09-24 13:02:20 UTC


README

Latest Stable Version Total Downloads GitHub license

订阅 MySQL 事件,如 jQuery,基于 php-mysql-replication

中文说明

MySQL 服务器设置

在你的 MySQL 服务器配置文件中,你需要启用复制

[mysqld]
server-id        = 1
log_bin          = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size  = 100M
binlog_row_image = full
binlog-format    = row #Very important if you want to receive write, update and delete row events
Mysql replication events explained https://dev.mysqlserver.cn/doc/internals/en/event-meanings.html

MySQL 用户权限

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'user'@'host';

GRANT SELECT ON `dbName`.* TO 'user'@'host';

安装

Laravel

安装

composer require jimchen/laravel-trigger

发布配置

php artisan vendor:publish --provider="JimChen\Trigger\TriggerServiceProvider"

Lumen

安装

composer require jimchen/laravel-trigger

编辑 bootstrap/app.php 添加

$app->register(JimChen\Trigger\TriggerServiceProvider::class);
...
$app->configure('trigger');

发布配置和路由

php artisan trigger:install [--force]

配置

编辑 .env,添加

TRIGGER_HOST=192.168.xxx.xxx
TRIGGER_PORT=3306
TRIGGER_USER=username
TRIGGER_PASSWORD=password
...

用法

php artisan trigger:start [-R=xxx]

订阅者

<?php
namespace App\Listeners;

use JimChen\Trigger\EventSubscriber;

class ExampeSubscriber extends EventSubscriber
{
    public function onUpdate(UpdateRowsDTO $event)
    {
        //
    }

    public function onDelete(DeleteRowsDTO $event)
    {
        //
    }

    public function onWrite(WriteRowsDTO $event)
    {
        //
    }
}

更多订阅者用法

事件订阅者

事件路由

通用

$trigger->on('database.table', 'write', function($event) { /* do something */ });

多表和多事件

$trigger->on('database.table1,database.table2', 'write,update', function($event) { /* do something */ });

多事件

$trigger->on('database.table1,database.table2', [
    'write'  => function($event) { /* do something */ },
    'update' => function($event) { /* do something */ },
]);

作为控制器执行动作

$trigger->on('database.table', 'write', 'App\\Http\\Controllers\\ExampleController'); // call default method 'handle'
$trigger->on('database.table', 'write', 'App\\Http\\Controllers\\ExampleController@write');

作为可调用执行动作

class Foo
{
    public static function bar($event)
    {
        dump($event);
    }
}

$trigger->on('database.table', 'write', 'Foo@bar'); // call default method 'handle'
$trigger->on('database.table', 'write', ['Foo', 'bar']);

作为作业执行动作

作业

namespace App\Jobs;

class ExampleJob extends Job
{
    private $event;

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

    public function handle()
    {
        dump($this->event);
    }
}

路由

$trigger->on('database.table', 'write', 'App\Jobs\ExampleJob'); // call default method 'dispatch'
$trigger->on('database.table', 'write', 'App\Jobs\ExampleJob@dispatch_now');

事件列表

php artisan trigger:list [-R=xxx]

终止

php artisan trigger:terminate [-R=xxx]

感谢

JetBrains