|
@@ -0,0 +1,85 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace App\Console\Commands;
|
|
|
|
+
|
|
|
|
+use Illuminate\Console\Command;
|
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
|
+use Org\Multilinguals\Apollo\Client\ApolloClient;
|
|
|
|
+
|
|
|
|
+class Apollo extends Command
|
|
|
|
+{
|
|
|
|
+ /**
|
|
|
|
+ * The name and signature of the console command.
|
|
|
|
+ *
|
|
|
|
+ * @var string
|
|
|
|
+ */
|
|
|
|
+ protected $signature = 'apollo';
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * The console command description.
|
|
|
|
+ *
|
|
|
|
+ * @var string
|
|
|
|
+ */
|
|
|
|
+ protected $description = 'Apollo配置服务';
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private $apollo;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Create a new command instance.
|
|
|
|
+ *
|
|
|
|
+ * @return void
|
|
|
|
+ */
|
|
|
|
+ public function __construct()
|
|
|
|
+ {
|
|
|
|
+ parent::__construct();
|
|
|
|
+
|
|
|
|
+ $this->save_dir = storage_path('apollo');
|
|
|
|
+ $this->config_tpl = config_path() . '/customer.tpl';
|
|
|
|
+ $this->config_file = config_path() . '/customer.php';
|
|
|
|
+
|
|
|
|
+ $this->apollo = new ApolloClient(
|
|
|
|
+ config('apollo.config_server'),
|
|
|
|
+ config('apollo.app_id'),
|
|
|
|
+ config('apollo.namespaces')
|
|
|
|
+ );
|
|
|
|
+ $this->apollo->save_dir = $this->save_dir;
|
|
|
|
+ if(!is_dir($this->save_dir)){
|
|
|
|
+ mkdir($this->save_dir);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Execute the console command.
|
|
|
|
+ *
|
|
|
|
+ * @return mixed
|
|
|
|
+ */
|
|
|
|
+ public function handle()
|
|
|
|
+ {
|
|
|
|
+ $this->line('apollo配置服务开启');
|
|
|
|
+ $restart = true; //失败自动重启
|
|
|
|
+ do {
|
|
|
|
+ $error = $this->apollo->start(function () {
|
|
|
|
+ $list = glob($this->save_dir . DIRECTORY_SEPARATOR . 'apolloConfig.*');
|
|
|
|
+ $apollo = [];
|
|
|
|
+ foreach ($list as $l) {
|
|
|
|
+ $config = require $l;
|
|
|
|
+ if (is_array($config) && isset($config['configurations'])) {
|
|
|
|
+ $apollo = array_merge($apollo, $config['configurations']);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!$apollo) {
|
|
|
|
+ Log::error('Load Apollo Config Failed, no config available');
|
|
|
|
+ }
|
|
|
|
+ $tpl = file_get_contents($this->config_tpl);
|
|
|
|
+ foreach ($apollo as $key =>$value){
|
|
|
|
+ $tpl = str_replace('{'.$key.'}',$value,$tpl);
|
|
|
|
+ }
|
|
|
|
+ file_put_contents($this->config_file,$tpl);
|
|
|
|
+ }); //此处传入回调
|
|
|
|
+ if ($error) {
|
|
|
|
+ Log::info("Apollo Hand error :" . $error);
|
|
|
|
+ }
|
|
|
|
+ } while ($error && $restart);
|
|
|
|
+ }
|
|
|
|
+}
|