From a0e8dc3b8a78cef31e09ee4d760b1df29f2c6922 Mon Sep 17 00:00:00 2001 From: xu42 Date: Mon, 10 Feb 2020 15:03:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E4=BC=98?= =?UTF-8?q?=E5=85=88=E4=BB=8EApollo=E8=AF=BB=E5=8F=96=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E9=87=8D=E8=AF=95=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/ApolloController.php | 29 +++++++++++++++++++----- src/Util/EnvUtil.php | 34 +++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Controller/ApolloController.php b/src/Controller/ApolloController.php index 428d886..c579b5f 100644 --- a/src/Controller/ApolloController.php +++ b/src/Controller/ApolloController.php @@ -7,6 +7,7 @@ use Symfony\Component\Yaml\Yaml; use YouzanCloudBoot\Component\BaseComponent; use YouzanCloudBoot\Constant\Env; +use YouzanCloudBoot\Facades\LogFacade; use YouzanCloudBoot\Util\ApolloUtil; class ApolloController extends BaseComponent @@ -17,13 +18,31 @@ public function handle(Request $request, Response $response, array $args) /** @var ApolloUtil $apollo */ $apollo = $this->getContainer()->get('apolloUtil'); - $configSystem = $apollo->get('system'); - $configApplication = $apollo->get('application'); - $configAll = array_merge($configApplication, $configSystem); - - file_put_contents(Env::APOLLO_FILE, Yaml::dump($configAll)); + $this->writeToFile($apollo); return $response->withJson(['status' => 'OK']); } + private function writeToFile(ApolloUtil $apollo, $reties = 3) + { + if ($reties < 0) { + LogFacade::warn("Apollo writeToFile. exceeds the maximum retries"); + return; + } + + $configAll = array_merge($apollo->get('system'), $apollo->get('application')); + if (empty($configAll)) { + LogFacade::warn("Apollo writeToFile. the configAll empty"); + return $this->writeToFile($apollo, --$reties); + } + + // write to file + $res = file_put_contents(Env::APOLLO_FILE, Yaml::dump($configAll)); + if (false === $res) { + LogFacade::warn("Apollo writeToFile. write return false"); + return $this->writeToFile($apollo, --$reties); + } + } + + } \ No newline at end of file diff --git a/src/Util/EnvUtil.php b/src/Util/EnvUtil.php index fa106f6..1f281ea 100644 --- a/src/Util/EnvUtil.php +++ b/src/Util/EnvUtil.php @@ -20,21 +20,41 @@ class EnvUtil extends BaseComponent */ public function get(string $varName): ?string { - $key = str_replace('.', '_', $varName); - if (isset($_SERVER[$key])) { - return $_SERVER[$key]; + // 1. 优先从Apollo读取 + $val = $this->getFromApollo($varName); + if (!empty($val)) { + return $val; + } + + // 2. Apollo取不到则从Env取 + return $this->getFromEnv($varName); + } + + private function getFromApollo(string $varName): ?string + { + if (empty($this->apolloConfig) && file_exists(Env::APOLLO_FILE)) { + try { + $this->apolloConfig = Yaml::parseFile(Env::APOLLO_FILE); + } catch (\Exception $e) { + + } + } + + if (is_array($this->apolloConfig) && isset($this->apolloConfig[$varName])) { + return $this->apolloConfig[$varName]; } return null; } - public function getFromApollo(string $varName): ?string + private function getFromEnv(string $varName): ?string { - if (empty($this->apolloConfig)) { - $this->apolloConfig = Yaml::parseFile(Env::APOLLO_FILE); + $key = str_replace('.', '_', $varName); + if (isset($_SERVER[$key])) { + return $_SERVER[$key]; } - return $this->apolloConfig[$varName] ?? null; + return null; } public function getAppName(): ?string