1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Components\Eureka; |
4
|
|
|
|
5
|
|
|
class Client |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
protected $host; |
8
|
|
|
|
9
|
|
|
protected $port; |
10
|
|
|
|
11
|
|
|
protected $context; |
12
|
|
|
|
13
|
|
|
public function __construct($host, $port, $context = 'eureka/v2 ') |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$this->host = $host; |
16
|
|
|
$this->port = $port; |
17
|
|
|
$this->context = $context; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function getEurekaUri() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
return $this->host . ':' . $this->port . '/' . $this->context; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Register app in eureka. |
27
|
|
|
* |
28
|
|
|
* @param string $appId |
|
|
|
|
29
|
|
|
* @param array $data |
|
|
|
|
30
|
|
|
* @return |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function register($appId, array $data) |
33
|
|
|
{ |
34
|
|
|
return $this->client->request('POST', $this->getEurekaUri() . '/apps/' . $appId, [ |
|
|
|
|
35
|
|
|
'json' => [ |
36
|
|
|
'instance' => $data, |
37
|
|
|
], |
38
|
|
|
]); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* De-register app from eureka. |
43
|
|
|
* |
44
|
|
|
* @param string $appId |
|
|
|
|
45
|
|
|
* @param string $instanceId |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @return ResponseInterface |
|
|
|
|
48
|
|
|
* @throws GuzzleException |
49
|
|
|
* |
50
|
|
|
*/ |
|
|
|
|
51
|
|
|
public function deRegister($appId, $instanceId) |
52
|
|
|
{ |
53
|
|
|
return $this->client->request('DELETE', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Send app heartbeat. |
58
|
|
|
* |
59
|
|
|
* @param string $appId |
|
|
|
|
60
|
|
|
* @param string $instanceId |
|
|
|
|
61
|
|
|
* |
62
|
|
|
* @return ResponseInterface |
63
|
|
|
* @throws GuzzleException |
64
|
|
|
* |
65
|
|
|
*/ |
|
|
|
|
66
|
|
|
public function heartBeat($appId, $instanceId) |
67
|
|
|
{ |
68
|
|
|
return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get all registered applications. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
* @throws GuzzleException |
76
|
|
|
* |
77
|
|
|
*/ |
|
|
|
|
78
|
|
|
public function getAllApps() |
79
|
|
|
{ |
80
|
|
|
$response = $this->client->request('GET', $this->getEurekaUri() . '/apps', [ |
|
|
|
|
81
|
|
|
'headers' => [ |
82
|
|
|
'Accept' => 'application/json', |
83
|
|
|
], |
84
|
|
|
]); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return \GuzzleHttp\json_decode($response->getBody(), true); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get application. |
91
|
|
|
* |
92
|
|
|
* @param string $appId |
|
|
|
|
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
* @throws GuzzleException |
96
|
|
|
* |
97
|
|
|
*/ |
|
|
|
|
98
|
|
|
public function getApp($appId) |
99
|
|
|
{ |
100
|
|
|
$response = $this->client->request('GET', $this->getEurekaUri() . '/apps/' . $appId, [ |
|
|
|
|
101
|
|
|
'headers' => [ |
102
|
|
|
'Accept' => 'application/json', |
103
|
|
|
], |
104
|
|
|
]); |
|
|
|
|
105
|
|
|
|
106
|
|
|
return \GuzzleHttp\json_decode($response->getBody(), true); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get application Instance. |
111
|
|
|
* |
112
|
|
|
* @param string $appId |
|
|
|
|
113
|
|
|
* @param string $instanceId |
|
|
|
|
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
* @throws GuzzleException |
117
|
|
|
* |
118
|
|
|
*/ |
|
|
|
|
119
|
|
|
public function getAppInstance($appId, $instanceId) |
120
|
|
|
{ |
121
|
|
|
$response = $this->client->request('GET', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId, [ |
|
|
|
|
122
|
|
|
'headers' => [ |
123
|
|
|
'Accept' => 'application/json', |
124
|
|
|
], |
125
|
|
|
]); |
|
|
|
|
126
|
|
|
|
127
|
|
|
return \GuzzleHttp\json_decode($response->getBody(), true); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get Instance. |
132
|
|
|
* |
133
|
|
|
* @param string $instanceId |
|
|
|
|
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
* @throws GuzzleException |
137
|
|
|
* |
138
|
|
|
*/ |
|
|
|
|
139
|
|
|
public function getInstance($instanceId) |
140
|
|
|
{ |
141
|
|
|
$response = $this->client->request('GET', $this->getEurekaUri() . '/instances/' . $instanceId, [ |
|
|
|
|
142
|
|
|
'headers' => [ |
143
|
|
|
'Accept' => 'application/json', |
144
|
|
|
], |
145
|
|
|
]); |
|
|
|
|
146
|
|
|
|
147
|
|
|
return json_decode($response->getBody(), true); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Take Instance out of the service. |
152
|
|
|
* |
153
|
|
|
* @param string $appId |
|
|
|
|
154
|
|
|
* @param string $instanceId |
|
|
|
|
155
|
|
|
* |
156
|
|
|
* |
157
|
|
|
*/ |
|
|
|
|
158
|
|
|
public function takeInstanceOut($appId, $instanceId) |
159
|
|
|
{ |
160
|
|
|
return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/status', [ |
|
|
|
|
161
|
|
|
'query' => [ |
162
|
|
|
'value' => 'OUT_OF_SERVICE', |
163
|
|
|
], |
164
|
|
|
]); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Put Instance back into the service. |
169
|
|
|
* |
170
|
|
|
* @param string $appId |
|
|
|
|
171
|
|
|
* @param string $instanceId |
|
|
|
|
172
|
|
|
* |
173
|
|
|
* @return ResponseInterface |
174
|
|
|
* @throws GuzzleException |
175
|
|
|
* |
176
|
|
|
*/ |
|
|
|
|
177
|
|
|
public function putInstanceBack($appId, $instanceId) |
178
|
|
|
{ |
179
|
|
|
return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/status', [ |
|
|
|
|
180
|
|
|
'query' => [ |
181
|
|
|
'value' => 'UP', |
182
|
|
|
], |
183
|
|
|
]); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Update app Instance metadata. |
188
|
|
|
* |
189
|
|
|
* @param string $appId |
|
|
|
|
190
|
|
|
* @param string $instanceId |
|
|
|
|
191
|
|
|
* @param array $metadata |
|
|
|
|
192
|
|
|
* |
193
|
|
|
* @return ResponseInterface |
194
|
|
|
* @throws GuzzleException |
195
|
|
|
* |
196
|
|
|
*/ |
|
|
|
|
197
|
|
|
public function updateAppInstanceMetadata($appId, $instanceId, array $metadata) |
198
|
|
|
{ |
199
|
|
|
return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/metadata', [ |
|
|
|
|
200
|
|
|
'query' => $metadata, |
201
|
|
|
]); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get all instances by a vip address. |
206
|
|
|
* |
207
|
|
|
* @param string $vipAddress |
|
|
|
|
208
|
|
|
* |
209
|
|
|
*/ |
|
|
|
|
210
|
|
|
public function getInstancesByVipAddress($vipAddress) |
211
|
|
|
{ |
212
|
|
|
$response = $this->client->request('GET', $this->getEurekaUri() . '/vips/' . $vipAddress, [ |
|
|
|
|
213
|
|
|
'headers' => [ |
214
|
|
|
'Accept' => 'application/json', |
215
|
|
|
], |
216
|
|
|
]); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get all instances by a secure vip address. |
221
|
|
|
* |
222
|
|
|
* @param string $secureVipAddress |
|
|
|
|
223
|
|
|
* |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function getInstancesBySecureVipAddress($secureVipAddress) |
227
|
|
|
{ |
228
|
|
|
$response = $this->client->request('GET', $this->getEurekaUri() . '/svips/' . $secureVipAddress, [ |
|
|
|
|
229
|
|
|
'headers' => [ |
230
|
|
|
'Accept' => 'application/json', |
231
|
|
|
], |
232
|
|
|
]); |
|
|
|
|
233
|
|
|
|
234
|
|
|
return json_decode($response->getBody(), true); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function up(string $appId, string $instanceId) |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/status', [ |
|
|
|
|
240
|
|
|
'query' => [ |
241
|
|
|
'value' => 'UP', |
242
|
|
|
], |
243
|
|
|
]); |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function down($appId, $instanceId) |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
return $this->client->request('PUT', $this->getEurekaUri() . '/apps/' . $appId . '/' . $instanceId . '/status', [ |
|
|
|
|
249
|
|
|
'query' => [ |
250
|
|
|
'value' => 'DOWN', |
251
|
|
|
], |
252
|
|
|
]); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the alive instances |
257
|
|
|
* @param string $appId |
|
|
|
|
258
|
|
|
* @return array |
|
|
|
|
259
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
|
|
|
260
|
|
|
*/ |
261
|
|
|
public function getUpInstances($appId) |
262
|
|
|
{ |
263
|
|
|
$apps = $this->getApp($appId); |
264
|
|
|
return array_filter($apps['application']['instance'], function ($instance) { |
|
|
|
|
265
|
|
|
return $instance['status'] === 'UP'; |
266
|
|
|
}); |
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Get the url of instance. |
271
|
|
|
* @param array $instance |
|
|
|
|
272
|
|
|
* @return string |
|
|
|
|
273
|
|
|
*/ |
274
|
|
|
public function getInstanceUrl(array $instance) |
275
|
|
|
{ |
276
|
|
|
if ($instance['securePort']['@enabled'] === 'true') { |
277
|
|
|
$url = sprintf('%s://%s:%d', 'https', $instance['ipAddr'], $instance['securePort']['$']); |
278
|
|
|
} elseif ($instance['port']['@enabled'] === 'true') { |
279
|
|
|
$url = sprintf('%s://%s:%d', 'http', $instance['ipAddr'], $instance['port']['$']); |
280
|
|
|
} else { |
281
|
|
|
$parts = parse_url($instance['homePageUrl']); |
282
|
|
|
if (!isset($parts['host'])) { |
283
|
|
|
throw new \RuntimeException('Invalid homePageUrl: ' . $instance['homePageUrl']); |
284
|
|
|
} |
285
|
|
|
$url = sprintf('%s://%s:%d', isset($parts['scheme']) ? $parts['scheme'] : 'http', $parts['host'], isset($parts['port']) ? $parts['port'] : 80); |
286
|
|
|
} |
287
|
|
|
return $url; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|