Unknown 6 yıl önce
ebeveyn
işleme
eb37379adb

+ 6 - 1
.env.example

@@ -20,4 +20,9 @@ QUEUE_CONNECTION=sync
 
 JWT_SECRET=chxqyhnjuikm67wshaed8ij3hyf2ndh3
 
-PER_PAGE=20
+PER_PAGE=20
+
+APP_ID=chxq-platform-manage
+CLUSTER=default
+APOLLO_NAMESPACES="application,cms-manage"
+APOLLO_CONFIG_SERVER=http://127.0.0.1:18080

+ 1 - 0
.gitignore

@@ -5,3 +5,4 @@ Homestead.yaml
 .env
 composer.lock
 /storage/*
+/config/customer.php

+ 85 - 0
app/Console/Commands/apollo.php

@@ -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);
+    }
+}

+ 2 - 1
app/Console/Kernel.php

@@ -2,6 +2,7 @@
 
 namespace App\Console;
 
+use App\Console\Commands\Apollo;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
 
@@ -13,7 +14,7 @@ class Kernel extends ConsoleKernel
      * @var array
      */
     protected $commands = [
-        //
+        Apollo::class
     ];
 
     /**

+ 18 - 0
app/Helper/helper.php

@@ -0,0 +1,18 @@
+<?php
+/**
+ * 添加自定义辅助函数
+ */
+
+if ( ! function_exists('config_path'))
+{
+    /**
+     * Get the configuration path.
+     *
+     * @param string $path
+     * @return string
+     */
+    function config_path($path = '')
+    {
+        return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
+    }
+}

+ 1 - 0
bootstrap/app.php

@@ -25,6 +25,7 @@ $app->withFacades();
 
 $app->withEloquent();
 $app->configure('api');
+$app->configure('apollo');
 $app->configure('auth');
 $app->configure('jwt');
 

+ 5 - 1
composer.json

@@ -12,6 +12,7 @@
         "hhxsv5/laravel-s": "~3.4.0",
         "laravel/lumen-framework": "5.8.*",
         "tymon/jwt-auth": "1.0.0-rc.4.1",
+        "multilinguals/apollo-client": "^0.1.2",
         "vlucas/phpdotenv": "^3.3"
     },
     "require-dev": {
@@ -26,7 +27,10 @@
         ],
         "psr-4": {
             "App\\": "app/"
-        }
+        },
+        "files": [
+            "app/Helper/helper.php"
+        ]
     },
     "autoload-dev": {
         "classmap": [

+ 10 - 0
config/apollo.php

@@ -0,0 +1,10 @@
+<?php
+
+return [
+    'namespaces' => explode(',', env('APOLLO_NAMESPACES')),
+    'cluster' => env('APOLLO_CLUSTER'),
+    'save_dir' => storage_path('apollo'),
+    'config_server' => env('APOLLO_CONFIG_SERVER'),
+    'app_id' => env('APP_ID'),
+    'timeout_interval' => 70
+];

+ 6 - 0
config/customer.tpl

@@ -0,0 +1,6 @@
+<?php
+//此文件为apollo配置文件模板,禁止修改移动
+return [
+    'jwt_secret' => '{jwt_secret}',
+    'manage_service_url' => '{manage_service_url}',
+];

+ 1 - 1
config/jwt.php

@@ -25,7 +25,7 @@ return [
     |
     */
 
-    'secret' => env('JWT_SECRET'),
+    'secret' => env('JWT_SECRET',config('customer.jwt_secret')),
 
     /*
     |--------------------------------------------------------------------------