Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of start of ServerListManager in nacos client

Shulou Source: shulou.com Published: 2022-06-02 08:39:45 09月20日 Update

This article mainly explains "what is the function of ServerListManager start in nacos client". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the role of ServerListManager start in nacos client"?

Order

This paper mainly studies the start of nacos client's ServerListManager.

ServerListManager

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;} / /.}

The start method of ServerListManager executes GetServerListTask under the condition of non-isStarted and non-isFixed. The number of failed retries is initServerlistRetryTimes. If serverUrls is empty, NacosException; is thrown. If it is not empty, register the getServersTask and execute once every 30 seconds to refresh server list.

GetServerListTask

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 newList) {if (null = = newList | | newList.isEmpty ()) {LOGGER.warn ("[update-serverlist] current serverlist from address server is emptiness!"); 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, 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 implements the Runnable interface, and its run method obtains the server address list through the getApacheServerList request addressServerUrl, and then executes the updateIfChanged method; this method determines whether the server list has changed, updates the serverUrls if there is a change, and then publishes the ServerlistChangeEvent

Summary

The start method of ServerListManager executes GetServerListTask under the condition of non-isStarted and non-isFixed. The number of failed retries is initServerlistRetryTimes. If serverUrls is empty, NacosException; is thrown. If it is not empty, register the getServersTask and execute once every 30 seconds to refresh server list.

At this point, I believe you have a deeper understanding of "what is the role of ServerListManager start in nacos client". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Methods functions content conditions times learning practical deeper interest address practicality practice summary interface easy to operate more friends websites channels updates Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft macOS Linux OPPO Reno