Skip to content

Commit

Permalink
Merge pull request #16 from youzan/hotfix/apollo_2
Browse files Browse the repository at this point in the history
环境变量优先从Apollo读取
  • Loading branch information
xu42 authored Feb 10, 2020
2 parents 89563ce + a0e8dc3 commit a0d437b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
29 changes: 24 additions & 5 deletions src/Controller/ApolloController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}


}
34 changes: 27 additions & 7 deletions src/Util/EnvUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0d437b

Please sign in to comment.