改进
This commit is contained in:
parent
68020d67a5
commit
93a3497b76
@ -75,40 +75,49 @@ public function handle()
|
|||||||
$this->info('正在准备节点');
|
$this->info('正在准备节点');
|
||||||
$instance_id = config('app.instance_id');
|
$instance_id = config('app.instance_id');
|
||||||
|
|
||||||
// 检测其他 莱云 计算节点
|
$inst_data = [
|
||||||
$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([
|
|
||||||
'instance_id' => $instance_id,
|
'instance_id' => $instance_id,
|
||||||
'ip' => $addr,
|
'ip' => $addr,
|
||||||
'port' => $port,
|
'port' => $port,
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
]);
|
];
|
||||||
|
|
||||||
$this->warn('节点注册成功');
|
dd(cluster_run_wait('register', $inst_data));
|
||||||
} else {
|
|
||||||
$this->warn('节点已注册');
|
|
||||||
|
|
||||||
// 如果 IP 不同,则更新 IP
|
// 检测其他 莱云 计算节点
|
||||||
if ($node['ip'] != $addr) {
|
// $nodes = Cache::get('nodes', collect([]));
|
||||||
$this->info('正在更新节点 IP');
|
|
||||||
$node['ip'] = $addr;
|
|
||||||
$this->info('节点 IP 更新成功');
|
|
||||||
}
|
|
||||||
|
|
||||||
$node['port'] = $port;
|
// 检测节点是否在集合里
|
||||||
}
|
// $node = $nodes->where('instance_id', $instance_id)->first();
|
||||||
|
|
||||||
// save cache
|
// if ($node == null) {
|
||||||
Cache::forever('nodes', $nodes);
|
// $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);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
namespace App\Console\Commands\Cluster;
|
namespace App\Console\Commands\Cluster;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Redis\RedisManager;
|
use Illuminate\Redis\Connections\PhpRedisConnection;
|
||||||
|
|
||||||
class Send extends Command
|
class Send extends Command
|
||||||
{
|
{
|
||||||
@ -22,7 +22,7 @@ class Send extends Command
|
|||||||
protected $description = '发送节点心跳';
|
protected $description = '发送节点心跳';
|
||||||
|
|
||||||
|
|
||||||
protected RedisManager $redis;
|
protected PhpRedisConnection $redis;
|
||||||
protected String $instance_id;
|
protected String $instance_id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,7 +34,7 @@ public function __construct()
|
|||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->redis = app('redis');
|
$this->redis = app('redis')->connection('cluster_ready');
|
||||||
$this->instance_id = config('app.instance_id');
|
$this->instance_id = config('app.instance_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,13 +58,10 @@ public function handle()
|
|||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
// get cpu usage
|
// get cpu usage
|
||||||
$cpu = round($this->getCpuUsage(), 2);
|
// $cpu = round($this->getCpuUsage(), 2);
|
||||||
|
|
||||||
echo "CPU: {$cpu}%\n";
|
// echo "CPU: {$cpu}%\n";
|
||||||
$this->redis->publish('cluster_ready', json_encode([
|
$this->publish();
|
||||||
'instance_id' => $this->instance_id,
|
|
||||||
'cpu' => $cpu,
|
|
||||||
]));
|
|
||||||
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
@ -91,7 +88,20 @@ public function publish()
|
|||||||
{
|
{
|
||||||
$this->redis->publish('cluster_ready', json_encode([
|
$this->redis->publish('cluster_ready', json_encode([
|
||||||
'instance_id' => $this->instance_id,
|
'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(),
|
||||||
|
// ],
|
||||||
|
// ]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,39 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\Cache;
|
|
||||||
|
|
||||||
function now($timezone = null)
|
function now($timezone = null)
|
||||||
{
|
{
|
||||||
return Carbon::now($timezone);
|
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()
|
// function nodes()
|
||||||
// {
|
// {
|
||||||
|
@ -89,6 +89,9 @@ public function generateInstanceId()
|
|||||||
// 追加到 .env 文件
|
// 追加到 .env 文件
|
||||||
file_put_contents($env_path, PHP_EOL . "INSTANCE_ID={$instance_id}", FILE_APPEND);
|
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 = file_get_contents(app()->environmentFilePath());
|
||||||
// $env = preg_replace('/INSTANCE_ID=(.*)/', 'INSTANCE_ID=' . $instance_id, $env);
|
// $env = preg_replace('/INSTANCE_ID=(.*)/', 'INSTANCE_ID=' . $instance_id, $env);
|
||||||
// file_put_contents(app()->environmentFilePath(), $env);
|
// file_put_contents(app()->environmentFilePath(), $env);
|
||||||
|
Loading…
Reference in New Issue
Block a user