This commit is contained in:
iVampireSP.com 2022-10-31 15:35:59 +08:00
parent 68020d67a5
commit 93a3497b76
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
4 changed files with 84 additions and 36 deletions

View File

@ -75,40 +75,49 @@ public function handle()
$this->info('正在准备节点');
$instance_id = config('app.instance_id');
// 检测其他 莱云 计算节点
$nodes = Cache::get('nodes', collect([]));
// 检测节点是否在集合里
$node = $nodes->where('instance_id', $instance_id)->first();
if ($node == null) {
$this->warn('节点未注册');
$this->info('正在注册节点');
// add to collect
$nodes->push([
$inst_data = [
'instance_id' => $instance_id,
'ip' => $addr,
'port' => $port,
'type' => $type,
]);
];
$this->warn('节点注册成功');
} else {
$this->warn('节点已注册');
dd(cluster_run_wait('register', $inst_data));
// 如果 IP 不同,则更新 IP
if ($node['ip'] != $addr) {
$this->info('正在更新节点 IP');
$node['ip'] = $addr;
$this->info('节点 IP 更新成功');
}
// 检测其他 莱云 计算节点
// $nodes = Cache::get('nodes', collect([]));
$node['port'] = $port;
}
// 检测节点是否在集合里
// $node = $nodes->where('instance_id', $instance_id)->first();
// save cache
Cache::forever('nodes', $nodes);
// if ($node == null) {
// $this->warn('节点未注册');
// $this->info('正在注册节点');
// // add to collect
// $nodes->push([
// 'instance_id' => $instance_id,
// 'ip' => $addr,
// 'port' => $port,
// 'type' => $type,
// ]);
// $this->warn('节点注册成功');
// } else {
// $this->warn('节点已注册');
// // 如果 IP 不同,则更新 IP
// if ($node['ip'] != $addr) {
// $this->info('正在更新节点 IP');
// $node['ip'] = $addr;
// $this->info('节点 IP 更新成功');
// }
// $node['port'] = $port;
// }
// // save cache
// Cache::forever('nodes', $nodes);

View File

@ -3,7 +3,7 @@
namespace App\Console\Commands\Cluster;
use Illuminate\Console\Command;
use Illuminate\Redis\RedisManager;
use Illuminate\Redis\Connections\PhpRedisConnection;
class Send extends Command
{
@ -22,7 +22,7 @@ class Send extends Command
protected $description = '发送节点心跳';
protected RedisManager $redis;
protected PhpRedisConnection $redis;
protected String $instance_id;
/**
@ -34,7 +34,7 @@ public function __construct()
{
parent::__construct();
$this->redis = app('redis');
$this->redis = app('redis')->connection('cluster_ready');
$this->instance_id = config('app.instance_id');
}
@ -58,13 +58,10 @@ public function handle()
while (true) {
// get cpu usage
$cpu = round($this->getCpuUsage(), 2);
// $cpu = round($this->getCpuUsage(), 2);
echo "CPU: {$cpu}%\n";
$this->redis->publish('cluster_ready', json_encode([
'instance_id' => $this->instance_id,
'cpu' => $cpu,
]));
// echo "CPU: {$cpu}%\n";
$this->publish();
sleep(1);
}
@ -91,7 +88,20 @@ public function publish()
{
$this->redis->publish('cluster_ready', json_encode([
'instance_id' => $this->instance_id,
'cpu' => 1
'task_id' => cluster_task_id(),
'type' => 'register',
'data' => [
'cpu' => $this->getCpuUsage(),
],
]));
// $this->redis->publish('cluster_ready', json_encode([
// 'instance_id' => $this->instance_id,
// 'task_id' => cluster_task_id(),
// 'type' => 'cpu_usage',
// 'data' => [
// 'cpu' => $this->getCpuUsage(),
// ],
// ]));
}
}

View File

@ -1,13 +1,39 @@
<?php
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
function now($timezone = null)
{
return Carbon::now($timezone);
}
function cluster_task_id()
{
return uniqid(config('app.instance_id') . '_');
}
function cluster_run_wait($command, $data = [])
{
$redis = app('redis')->connection('cluster_ready');
$task_id = cluster_task_id();
$redis->publish('cluster_ready', json_encode([
'task_id' => $task_id,
'type' => $command,
'data' => $data,
]));
// 等待结果,最多等待 10 秒
$result = $redis->blpop("cluster:task:{$task_id}", 10);
if ($result) {
return json_decode($result[1], true);
} else {
throw new \Exception('任务执行超时');
}
}
// function nodes()
// {

View File

@ -89,6 +89,9 @@ public function generateInstanceId()
// 追加到 .env 文件
file_put_contents($env_path, PHP_EOL . "INSTANCE_ID={$instance_id}", FILE_APPEND);
// 重新加载配置
config(['app.instance_id' => $instance_id]);
// $env = file_get_contents(app()->environmentFilePath());
// $env = preg_replace('/INSTANCE_ID=(.*)/', 'INSTANCE_ID=' . $instance_id, $env);
// file_put_contents(app()->environmentFilePath(), $env);