您好,登录后才能下订单哦!
本篇内容主要讲解“nacos client中ServerListManager的start有什么作用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“nacos client中ServerListManager的start有什么作用”吧!
本文主要研究一下nacos client的ServerListManager的start
nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java
public class ServerListManager { private static final Logger LOGGER = LogUtils.logger(ServerListManager.class); private static final String HTTPS = "https://"; private static final String HTTP = "http://"; //...... 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); } TimerService.scheduleWithFixedDelay(getServersTask, 0L, 30L, TimeUnit.SECONDS); isStarted = true; } //...... }
ServerListManager的start方法在非isStarted且非isFixed的条件下会执行GetServerListTask,失败重试次数为initServerlistRetryTimes,如果serverUrls为空则抛出NacosException;如果不为空则注册该getServersTask每隔30秒执行一次来刷新server list
nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java
public class ServerListManager { //...... 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<String>(); for (String server : newList) { if (server.startsWith(HTTP) || server.startsWith(HTTPS)) { newServerAddrList.add(server); } else { newServerAddrList.add(HTTP + server); } } /** * no change */ if (newServerAddrList.equals(serverUrls)) { return; } serverUrls = new ArrayList<String>(newServerAddrList); iterator = iterator(); currentServerAddr = iterator.next(); EventDispatcher.fireEvent(new ServerlistChangeEvent()); LOGGER.info("[{}] [update-serverlist] serverlist updated to {}", name, serverUrls); } private List<String> getApacheServerList(String url, String name) { try { HttpResult httpResult = HttpSimpleClient.httpGet(url, null, null, null, 3000); if (HttpURLConnection.HTTP_OK == httpResult.code) { if (DEFAULT_NAME.equals(name)) { EnvUtil.setSelfEnv(httpResult.headers); } List<String> lines = IOUtils.readLines(new StringReader(httpResult.content)); List<String> result = new ArrayList<String>(lines.size()); for (String serverAddr : lines) { if (org.apache.commons.lang3.StringUtils.isNotBlank(serverAddr)) { String[] ipPort = serverAddr.trim().split(":"); String ip = ipPort[0].trim(); if (ipPort.length == 1) { result.add(ip + ":" + ParamUtil.getDefaultServerPort()); } else { result.add(serverAddr); } } } return result; } else { LOGGER.error("[check-serverlist] error. addressServerUrl: {}, code: {}", addressServerUrl, httpResult.code); return null; } } catch (IOException e) { LOGGER.error("[check-serverlist] exception. url: " + url, e); return null; } } //...... }
GetServerListTask实现了Runnable接口,其run方法会通过getApacheServerList请求addressServerUrl获取服务端地址列表,然后执行updateIfChanged方法;该方法会判断server list是否有变更,有变更则更新serverUrls,然后发布ServerlistChangeEvent
ServerListManager的start方法在非isStarted且非isFixed的条件下会执行GetServerListTask,失败重试次数为initServerlistRetryTimes,如果serverUrls为空则抛出NacosException;如果不为空则注册该getServersTask每隔30秒执行一次来刷新server list
到此,相信大家对“nacos client中ServerListManager的start有什么作用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。