您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關nacos中ServerStatusManager的作用是什么,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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,它實現了Runnable接口,其run方法執行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這幾種狀態
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會根據status及httpMethod進行讀寫路由,路由的不到的返回HttpServletResponse.SC_SERVICE_UNAVAILABLE
ServerStatusManager的init方法注冊了ServerStatusUpdater,它實現了Runnable接口,其run方法執行refreshServerStatus;refreshServerStatus會判斷consistencyService是否是available,如果是更新serverStatus為ServerStatus.UP,否則為ServerStatus.DOWN
以上就是nacos中ServerStatusManager的作用是什么,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。