1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Components\Apollo; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Components\HttpClient\SimpleHttpTrait; |
6
|
|
|
use Hhxsv5\LaravelS\Swoole\Coroutine\Context; |
7
|
|
|
use Swoole\Coroutine; |
8
|
|
|
|
9
|
|
|
class Apollo |
10
|
|
|
{ |
11
|
|
|
use SimpleHttpTrait; |
|
|
|
|
12
|
|
|
|
13
|
|
|
protected $server; |
14
|
|
|
protected $appId; |
15
|
|
|
protected $cluster = 'default'; |
16
|
|
|
protected $namespaces = ['application']; |
17
|
|
|
protected $clientIp; |
18
|
|
|
protected $pullTimeout = 5; |
19
|
|
|
protected $keepOldEnv = false; |
20
|
|
|
|
21
|
|
|
protected $releaseKeys = []; |
22
|
|
|
protected $notifications = []; |
23
|
|
|
|
24
|
|
|
protected $watching = true; |
25
|
|
|
|
26
|
|
|
public function __construct(array $settings) |
27
|
|
|
{ |
28
|
|
|
$this->server = $settings['server']; |
29
|
|
|
$this->appId = $settings['app_id']; |
30
|
|
|
if (isset($settings['cluster'])) { |
31
|
|
|
$this->cluster = $settings['cluster']; |
32
|
|
|
} |
33
|
|
|
if (isset($settings['namespaces'])) { |
34
|
|
|
$this->namespaces = $settings['namespaces']; |
35
|
|
|
} |
36
|
|
|
if (isset($settings['client_ip'])) { |
37
|
|
|
$this->clientIp = $settings['client_ip']; |
38
|
|
|
} else { |
39
|
|
|
$this->clientIp = current(swoole_get_local_ip()) ?: null; |
40
|
|
|
} |
41
|
|
|
if (isset($settings['pull_timeout'])) { |
42
|
|
|
$this->pullTimeout = $settings['pull_timeout']; |
43
|
|
|
} |
44
|
|
|
if (isset($settings['keep_old_env'])) { |
45
|
|
|
$this->keepOldEnv = $settings['keep_old_env']; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function createFromEnv() |
50
|
|
|
{ |
51
|
|
|
$envs = getenv(); |
52
|
|
|
if (!isset($envs['APOLLO_SERVER'], $envs['APOLLO_APP_ID'])) { |
53
|
|
|
throw new \InvalidArgumentException('Missing environment variable APOLLO_SERVER & APOLLO_APP_ID'); |
54
|
|
|
} |
55
|
|
|
$options = [ |
56
|
|
|
'server' => $envs['APOLLO_SERVER'], |
57
|
|
|
'app_id' => $envs['APOLLO_APP_ID'], |
58
|
|
|
'cluster' => isset($envs['APOLLO_CLUSTER']) ? $envs['APOLLO_CLUSTER'] : null, |
59
|
|
|
'namespaces' => isset($envs['APOLLO_NAMESPACES']) ? explode(',', $envs['APOLLO_NAMESPACES']) : null, |
60
|
|
|
'client_ip' => isset($envs['APOLLO_CLIENT_IP']) ? $envs['APOLLO_CLIENT_IP'] : null, |
61
|
|
|
'pull_timeout' => isset($envs['APOLLO_PULL_TIMEOUT']) ? $envs['APOLLO_PULL_TIMEOUT'] : null, |
62
|
|
|
'keep_old_env' => isset($envs['APOLLO_KEEP_OLD_ENV']) ? $envs['APOLLO_KEEP_OLD_ENV'] : false, |
63
|
|
|
]; |
64
|
|
|
return new static($options); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
public function pullBatch(array $namespaces, $withReleaseKey = false, array $options = []) |
69
|
|
|
{ |
70
|
|
|
$configs = []; |
71
|
|
|
$uri = sprintf('%s/configs/%s/%s/', $this->server, $this->appId, $this->cluster); |
72
|
|
|
foreach ($namespaces as $namespace) { |
73
|
|
|
$url = $uri . $namespace . '?' . http_build_query([ |
74
|
|
|
'releaseKey' => $withReleaseKey && isset($this->releaseKeys[$namespace]) ? $this->releaseKeys[$namespace] : null, |
75
|
|
|
'ip' => $this->clientIp, |
76
|
|
|
]); |
77
|
|
|
$timeout = isset($options['timeout']) ? $options['timeout'] : $this->pullTimeout; |
78
|
|
|
$response = $this->httpGet($url, compact('timeout')); |
79
|
|
|
if ($response['statusCode'] === 200) { |
80
|
|
|
$configs[$namespace] = json_decode($response['body'], true); |
81
|
|
|
$this->releaseKeys[$namespace] = $configs[$namespace]['releaseKey']; |
82
|
|
|
} elseif ($response['statusCode'] === 304) { |
83
|
|
|
// ignore 304 |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
return $configs; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function pullAll($withReleaseKey = false, array $options = []) |
91
|
|
|
{ |
92
|
|
|
return $this->pullBatch($this->namespaces, $withReleaseKey, $options); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function pullAllAndSave($filepath, array $options = []) |
96
|
|
|
{ |
97
|
|
|
$all = $this->pullAll(false, $options); |
98
|
|
|
if (count($all) !== count($this->namespaces)) { |
99
|
|
|
throw new \RuntimeException('Incomplete Apollo configuration list'); |
100
|
|
|
} |
101
|
|
|
$configs = []; |
102
|
|
|
foreach ($all as $namespace => $config) { |
103
|
|
|
$configs[] = '# Namespace: ' . $config['namespaceName']; |
104
|
|
|
ksort($config['configurations']); |
105
|
|
|
foreach ($config['configurations'] as $key => $value) { |
106
|
|
|
$configs[] = sprintf('%s=%s', $key, $value); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
if (empty($configs)) { |
110
|
|
|
throw new \RuntimeException('Empty Apollo configuration list'); |
111
|
|
|
} |
112
|
|
|
if ($this->keepOldEnv && file_exists($filepath)) { |
113
|
|
|
rename($filepath, $filepath . '.' . date('YmdHis')); |
114
|
|
|
} |
115
|
|
|
$fileContent = implode(PHP_EOL, $configs); |
116
|
|
|
if (Context::inCoroutine()) { |
117
|
|
|
Coroutine::writeFile($filepath, $fileContent); |
|
|
|
|
118
|
|
|
} else { |
119
|
|
|
file_put_contents($filepath, $fileContent); |
120
|
|
|
} |
121
|
|
|
return $configs; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function startWatchNotification(callable $callback, array $options = []) |
125
|
|
|
{ |
126
|
|
|
if (!isset($options['timeout']) || $options['timeout'] < 60) { |
127
|
|
|
$options['timeout'] = 70; |
128
|
|
|
} |
129
|
|
|
$this->watching = true; |
130
|
|
|
$this->notifications = []; |
131
|
|
|
foreach ($this->namespaces as $namespace) { |
132
|
|
|
$this->notifications[$namespace] = ['namespaceName' => $namespace, 'notificationId' => -1]; |
133
|
|
|
} |
134
|
|
|
while ($this->watching) { |
135
|
|
|
$url = sprintf('%s/notifications/v2?%s', |
136
|
|
|
$this->server, |
137
|
|
|
http_build_query([ |
138
|
|
|
'appId' => $this->appId, |
139
|
|
|
'cluster' => $this->cluster, |
140
|
|
|
'notifications' => json_encode(array_values($this->notifications)), |
141
|
|
|
]) |
142
|
|
|
); |
143
|
|
|
$response = $this->httpGet($url, $options); |
144
|
|
|
|
145
|
|
|
if ($response['statusCode'] === 200) { |
146
|
|
|
$notifications = json_decode($response['body'], true); |
147
|
|
|
if (empty($notifications)) { |
148
|
|
|
continue; |
149
|
|
|
} |
150
|
|
|
$callback($notifications); |
151
|
|
|
array_walk($notifications, function (&$notification) { |
152
|
|
|
unset($notification['messages']); |
153
|
|
|
}); |
154
|
|
|
$this->notifications = array_merge($this->notifications, array_column($notifications, null, 'namespaceName')); |
155
|
|
|
} elseif ($response['statusCode'] === 304) { |
156
|
|
|
// ignore 304 |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function stopWatchNotification() |
162
|
|
|
{ |
163
|
|
|
$this->watching = false; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|