Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of start of ServerListManager in nacos client

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report