提前准备集群

This commit is contained in:
iVampireSP.com 2022-10-29 14:18:22 +08:00
parent 00f49352a2
commit 9e7824d402
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
4 changed files with 118 additions and 10 deletions

View File

@ -1,28 +1,26 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands\Cluster;
use App\Models\Module\Module;
use DB; use DB;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class Check extends Command class Init extends Command
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'check'; protected $signature = 'cluster:init';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = '检查服务可用性'; protected $description = '初始化此节点。当公网 IP 发生变化时,需要重新初始化。';
/** /**
* Create a new command instance. * Create a new command instance.
@ -95,9 +93,9 @@ public function handle()
'type' => $type, 'type' => $type,
]); ]);
$this->info('节点注册成功'); $this->warn('节点注册成功');
} else { } else {
$this->info('节点已注册'); $this->warn('节点已注册');
// 如果 IP 不同,则更新 IP // 如果 IP 不同,则更新 IP
if ($node['ip'] != $addr) { if ($node['ip'] != $addr) {

View File

@ -0,0 +1,42 @@
<?php
namespace App\Console\Commands\Cluster;
use Illuminate\Console\Command;
class Sync extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cluster:sync';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步节点存储的数据';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace App\Console\Commands\Cluster;
use Illuminate\Console\Command;
class Worker extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cluster:work';
/**
* The console command description.
*
* @var string
*/
protected $description = '开始节点协调工作';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
$redis = app('redis');
$instance_id = config('app.instance_id');
while (true) {
// get cpu usage
$cpu = round($this->getCpuUsage(), 2);
$redis->publish('cluster_ready', json_encode([
'instance_id' => $instance_id,
'cpu' => $cpu,
]));
sleep(1);
}
}
public function getCpuUsage()
{
$load = sys_getloadavg();
return $load[0];
}
}

View File

@ -4,7 +4,7 @@
use App\Console\Commands\BanUser; use App\Console\Commands\BanUser;
use App\Console\Commands\CalcModule; use App\Console\Commands\CalcModule;
use App\Console\Commands\Check; use App\Console\Commands\Cluster;
use App\Console\Commands\Count; use App\Console\Commands\Count;
use App\Console\Commands\GetUser; use App\Console\Commands\GetUser;
use App\Console\Commands\ReduceBalance; use App\Console\Commands\ReduceBalance;
@ -40,7 +40,9 @@ class Kernel extends ConsoleKernel
ReduceBalance::class, ReduceBalance::class,
Count::class, Count::class,
Status::class, Status::class,
Check::class, Cluster\Init::class,
Cluster\Worker::class,
Cluster\Sync::class,
Commands\System\Down::class, Commands\System\Down::class,
Commands\System\Up::class, Commands\System\Up::class,
]; ];