改进 节点注册和更新

This commit is contained in:
iVampireSP.com 2023-02-09 18:56:47 +08:00
parent 118e5e0550
commit f852717191
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132

View File

@ -42,33 +42,50 @@ public static function publish($event, $data = [], $register = false): void
} }
} }
public static function registerThisNode($report = true): void public static function registerThisNode($report = true, $data = []): void
{ {
$node_id = config('settings.node.id'); $node_id = config('settings.node.id');
ClusterSupport::hset('nodes', $node_id, [ $node = [
'type' => config('settings.node.type'), 'type' => config('settings.node.type'),
'id' => $node_id, 'id' => $node_id,
'ip' => config('settings.node.ip'), 'ip' => config('settings.node.ip'),
// utc +8 timestamp
'last_heartbeat' => time(), 'last_heartbeat' => time(),
]); ];
$node = array_merge($node, $data);
self::updateThisNode($node);
if ($report) { if ($report) {
ClusterSupport::publish('node.ok'); ClusterSupport::publish('node.ok');
} }
} }
public static function updateThisNode($data = []): void
{
$node_id = config('settings.node.id');
$node = self::hget('nodes', $node_id, "[]");
$node = json_decode($node, true);
$node = array_merge($node, $data);
ClusterSupport::hset('nodes', $node_id, $node);
}
public static function hset($key, $value, $data = []): void public static function hset($key, $value, $data = []): void
{ {
/** @noinspection PhpUndefinedMethodInspection */ /** @noinspection PhpUndefinedMethodInspection */
Redis::hset(self::$prefix.$key, $value, json_encode($data)); Redis::hset(self::$prefix . $key, $value, json_encode($data));
} }
/** /**
* @param string|array $events 事件名称 * @param string|array $events 事件名称
* @param $callback callable 回调函数,接收一个参数,为事件数据。 * @param $callback callable 回调函数,接收一个参数,为事件数据。
* @param $ignore_self bool 是否忽略此节点的消息。 * @param $ignore_self bool 是否忽略此节点的消息。
*
* @return void * @return void
*/ */
public static function listen(string|array $events, callable $callback, bool $ignore_self = true): void public static function listen(string|array $events, callable $callback, bool $ignore_self = true): void
@ -99,13 +116,13 @@ public static function listen(string|array $events, callable $callback, bool $ig
public static function get($key, $default = null): string|array|null public static function get($key, $default = null): string|array|null
{ {
/** @noinspection PhpUndefinedMethodInspection */ /** @noinspection PhpUndefinedMethodInspection */
return Redis::get(self::$prefix.$key, $default); return Redis::get(self::$prefix . $key, $default);
} }
public static function forget($key): void public static function forget($key): void
{ {
/** @noinspection PhpUndefinedMethodInspection */ /** @noinspection PhpUndefinedMethodInspection */
Redis::forget(self::$prefix.$key); Redis::forget(self::$prefix . $key);
} }
// forever // forever
@ -117,13 +134,13 @@ public static function forever($key, $value): void
public static function set($key, $value, $ttl = null): void public static function set($key, $value, $ttl = null): void
{ {
/** @noinspection PhpUndefinedMethodInspection */ /** @noinspection PhpUndefinedMethodInspection */
Redis::set(self::$prefix.$key, $value, $ttl); Redis::set(self::$prefix . $key, $value, $ttl);
} }
public static function hget($key, $hashKey, $default = []): string|array|null public static function hget($key, $hashKey, $default = []): string|array|null
{ {
/** @noinspection PhpUndefinedMethodInspection */ /** @noinspection PhpUndefinedMethodInspection */
$value = Redis::hget(self::$prefix.$key, $hashKey); $value = Redis::hget(self::$prefix . $key, $hashKey);
return $value ?: $default; return $value ?: $default;
} }
@ -150,7 +167,7 @@ public static function nodes($hide_ip = false): array
public static function hgetAll($hashKey, $default = []): array public static function hgetAll($hashKey, $default = []): array
{ {
/** @noinspection PhpUndefinedMethodInspection */ /** @noinspection PhpUndefinedMethodInspection */
$value = Redis::hgetall(self::$prefix.$hashKey); $value = Redis::hgetall(self::$prefix . $hashKey);
return $value ?: $default; return $value ?: $default;
} }