umbrellio /php-table-sync

PHP 实现的库,提供微服务之间的数据同步

3.1.0 2023-08-07 12:44 UTC

README

PHP 实现的库,提供微服务之间的数据同步

Github Status Coverage Status Latest Stable Version Total Downloads Code Intelligence Status Build Status Code Coverage Scrutinizer Code Quality

安装

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 创建。

Supported by Umbrellio