forked from alibaba/nacos
/
ServerListManager.java
589 lines (487 loc) · 20.5 KB
/
ServerListManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.client.config.impl;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.utils.ContextPathUtil;
import com.alibaba.nacos.client.utils.EnvUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.InternetAddressUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import org.slf4j.Logger;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTPS_PREFIX;
import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTP_PREFIX;
/**
* Serverlist Manager.
*
* @author Nacos
*/
public class ServerListManager implements Closeable {
private static final Logger LOGGER = LogUtils.logger(ServerListManager.class);
private final NacosRestTemplate nacosRestTemplate = ConfigHttpClientManager.getInstance().getNacosRestTemplate();
private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.ServerListManager");
t.setDaemon(true);
return t;
});
/**
* The name of the different environment.
*/
private final String name;
private String namespace = "";
private String tenant = "";
public static final String DEFAULT_NAME = "default";
public static final String CUSTOM_NAME = "custom";
public static final String FIXED_NAME = "fixed";
private final int initServerlistRetryTimes = 5;
/**
* Connection timeout and socket timeout with other servers.
*/
static final int TIMEOUT = 5000;
final boolean isFixed;
boolean isStarted = false;
private String endpoint;
private int endpointPort = 8080;
private String contentPath = ParamUtil.getDefaultContextPath();
private String serverListName = ParamUtil.getDefaultNodesPath();
volatile List<String> serverUrls = new ArrayList<>();
private volatile String currentServerAddr;
private Iterator<String> iterator;
public String serverPort = ParamUtil.getDefaultServerPort();
public String addressServerUrl;
private String serverAddrsStr;
public ServerListManager() {
this.isFixed = false;
this.isStarted = false;
this.name = DEFAULT_NAME;
}
public ServerListManager(List<String> fixed) {
this(fixed, null);
}
public ServerListManager(List<String> fixed, String namespace) {
this.isFixed = true;
this.isStarted = true;
List<String> serverAddrs = new ArrayList<>();
for (String serverAddr : fixed) {
String[] serverAddrArr = InternetAddressUtil.splitIPPortStr(serverAddr);
if (serverAddrArr.length == 1) {
serverAddrs
.add(serverAddrArr[0] + InternetAddressUtil.IP_PORT_SPLITER + ParamUtil.getDefaultServerPort());
} else {
serverAddrs.add(serverAddr);
}
}
this.serverUrls = new ArrayList<>(serverAddrs);
if (StringUtils.isNotBlank(namespace)) {
this.namespace = namespace;
this.tenant = namespace;
}
this.name = initServerName(null);
}
public ServerListManager(String host, int port) {
this.isFixed = false;
this.isStarted = false;
this.endpoint = host;
this.endpointPort = port;
this.name = initServerName(null);
initAddressServerUrl(null);
}
public ServerListManager(String endpoint) throws NacosException {
this(endpoint, null);
}
public ServerListManager(String endpoint, String namespace) throws NacosException {
this.isFixed = false;
this.isStarted = false;
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint);
this.endpoint = initEndpoint(properties);
if (StringUtils.isBlank(endpoint)) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank");
}
if (StringUtils.isNotBlank(namespace)) {
this.namespace = namespace;
this.tenant = namespace;
}
this.name = initServerName(null);
initAddressServerUrl(properties);
}
public ServerListManager(Properties properties) throws NacosException {
this.isStarted = false;
this.serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
initParam(properties);
if (StringUtils.isNotBlank(namespace)) {
this.namespace = namespace;
this.tenant = namespace;
}
if (StringUtils.isNotEmpty(serverAddrsStr)) {
this.isFixed = true;
List<String> serverAddrs = new ArrayList<>();
StringTokenizer serverAddrsTokens = new StringTokenizer(this.serverAddrsStr, ",;");
while (serverAddrsTokens.hasMoreTokens()) {
String serverAddr = serverAddrsTokens.nextToken().trim();
if (serverAddr.startsWith(HTTP_PREFIX) || serverAddr.startsWith(HTTPS_PREFIX)) {
serverAddrs.add(serverAddr);
} else {
String[] serverAddrArr = InternetAddressUtil.splitIPPortStr(serverAddr);
if (serverAddrArr.length == 1) {
serverAddrs.add(HTTP_PREFIX + serverAddrArr[0] + InternetAddressUtil.IP_PORT_SPLITER + ParamUtil
.getDefaultServerPort());
} else {
serverAddrs.add(HTTP_PREFIX + serverAddr);
}
}
}
this.serverUrls = serverAddrs;
this.name = initServerName(properties);
} else {
if (StringUtils.isBlank(endpoint)) {
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank");
}
this.isFixed = false;
this.name = initServerName(properties);
initAddressServerUrl(properties);
}
}
private String initServerName(Properties properties) {
String serverName = "";
//1.user define server name.
if (properties != null && properties.containsKey(PropertyKeyConst.SERVER_NAME)) {
serverName = properties.get(PropertyKeyConst.SERVER_NAME).toString();
} else {
// if fix url,use fix url join string.
if (isFixed) {
serverName = FIXED_NAME + "-" + (StringUtils.isNotBlank(namespace) ? (StringUtils.trim(namespace) + "-")
: "") + getFixedNameSuffix(serverUrls.toArray(new String[serverUrls.size()]));
} else {
//if use endpoint , use endpoint ,content path ,serverlist name
serverName = CUSTOM_NAME + "-" + String
.join("_", endpoint, String.valueOf(endpointPort), contentPath, serverListName) + (
StringUtils.isNotBlank(namespace) ? ("_" + StringUtils.trim(namespace)) : "");
}
}
serverName.replaceAll("\\/", "_");
serverName.replaceAll("\\:", "_");
return serverName;
}
private void initAddressServerUrl(Properties properties) {
if (isFixed) {
return;
}
StringBuilder addressServerUrlTem = new StringBuilder(
String.format("http://%s:%d%s/%s", this.endpoint, this.endpointPort,
ContextPathUtil.normalizeContextPath(this.contentPath), this.serverListName));
boolean hasQueryString = false;
if (StringUtils.isNotBlank(namespace)) {
addressServerUrlTem.append("?namespace=").append(namespace);
hasQueryString = true;
}
if (properties != null && properties.containsKey(PropertyKeyConst.ENDPOINT_QUERY_PARAMS)) {
addressServerUrlTem
.append(hasQueryString ? "&" : "?" + properties.get(PropertyKeyConst.ENDPOINT_QUERY_PARAMS));
}
this.addressServerUrl = addressServerUrlTem.toString();
LOGGER.info("serverName = {}, address server url = {}", this.name, this.addressServerUrl);
}
private void initParam(Properties properties) {
this.endpoint = initEndpoint(properties);
String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH);
if (!StringUtils.isBlank(contentPathTmp)) {
this.contentPath = contentPathTmp;
}
String serverListNameTmp = properties.getProperty(PropertyKeyConst.CLUSTER_NAME);
if (!StringUtils.isBlank(serverListNameTmp)) {
this.serverListName = serverListNameTmp;
}
}
private String initEndpoint(final Properties properties) {
String endpointPortTmp = TemplateUtils
.stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT),
() -> properties.getProperty(PropertyKeyConst.ENDPOINT_PORT));
if (StringUtils.isNotBlank(endpointPortTmp)) {
this.endpointPort = Integer.parseInt(endpointPortTmp);
}
String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT);
// Whether to enable domain name resolution rules
String isUseEndpointRuleParsing = properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
System.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
String.valueOf(ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE)));
if (Boolean.parseBoolean(isUseEndpointRuleParsing)) {
String endpointUrl = ParamUtil.parsingEndpointRule(endpointTmp);
if (StringUtils.isNotBlank(endpointUrl)) {
this.serverAddrsStr = "";
}
return endpointUrl;
}
return StringUtils.isNotBlank(endpointTmp) ? endpointTmp : "";
}
/**
* Start.
*
* @throws NacosException nacos exception
*/
public synchronized void start() throws NacosException {
if (isStarted || isFixed) {
return;
}
GetServerListTask getServersTask = new GetServerListTask(addressServerUrl);
for (int i = 0; i < initServerlistRetryTimes && serverUrls.isEmpty(); ++i) {
getServersTask.run();
try {
this.wait((i + 1) * 100L);
} catch (Exception e) {
LOGGER.warn("get serverlist fail,url: {}", addressServerUrl);
}
}
if (serverUrls.isEmpty()) {
LOGGER.error("[init-serverlist] fail to get NACOS-server serverlist! env: {}, url: {}", name,
addressServerUrl);
throw new NacosException(NacosException.SERVER_ERROR,
"fail to get NACOS-server serverlist! env:" + name + ", not connnect url:" + addressServerUrl);
}
// executor schedules the timer task
this.executorService.scheduleWithFixedDelay(getServersTask, 0L, 30L, TimeUnit.SECONDS);
isStarted = true;
}
public List<String> getServerUrls() {
return serverUrls;
}
Iterator<String> iterator() {
if (serverUrls.isEmpty()) {
LOGGER.error("[{}] [iterator-serverlist] No server address defined!", name);
}
return new ServerAddressIterator(serverUrls);
}
@Override
public void shutdown() throws NacosException {
String className = this.getClass().getName();
LOGGER.info("{} do shutdown begin", className);
ThreadUtils.shutdownThreadPool(executorService, LOGGER);
LOGGER.info("{} do shutdown stop", className);
}
class GetServerListTask implements Runnable {
final String url;
GetServerListTask(String url) {
this.url = url;
}
@Override
public void run() {
/*
get serverlist from nameserver
*/
try {
updateIfChanged(getApacheServerList(url, name));
} catch (Exception e) {
LOGGER.error("[" + name + "][update-serverlist] failed to update serverlist from address server!", e);
}
}
}
private void updateIfChanged(List<String> newList) {
if (null == newList || newList.isEmpty()) {
LOGGER.warn("[update-serverlist] current serverlist from address server is empty!!!");
return;
}
List<String> newServerAddrList = new ArrayList<>();
for (String server : newList) {
if (server.startsWith(HTTP_PREFIX) || server.startsWith(HTTPS_PREFIX)) {
newServerAddrList.add(server);
} else {
newServerAddrList.add(HTTP_PREFIX + server);
}
}
/*
no change
*/
if (newServerAddrList.equals(serverUrls)) {
return;
}
serverUrls = new ArrayList<>(newServerAddrList);
iterator = iterator();
currentServerAddr = iterator.next();
// Using unified event processor, NotifyCenter
NotifyCenter.publishEvent(new ServerlistChangeEvent());
LOGGER.info("[{}] [update-serverlist] serverlist updated to {}", name, serverUrls);
}
private List<String> getApacheServerList(String url, String name) {
try {
HttpRestResult<String> httpResult = nacosRestTemplate.get(url, Header.EMPTY, Query.EMPTY, String.class);
if (httpResult.ok()) {
if (DEFAULT_NAME.equals(name)) {
EnvUtil.setSelfEnv(httpResult.getHeader().getOriginalResponseHeader());
}
List<String> lines = IoUtils.readLines(new StringReader(httpResult.getData()));
List<String> result = new ArrayList<>(lines.size());
for (String serverAddr : lines) {
if (StringUtils.isNotBlank(serverAddr)) {
String[] ipPort = InternetAddressUtil.splitIPPortStr(serverAddr.trim());
String ip = ipPort[0].trim();
if (ipPort.length == 1) {
result.add(ip + InternetAddressUtil.IP_PORT_SPLITER + ParamUtil.getDefaultServerPort());
} else {
result.add(serverAddr);
}
}
}
return result;
} else {
LOGGER.error("[check-serverlist] error. addressServerUrl: {}, code: {}", addressServerUrl,
httpResult.getCode());
return null;
}
} catch (Exception e) {
LOGGER.error("[check-serverlist] exception. url: " + url, e);
return null;
}
}
String getUrlString() {
return serverUrls.toString();
}
String getFixedNameSuffix(String... serverIps) {
StringBuilder sb = new StringBuilder();
String split = "";
for (String serverIp : serverIps) {
sb.append(split);
serverIp = serverIp.replaceAll("http(s)?://", "");
sb.append(serverIp.replaceAll(":", "_"));
split = "-";
}
return sb.toString();
}
@Override
public String toString() {
return "ServerManager-" + name + "-" + getUrlString();
}
public boolean contain(String ip) {
return serverUrls.contains(ip);
}
public void refreshCurrentServerAddr() {
iterator = iterator();
currentServerAddr = iterator.next();
}
public String getNextServerAddr() {
if (iterator == null || !iterator.hasNext()) {
refreshCurrentServerAddr();
return currentServerAddr;
}
try {
return iterator.next();
} catch (Exception e) {
//No nothing.
}
refreshCurrentServerAddr();
return currentServerAddr;
}
public String getCurrentServerAddr() {
if (StringUtils.isBlank(currentServerAddr)) {
iterator = iterator();
currentServerAddr = iterator.next();
}
return currentServerAddr;
}
public void updateCurrentServerAddr(String currentServerAddr) {
this.currentServerAddr = currentServerAddr;
}
public Iterator<String> getIterator() {
return iterator;
}
public String getContentPath() {
return contentPath;
}
public String getName() {
return name;
}
public String getNamespace() {
return namespace;
}
public String getTenant() {
return tenant;
}
/**
* Sort the address list, with the same room priority.
*/
private static class ServerAddressIterator implements Iterator<String> {
static class RandomizedServerAddress implements Comparable<RandomizedServerAddress> {
static Random random = new Random();
String serverIp;
int priority = 0;
int seed;
public RandomizedServerAddress(String ip) {
try {
this.serverIp = ip;
/*
change random scope from 32 to Integer.MAX_VALUE to fix load balance issue
*/
this.seed = random.nextInt(Integer.MAX_VALUE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int compareTo(RandomizedServerAddress other) {
if (this.priority != other.priority) {
return other.priority - this.priority;
} else {
return other.seed - this.seed;
}
}
}
public ServerAddressIterator(List<String> source) {
sorted = new ArrayList<>();
for (String address : source) {
sorted.add(new RandomizedServerAddress(address));
}
Collections.sort(sorted);
iter = sorted.iterator();
}
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public String next() {
return iter.next().serverIp;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
final List<RandomizedServerAddress> sorted;
final Iterator<RandomizedServerAddress> iter;
}
}