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 Client |
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 $backupOldEnv = 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 = (int)$settings['pull_timeout']; |
43
|
|
|
} |
44
|
|
|
if (isset($settings['backup_old_env'])) { |
45
|
|
|
$this->backupOldEnv = (bool)$settings['backup_old_env']; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function createFromEnv() |
50
|
|
|
{ |
51
|
|
|
if (!getenv('APOLLO_SERVER') || !getenv('APOLLO_APP_ID')) { |
52
|
|
|
throw new \InvalidArgumentException('Missing environment variable APOLLO_SERVER & APOLLO_APP_ID'); |
53
|
|
|
} |
54
|
|
|
$options = [ |
55
|
|
|
'server' => getenv('APOLLO_SERVER'), |
56
|
|
|
'app_id' => getenv('APOLLO_APP_ID'), |
57
|
|
|
'cluster' => getenv('APOLLO_CLUSTER') ?: null, |
58
|
|
|
'namespaces' => explode(',', getenv('APOLLO_NAMESPACES')) ?: null, |
59
|
|
|
'client_ip' => getenv('APOLLO_CLIENT_IP') ?: null, |
60
|
|
|
'pull_timeout' => getenv('APOLLO_PULL_TIMEOUT') ?: null, |
61
|
|
|
'backup_old_env' => getenv('APOLLO_BACKUP_OLD_ENV') ?: false, |
62
|
|
|
]; |
63
|
|
|
return new static($options); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
public function pullBatch(array $namespaces, $withReleaseKey = false, array $options = []) |
68
|
|
|
{ |
69
|
|
|
$configs = []; |
70
|
|
|
$uri = sprintf('%s/configs/%s/%s/', $this->server, $this->appId, $this->cluster); |
71
|
|
|
foreach ($namespaces as $namespace) { |
72
|
|
|
$url = $uri . $namespace . '?' . http_build_query([ |
73
|
|
|
'releaseKey' => $withReleaseKey && isset($this->releaseKeys[$namespace]) ? $this->releaseKeys[$namespace] : null, |
74
|
|
|
'ip' => $this->clientIp, |
75
|
|
|
]); |
76
|
|
|
$timeout = isset($options['timeout']) ? $options['timeout'] : $this->pullTimeout; |
77
|
|
|
$response = $this->httpGet($url, compact('timeout')); |
78
|
|
|
if ($response['statusCode'] === 200) { |
79
|
|
|
$configs[$namespace] = json_decode($response['body'], true); |
80
|
|
|
$this->releaseKeys[$namespace] = $configs[$namespace]['releaseKey']; |
81
|
|
|
} elseif ($response['statusCode'] === 304) { |
82
|
|
|
// ignore 304 |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
return $configs; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function pullAll($withReleaseKey = false, array $options = []) |
90
|
|
|
{ |
91
|
|
|
return $this->pullBatch($this->namespaces, $withReleaseKey, $options); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function pullAllAndSave($filepath, array $options = []) |
95
|
|
|
{ |
96
|
|
|
$all = $this->pullAll(false, $options); |
97
|
|
|
if (count($all) !== count($this->namespaces)) { |
98
|
|
|
throw new \RuntimeException('Incomplete Apollo configuration list'); |
99
|
|
|
} |
100
|
|
|
$configs = []; |
101
|
|
|
foreach ($all as $namespace => $config) { |
102
|
|
|
$configs[] = '# Namespace: ' . $config['namespaceName']; |
103
|
|
|
ksort($config['configurations']); |
104
|
|
|
foreach ($config['configurations'] as $key => $value) { |
105
|
|
|
$configs[] = sprintf('%s=%s', $key, $value); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
if (empty($configs)) { |
109
|
|
|
throw new \RuntimeException('Empty Apollo configuration list'); |
110
|
|
|
} |
111
|
|
|
if ($this->backupOldEnv && file_exists($filepath)) { |
112
|
|
|
rename($filepath, $filepath . '.' . date('YmdHis')); |
113
|
|
|
} |
114
|
|
|
$fileContent = implode(PHP_EOL, $configs); |
115
|
|
|
if (Context::inCoroutine()) { |
116
|
|
|
Coroutine::writeFile($filepath, $fileContent); |
|
|
|
|
117
|
|
|
} else { |
118
|
|
|
file_put_contents($filepath, $fileContent); |
119
|
|
|
} |
120
|
|
|
return $configs; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function startWatchNotification(callable $callback, array $options = []) |
124
|
|
|
{ |
125
|
|
|
if (!isset($options['timeout']) || $options['timeout'] < 60) { |
126
|
|
|
$options['timeout'] = 70; |
127
|
|
|
} |
128
|
|
|
$this->watching = true; |
129
|
|
|
$this->notifications = []; |
130
|
|
|
foreach ($this->namespaces as $namespace) { |
131
|
|
|
$this->notifications[$namespace] = ['namespaceName' => $namespace, 'notificationId' => -1]; |
132
|
|
|
} |
133
|
|
|
while ($this->watching) { |
134
|
|
|
$url = sprintf('%s/notifications/v2?%s', |
135
|
|
|
$this->server, |
136
|
|
|
http_build_query([ |
137
|
|
|
'appId' => $this->appId, |
138
|
|
|
'cluster' => $this->cluster, |
139
|
|
|
'notifications' => json_encode(array_values($this->notifications)), |
140
|
|
|
]) |
141
|
|
|
); |
142
|
|
|
$response = $this->httpGet($url, $options); |
143
|
|
|
|
144
|
|
|
if ($response['statusCode'] === 200) { |
145
|
|
|
$notifications = json_decode($response['body'], true); |
146
|
|
|
if (empty($notifications)) { |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
if (current($this->notifications)['notificationId'] !== -1) { // Ignore the first pull |
150
|
|
|
$callback($notifications); |
151
|
|
|
} |
152
|
|
|
array_walk($notifications, function (&$notification) { |
153
|
|
|
unset($notification['messages']); |
154
|
|
|
}); |
155
|
|
|
$this->notifications = array_merge($this->notifications, array_column($notifications, null, 'namespaceName')); |
156
|
|
|
} elseif ($response['statusCode'] === 304) { |
157
|
|
|
// ignore 304 |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function stopWatchNotification() |
163
|
|
|
{ |
164
|
|
|
$this->watching = false; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|