Cluster Ready

This commit is contained in:
iVampireSP.com 2022-10-29 13:53:32 +08:00
parent 6ece0651df
commit 00f49352a2
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
7 changed files with 205 additions and 3 deletions

View File

@ -39,5 +39,10 @@ ALIPAY_APP_ID=
FORUM_BASEURL=https://forum.laecloud.com
# 实例类型,如果是备用节点,需要设置为 backup。否则设置为 primary
INSTANCE_TYPE=primary
# 实例地址,留空会自动获取 IP。
INSTANCE_ADDRESS=
SOKETI_DEBUG=0

View File

@ -0,0 +1,129 @@
<?php
namespace App\Console\Commands;
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
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check';
/**
* 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()
{
//
$this->warn('检查服务可用性');
// 检查是否可以连接到数据库
try {
DB::connection()->getPdo();
$this->info('数据库连接正常');
} catch (\Exception $e) {
$this->error($e->getMessage());
}
// 检测是否可以连接到 Redis
try {
$redis = app('redis');
$redis->ping();
$this->info('Redis 连接正常');
} catch (\Exception $e) {
$this->error($e->getMessage());
}
if (config('app.instance_address')) {
$addr = $this->info('当前实例地址: ' . config('app.instance_address'));
} else {
$this->info('正在获取公网 IP');
// get public ip
$addr = file_get_contents('https://ifconfig.me/ip');
$this->warn('公网 IP: ' . $addr);
}
$port = config('laravels.listen_port');
$type = config('app.instance_type');
$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([
'instance_id' => $instance_id,
'ip' => $addr,
'port' => $port,
'type' => $type,
]);
$this->info('节点注册成功');
} else {
$this->info('节点已注册');
// 如果 IP 不同,则更新 IP
if ($node['ip'] != $addr) {
$this->info('正在更新节点 IP');
$node['ip'] = $addr;
$this->info('节点 IP 更新成功');
}
$node['port'] = $port;
}
// save cache
Cache::forever('nodes', $nodes);
// 检测模块是否正常
// Module::chunk(100, function ($modules) {
// foreach ($modules as $module) {
// $this->info('检测模块 ' . $module->name);
// if($module->check()) {
// $this->warn('模块 ' . $module->name . ' 正常');
// } else {
// $this->error('模块 ' . $module->name . ' 异常');
// }
// }
// });
}
}

View File

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

View File

@ -7,3 +7,16 @@ function now($timezone = null)
{
return Carbon::now($timezone);
}
// function nodes()
// {
// return Cache::remember('nodes', 60, function () {
// $collection = collect(['taylor', 'abigail', null])->map(function ($name) {
// return strtoupper($name);
// })->reject(function ($name) {
// return empty($name);
// });
// });
// }

View File

@ -3,11 +3,13 @@
namespace App\Models\Module;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Http;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
@ -130,6 +132,29 @@ public function remotePost($path = '', $data = [])
return [$json, $status];
}
public function check($module_id = null)
{
if ($module_id) {
$module = Module::find($module_id);
} else {
$module = $this;
}
try {
$http = Http::remote($module->api_token, $module->url);
// dd($module->url);
$response = $http->get('remote');
} catch (ConnectException $e) {
Log::error($e->getMessage());
}
if ($response->status() == 200) {
return true;
} else {
return false;
}
}
// get cached modules
public static function cached_modules()

View File

@ -2,12 +2,14 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Alipay\EasySDK\Kernel\Config as AlipayConfig;
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
use Dotenv\Dotenv;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use EasyWeChat\Pay\Application as WePay;
use Alipay\EasySDK\Kernel\Config as AlipayConfig;
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
class AppServiceProvider extends ServiceProvider
{
@ -25,6 +27,7 @@ public function register()
public function boot()
{
$this->generateInstanceId();
//
// header('server: Cluster Ready!');
@ -112,4 +115,23 @@ private function alipayOptions()
return $options;
}
public function generateInstanceId()
{
if (config('app.instance_id') == null) {
$instance_id = md5(uniqid());
// 获取 .env 目录
$env_path = dirname(__DIR__) . '/../.env';
// 追加到 .env 文件
file_put_contents($env_path, PHP_EOL . "INSTANCE_ID={$instance_id}", FILE_APPEND);
// $env = file_get_contents(app()->environmentFilePath());
// $env = preg_replace('/INSTANCE_ID=(.*)/', 'INSTANCE_ID=' . $instance_id, $env);
// file_put_contents(app()->environmentFilePath(), $env);
}
}
}

View File

@ -108,4 +108,10 @@
'cipher' => 'AES-256-CBC',
'instance_id' => env('INSTANCE_ID', null),
'instance_type' => env('INSTANCE_TYPE', 'primary'),
'instance_address' => env('INSTANCE_address', null),
];