Selaa lähdekoodia

Merge branch 'develop' of http://git.caihongxingqiu.net/rainbow/config-service into develop

caihongxingqiu 6 vuotta sitten
vanhempi
commit
6a974ff121

+ 5 - 0
.env.example

@@ -19,3 +19,8 @@ CACHE_DRIVER=file
 QUEUE_CONNECTION=sync
 
 JWT_SECRET=chxqyhnjuikm67wshaed8ij3hyf2ndh3
+
+APP_ID=chxq-platform
+CLUSTER=default
+APOLLO_NAMESPACES="application,config-service"
+APOLLO_CONFIG_SERVER=http://127.0.0.1:18080

+ 2 - 1
.gitignore

@@ -4,4 +4,5 @@ Homestead.json
 Homestead.yaml
 .env
 /storage/*
-composer.lock
+composer.lock
+/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
     ];
 
     /**

+ 14 - 0
app/Helper/helper.php

@@ -3,6 +3,20 @@
  * 添加自定义辅助函数
  */
 
+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);
+    }
+}
+
 /**
  * 参数签名校验
  * @param array $params

+ 1 - 1
app/Http/Controllers/UploadController.php

@@ -30,7 +30,7 @@ class UploadController extends Controller
                 return $this->jsonError('图片上传失败,请重试');
             }
         }else{
-            return $this->jsonError('图片上传失败,请重试');
+            return $this->jsonError('仅支持图片上传,请重试');
         }
     }
 }

+ 1 - 0
bootstrap/app.php

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

+ 1 - 0
composer.json

@@ -14,6 +14,7 @@
         "laravel/lumen-framework": "5.8.*",
         "league/fractal": "^0.17.0",
         "tymon/jwt-auth": "1.0.0-rc.4.1",
+        "multilinguals/apollo-client": "^0.1.2",
         "vlucas/phpdotenv": "^3.3"
     },
     "require-dev": {

+ 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
+];

+ 0 - 5
config/customer.php

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

+ 7 - 0
config/customer.tpl

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

+ 1 - 1
config/jwt.php

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