Commit 4fb4bd8b authored by Euan游根明's avatar Euan游根明

feat: 新增参数

parent faf00d54
......@@ -15,6 +15,12 @@ if (isset($GLOBALS['_composer_autoload_path'])) {
unset($file);
}
$options = getopt('', array('prepend:'));
if (isset($options['prepend'])) {
require $options['prepend'];
}
require NACOS_COMPOSER_INSTALL;
\Galaxy\NacosClient\Nacos\Nacos::run();
\ No newline at end of file
\Galaxy\NacosClient\Nacos\Nacos::run($options);
\ No newline at end of file
......@@ -6,8 +6,263 @@ class Nacos
{
public static function run()
public static function run($options)
{
var_dump("hello world");
self::configure();
$this->info('开始拉取配置文件');
$url = self::$configs['NACOS_URL'] ?? '';
$group = self::$configs['GROUP'] ?? '';
if (empty($url)){
$this->info('无nacos链接');
return;
}
$appName = env('APP_NAME');
$appEnv = env('APP_ENV');
if ($appName != $group) {
$this->info($group . '应用不匹配');
return;
}
$env = '.env';
$modelPath = base_path(). '/' . $env;
$key = $group . '-env-' . $appEnv;
$res = $this->getConfig($key, $group);
if (empty($res)) {
$log = $key . '配置不存在';
$this->info($log);
return;
}
try {
$cacheKey = 'fileMd5' . $key;
$md5File = md5($res);
$oldMd5 = Nacos::Nacosget($cacheKey);
if (!$oldMd5) {
$this->write($modelPath, $res);
Nacos::Nacosput($cacheKey, $md5File);
$logs = '首次获取配置' . $key;
$this->info($logs);
return;
}
$logs = $key . '文件未发生变更' . PHP_EOL;
if ($oldMd5 != $md5File) {
$logs = $key . '文件发生变更,修改配置项' . PHP_EOL;
Nacos::Nacosput($cacheKey, $md5File);
$this->write($modelPath, $res);
}
$this->info($logs);
} catch (\Exception $e) {
$this->info('获取配置失败' . $key.': '.$e);
}
}
/**
* 写入配置
* @param $path
* @param $res
* @return void
* @throws Exception
*/
public function write($path,$res){
$save = file_put_contents($path, $res);
if ($save === false){
throw new Exception('保存配置失败'.$path);
}
}
/**
* 获取nacos配置
* @param $dataId
* @param $group
* @param $isSync
* @return false|\GuzzleHttp\Promise\PromiseInterface|mixed|string
*/
public function getConfig($dataId, $group = 'DEFAULT_GROUP')
{
try {
return $this->requery($dataId, $group);
} catch (\Exception $e) {
$code = $e->getCode();
if ($code == 404) {
return false;
}
$this->getToken(true);
return $this->requery($dataId, $group);
}
}
/**
* 发起请求
* @param $dataId
* @param $group
* @param $isSync
* @return \GuzzleHttp\Promise\PromiseInterface|mixed|string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function requery($dataId, $group)
{
$url = self::$configs['NACOS_URL'].'/nacos/v2/cs/config?accessToken='.$this->getToken().'&dataId='.$dataId.'&group='.$group;
return $this->getConfigRequest($url);
}
/**
* 获取token
* @param $new
* @return mixed|string|null
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private function getToken($new = false)
{
$key = 'nacos:sync:get:token';
$token = Nacos::Nacosget($key);;
if (empty($token) || $new) {
$user = self::$configs['ACCOUNT'];
$pass = self::$configs['NACOS_PWD'];
$url = self::$configs['NACOS_URL'].'/nacos/v1/auth/login';
$data = ['username' => $user, 'password' =>$pass];
$token = $this->login($data ,$url);
$logs = '获取token: ' . $token;
$this->info($logs);
}
Nacos::NacosPut($key, $token);
return $token;
}
/**
* 请求nacos
* @param $url
* @return mixed|string
*/
public function getConfigRequest($url){
// 创建一个cURL资源
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Access-Type: application/json",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err){
$this->info($err);
}
curl_close($curl);
$data = json_decode($response,true);
return $data['data'] ?? '';
}
/**
* 登入
* @return mixed|string|void
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private function login($data,$url)
{
try {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err){
$this->info($err);
}
curl_close($curl);
// 处理响应
$loginDataArr = json_decode($response,true);
return $loginDataArr['accessToken'] ?? '';
} catch (\Exception $e) {
$this->info($e->getMessage());
}
}
/**
* 读文件
* @param $key
* @return mixed|string
*/
public static function NacosGet($key){
$path = Nacos::getPath('/.nacos.cache');
$data = json_decode(
file_get_contents($path),
true,
);
if (empty($data)){
return '';
}
return $data[$key] ?? '';
}
/**
* 写入文件
* @param $key
* @param $value
* @return void
*/
public static function NacosPut($key,$value){
$path = Nacos::getPath('/.nacos.cache');
$data = json_decode(
file_get_contents($path),
true,
);
$data[$key] = $value;
file_put_contents(
$path,
json_encode($data),
);
}
/**
* 写日志
* @param $msg
* @return void
*/
private function info($msg)
{
Log::info($msg);
echo $msg."\n";
}
/**
* 获取文件路径
* @param $fileName
* @return string
*/
public static function getPath($fileName){
return base_path().$fileName;
}
/**
* 获取nacos
* @return void
*/
public static function configure()
{
$path = Nacos::getPath('/.env.nacos');
try {
$data = parse_ini_file($path);
} catch (\Exception $ex){
\Log::info($ex->getMessage());
$data = [];
}
Nacos::$configs = $data;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment