像jQuery一样订阅MySQL事件, 基于 php-mysql-replication
同步配置:
[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.mysql.com/doc/internals/en/event-meanings.html
用户权限:
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'user'@'host';
GRANT SELECT ON `dbName`.* TO 'user'@'host';
composer 安装
composer require "huangdijia/laravel-trigger:^4.0"
发布配置
php artisan vendor:publish --provider="Huangdijia\Trigger\TriggerServiceProvider"
composer 安装
composer require "huangdijia/laravel-trigger:^4.0"
编辑 bootstrap/app.php
,注册服务及加载配置:
$app->register(Huangdijia\Trigger\TriggerServiceProvider::class);
...
$app->configure('trigger');
publish config and route
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 Huangdijia\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]