废话不说,直接上代码'http://10.208.204.46:8161','query'=>['app_id'=>'wuc','cluster'=>'default','namespace'=>'application',]];维护进程namespaceApp\Console\Commands\Apollo;useGuzzleHttp\Client;useIlluminate\Console\Command;useIlluminate\Support\Arr;useIlluminate\Support\Facades\Storage;classSyncCommandextendsCommand{/***控制台命令的名称和签名。**@varstring*/protected$signature='ue:apollo:sync';/***控制台命令说明。**@varstring*/protected$description='阿波罗同步';受保护的$config=[];受保护的$url;/***创建一个新的命令实例。**@returnvoid*/publicfunction__construct(){parent::__construct();$this->url=config('apollo.server')。'/配置/'。内爆('/',array_values(配置('apollo.query')));}/***执行控制台命令。**@returnmixed*/publicfunctionhandle(){$this->doSync();}protectedfunctiondoSync(){$client=newClient(['timeout'=>2.00]);尝试{$response=$client->request('GET',$this->url);$body=json_decode($response->getBody()->getContents(),true);$cfg=Arr::get($body,'配置',[]);如果(!$cfg){返回真;}$cfg=array_map(function($value){if($row=json_decode($value,true)){return$row;}return$value;},$cfg);$项目=[];foreach($cfgas$key=>$value){data_set($items,$key,$value);}foreach($itemsas$k=>$item){$this->line('保存['.$k.']');$this->保存($k,$item);}}catch(\Exception$ex){$this->error($ex->getMessage());}}protectedfunctionsave($fileName,$item){if(config('apollo.sync.redis',false)){缓存()->tags('apollo')->forever($fileName,$item);$this->line('保存到Redis'.$fileName);}if(config('apollo.sync.file',false)){$this->line('保存到文件'.$fileName);$fileName='阿波罗/'.$文件名。'.php';ksort($item);$content=implode("\r\n",["put($fileName,$content);}$this->line('==================');}}
