umbrellio / php-table-sync
PHP 实现的库,提供微服务之间的数据同步
3.1.0
2023-08-07 12:44 UTC
Requires
- php: ^8.1
- ext-json: *
- ext-posix: *
- influxdb/influxdb-php: ^1.15
- laravel/framework: ^10.0
- monolog/monolog: ^3.0
- php-amqplib/php-amqplib: ^3.0
- thecodingmachine/safe: ^2.0
- umbrellio/laravel-heavy-jobs: ^3.0
Requires (Dev)
- laravel/legacy-factories: *
- mikey179/vfsstream: ^1.6
- mockery/mockery: ^1.0
- orchestra/testbench: ^8.0
- php-coveralls/php-coveralls: ^2.1
- php-mock/php-mock: ^2.0
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.5
- symplify/easy-coding-standard: ^11.0
README
PHP 实现的库,提供微服务之间的数据同步
安装
composer require umbrellio/php-table-sync php artisan vendor:publish --tag=config-table-sync
使用
以下是一个示例 User.php
,描述需要同步的模型
... User extends Model implements SyncableModel { use TableSyncable; ... public function routingKey(): string { return 'users'; } public function getTableSyncableAttributes(): array { return [ 'id' => $this->external_id, 'login' => $this->name, 'email' => $this->email, ]; } ...
当模型发生变化时,数据将根据 TableSyncObserver
的规则发送,要获取所需数据,请运行命令 table_sync:work
日志记录
基于 Monolog 包的日志记录,并包含一些扩展。
- 在
config/table_sync.php
中指定日志通道
... 'log' => [ 'channel' => 'table_sync', ], ...
- 并在
config/logging.php
中描述此通道
... 'table_sync' => [ 'driver' => 'stack', 'channels' => ['table_sync_daily', 'influxdb'], ], 'table_sync_daily' => [ 'driver' => 'daily', 'formatter' => LineTableSyncFormatter::class, 'formatter_with' => [ 'format' => '[%datetime%] %message% - %model% %event%', ], 'path' => storage_path('logs/table_sync/daily.log'), ], 'influxdb' => [ 'driver' => 'influxdb', 'measurement' => 'table_sync', ], ...
您可以使用内置的 LineTableSyncFormatter::class
,并带有以下可用参数:%datetime%
%message%
%direction%
%model%
%event%
%routing%
%attributes%
%exception%
驱动程序 influxdb
是一个附加选项,不需要在配置中添加
... 'table_sync' => [ 'driver' => 'daily', ], ...
作者
由 Korben Dallas 创建。