扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本篇内容主要讲解“nacos client中ServerListManager的start有什么作用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“nacos client中ServerListManager的start有什么作用”吧!
成都创新互联专业IDC数据服务器托管提供商,专业提供成都服务器托管,服务器租用,BGP机房服务器托管,BGP机房服务器托管,成都多线服务器托管等服务器托管服务。
本文主要研究一下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(ListnewList) { if (null == newList || newList.isEmpty()) { LOGGER.warn("[update-serverlist] current serverlist from address server is empty!!!"); return; } List newServerAddrList = new ArrayList (); 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 (newServerAddrList); iterator = iterator(); currentServerAddr = iterator.next(); EventDispatcher.fireEvent(new ServerlistChangeEvent()); LOGGER.info("[{}] [update-serverlist] serverlist updated to {}", name, serverUrls); } private List 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 lines = IOUtils.readLines(new StringReader(httpResult.content)); List result = new ArrayList (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有什么作用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流