提前准备集群
This commit is contained in:
parent
00f49352a2
commit
9e7824d402
@ -1,28 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
namespace App\Console\Commands\Cluster;
|
||||
|
||||
use App\Models\Module\Module;
|
||||
use DB;
|
||||
use Illuminate\Console\Command;
|
||||
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.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'check';
|
||||
protected $signature = 'cluster:init';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '检查服务可用性';
|
||||
protected $description = '初始化此节点。当公网 IP 发生变化时,需要重新初始化。';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
@ -95,9 +93,9 @@ public function handle()
|
||||
'type' => $type,
|
||||
]);
|
||||
|
||||
$this->info('节点注册成功');
|
||||
$this->warn('节点注册成功');
|
||||
} else {
|
||||
$this->info('节点已注册');
|
||||
$this->warn('节点已注册');
|
||||
|
||||
// 如果 IP 不同,则更新 IP
|
||||
if ($node['ip'] != $addr) {
|
42
app/Console/Commands/Cluster/Sync.php
Normal file
42
app/Console/Commands/Cluster/Sync.php
Normal 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()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
66
app/Console/Commands/Cluster/Worker.php
Normal file
66
app/Console/Commands/Cluster/Worker.php
Normal 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];
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
|
||||
use App\Console\Commands\BanUser;
|
||||
use App\Console\Commands\CalcModule;
|
||||
use App\Console\Commands\Check;
|
||||
use App\Console\Commands\Cluster;
|
||||
use App\Console\Commands\Count;
|
||||
use App\Console\Commands\GetUser;
|
||||
use App\Console\Commands\ReduceBalance;
|
||||
@ -40,7 +40,9 @@ class Kernel extends ConsoleKernel
|
||||
ReduceBalance::class,
|
||||
Count::class,
|
||||
Status::class,
|
||||
Check::class,
|
||||
Cluster\Init::class,
|
||||
Cluster\Worker::class,
|
||||
Cluster\Sync::class,
|
||||
Commands\System\Down::class,
|
||||
Commands\System\Up::class,
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user