From 9bd0f11e16e0ce8e8b59dfe5aa0d6b982506fe0e Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Wed, 4 Jan 2023 20:18:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Cluster=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=B1=BB=E3=80=82=E7=94=BB=E9=A5=BC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Cluster/Log.php | 33 ++++++++++ app/Console/Commands/Cluster/Sync.php | 18 +++--- app/Console/Commands/Cluster/Upload.php | 18 +++--- app/Console/Commands/Cluster/Work.php | 33 ++++++++++ app/Console/Commands/Init.php | 21 ++++--- app/Support/Cluster.php | 84 +++++++++++++++++++++++++ 6 files changed, 179 insertions(+), 28 deletions(-) create mode 100644 app/Console/Commands/Cluster/Log.php create mode 100644 app/Console/Commands/Cluster/Work.php create mode 100644 app/Support/Cluster.php diff --git a/app/Console/Commands/Cluster/Log.php b/app/Console/Commands/Cluster/Log.php new file mode 100644 index 0000000..2c9d063 --- /dev/null +++ b/app/Console/Commands/Cluster/Log.php @@ -0,0 +1,33 @@ +info('检查下载目录的 MD5 值。'); - $config_md5_key = "cluster:master_config_md5"; - $config_md5 = Cache::get($config_md5_key, ''); + $config_md5_key = "master_config_md5"; + $config_md5 = Cluster::get($config_md5_key, ''); $md5 = md5($config); if ($md5 !== $config_md5) { @@ -86,11 +86,11 @@ public function handle(): int if ($node_type === 'slave') { // 下载 .env 文件 $this->info('正在下载 .env 文件。'); - $env_cache_key = "cluster:${node_type}_env"; - $env_md5_key = "cluster:${node_type}_env_md5"; + $env_cache_key = "${node_type}_env"; + $env_md5_key = "${node_type}_env_md5"; - $env = Cache::get($env_cache_key); - $env_md5 = Cache::get($env_md5_key); + $env = Cluster::get($env_cache_key); + $env_md5 = Cluster::get($env_md5_key); $this->info('检查 .env 文件的 MD5 值。'); if (md5($env) !== $env_md5) { diff --git a/app/Console/Commands/Cluster/Upload.php b/app/Console/Commands/Cluster/Upload.php index 07d53f0..2c60f31 100644 --- a/app/Console/Commands/Cluster/Upload.php +++ b/app/Console/Commands/Cluster/Upload.php @@ -2,9 +2,9 @@ namespace App\Console\Commands\Cluster; +use App\Support\Cluster; use Illuminate\Console\Command; use Illuminate\Support\Facades\Artisan; -use Illuminate\Support\Facades\Cache; use Symfony\Component\Console\Command\Command as CommandAlias; use ZipArchive; @@ -112,26 +112,26 @@ public function upload($node_type) $this->info('正在上传 config 目录。'); - $cache_key = "cluster:${node_type}_config"; - Cache::forever($cache_key, file_get_contents($cacheZip)); + $cache_key = "${node_type}_config"; + Cluster::forever($cache_key, file_get_contents($cacheZip)); // md5 $this->info('正在报告 cache 目录的 MD5 值。'); - $cache_md5_key = "cluster:${node_type}_config_md5"; - Cache::forever($cache_md5_key, md5_file($cacheZip)); + $cache_md5_key = "${node_type}_config_md5"; + Cluster::forever($cache_md5_key, md5_file($cacheZip)); unlink($cacheZip); } // 上传 .env 文件 $this->info('正在上传 .env 文件。'); - $env_key = "cluster:${node_type}_env"; - Cache::forever($env_key, file_get_contents(base_path('.env'))); + $env_key = "${node_type}_env"; + Cluster::forever($env_key, file_get_contents(base_path('.env'))); // 上传 .env 文件的 MD5 $this->info('正在报告 .env 文件的 MD5 值。'); - $env_md5_key = "cluster:${node_type}_env_md5"; - Cache::forever($env_md5_key, md5_file(base_path('.env'))); + $env_md5_key = "${node_type}_env_md5"; + Cluster::forever($env_md5_key, md5_file(base_path('.env'))); $this->info('完成。'); } diff --git a/app/Console/Commands/Cluster/Work.php b/app/Console/Commands/Cluster/Work.php new file mode 100644 index 0000000..a3e9e91 --- /dev/null +++ b/app/Console/Commands/Cluster/Work.php @@ -0,0 +1,33 @@ +error('请先配置节点 IP。'); + return CommandAlias::FAILURE; + } + // 重写 .env 文件中的 NODE_ID $this->info('正在重写 .env 文件中的 NODE_ID。'); @@ -45,22 +52,16 @@ public function handle(): int file_put_contents(base_path('.env'), $env); - if (!config('settings.node.ip')) { - $this->error('请先配置节点 IP。'); - return CommandAlias::FAILURE; - } // 刷新配置缓存 $this->info('正在刷新配置缓存。'); Artisan::call('config:cache'); // redis 创建一个 hash - $this->info('正在创建 Redis hash。'); - $redis = app('redis')->connection(); - $redis->hset('nodes', $node_id, [ - 'type' => config('settings.node.type'), - 'id' => $node_id, - ]); + $this->info('正在注册节点。'); + Cluster::registerThisNode(); + + $this->info('初始化完成。'); return CommandAlias::SUCCESS; } diff --git a/app/Support/Cluster.php b/app/Support/Cluster.php new file mode 100644 index 0000000..54e09bc --- /dev/null +++ b/app/Support/Cluster.php @@ -0,0 +1,84 @@ + [ + 'type' => config('settings.node.type'), + 'id' => config('settings.node.id'), + 'ip' => config('settings.node.ip'), + ], + 'data' => $data, + ])); + } + + public static function hset($key, $value, $data = []): void + { + Redis::hset($key, $value, json_encode($data)); + } + + public static function get($key, $default = null): string|array|null + { + return Cache::get(self::$prefix . $key, $default); + } + + public static function set($key, $value, $ttl = null): void + { + Cache::put(self::$prefix . $key, $value, $ttl); + } + + public static function forget($key): void + { + Cache::forget(self::$prefix . $key); + } + + // forever + public static function forever($key, $value): void + { + Cache::forever(self::$prefix . $key, $value); + } + + public static function hget($key, $value, $default = null): string|array|null + { + return Redis::hget($key, $value, $default); + } + + + public static function registerThisNode(): void + { + $node_id = config('settings.node.id'); + + Cluster::hset('nodes', $node_id, [ + 'type' => config('settings.node.type'), + 'id' => $node_id, + 'ip' => config('settings.node.ip'), + ]); + + Cluster::publish('node_init'); + } +}