您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)nacos中ServerStatusManager的作用是什么,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerStatusManager.java
@Service public class ServerStatusManager { @Resource(name = "consistencyDelegate") private ConsistencyService consistencyService; @Autowired private SwitchDomain switchDomain; private ServerStatus serverStatus = ServerStatus.STARTING; @PostConstruct public void init() { GlobalExecutor.registerServerStatusUpdater(new ServerStatusUpdater()); } private void refreshServerStatus() { if (StringUtils.isNotBlank(switchDomain.getOverriddenServerStatus())) { serverStatus = ServerStatus.valueOf(switchDomain.getOverriddenServerStatus()); return; } if (consistencyService.isAvailable()) { serverStatus = ServerStatus.UP; } else { serverStatus = ServerStatus.DOWN; } } public ServerStatus getServerStatus() { return serverStatus; } public class ServerStatusUpdater implements Runnable { @Override public void run() { refreshServerStatus(); } } }
ServerStatusManager的init方法注冊了ServerStatusUpdater,它實現(xiàn)了Runnable接口,其run方法執(zhí)行refreshServerStatus;refreshServerStatus會判斷consistencyService是否是available,如果是更新serverStatus為ServerStatus.UP,否則為ServerStatus.DOWN
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerStatus.java
public enum ServerStatus { /** * server is up and ready for request */ UP, /** * server is out of service, something abnormal happened */ DOWN, /** * server is preparing itself for request, usually 'UP' is the next status */ STARTING, /** * server is manually paused */ PAUSED, /** * only write operation is permitted. */ WRITE_ONLY, /** * only read operation is permitted. */ READ_ONLY }
ServerStatus有UP、DOWN、STARTING、PAUSED、WRITE_ONLY、READ_ONLY這幾種狀態(tài)
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java
public class TrafficReviseFilter implements Filter { @Autowired private ServerStatusManager serverStatusManager; @Autowired private SwitchDomain switchDomain; @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; // request limit if exist: String urlString = req.getRequestURI() + "?" + req.getQueryString(); Map<String, Integer> limitedUrlMap = switchDomain.getLimitedUrlMap(); if (limitedUrlMap != null && limitedUrlMap.size() > 0) { for (Map.Entry<String, Integer> entry : limitedUrlMap.entrySet()) { String limitedUrl = entry.getKey(); if (StringUtils.startsWith(urlString, limitedUrl)) { resp.setStatus(entry.getValue()); return; } } } // if server is UP: if (serverStatusManager.getServerStatus() == ServerStatus.UP) { filterChain.doFilter(req, resp); return; } // requests from peer server should be let pass: String agent = req.getHeader("Client-Version"); if (StringUtils.isBlank(agent)) { agent = req.getHeader("User-Agent"); } if (StringUtils.startsWith(agent, UtilsAndCommons.NACOS_SERVER_HEADER)) { filterChain.doFilter(req, resp); return; } // write operation should be let pass in WRITE_ONLY status: if (serverStatusManager.getServerStatus() == ServerStatus.WRITE_ONLY && !HttpMethod.GET.equals(req.getMethod())) { filterChain.doFilter(req, resp); return; } // read operation should be let pass in READ_ONLY status: if (serverStatusManager.getServerStatus() == ServerStatus.READ_ONLY && HttpMethod.GET.equals(req.getMethod())) { filterChain.doFilter(req, resp); return; } resp.getWriter().write("server is " + serverStatusManager.getServerStatus().name() + " now, please try again later!"); resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); } }
TrafficReviseFilter會根據(jù)status及httpMethod進(jìn)行讀寫路由,路由的不到的返回HttpServletResponse.SC_SERVICE_UNAVAILABLE
ServerStatusManager的init方法注冊了ServerStatusUpdater,它實現(xiàn)了Runnable接口,其run方法執(zhí)行refreshServerStatus;refreshServerStatus會判斷consistencyService是否是available,如果是更新serverStatus為ServerStatus.UP,否則為ServerStatus.DOWN
以上就是nacos中ServerStatusManager的作用是什么,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。