本篇文章给大家分享的是有关nacos中ServerStatusManager的作用是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
10年的普兰网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整普兰建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“普兰网站设计”,“普兰网站推广”以来,每个客户项目都认真落实执行。
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(); MaplimitedUrlMap = switchDomain.getLimitedUrlMap(); if (limitedUrlMap != null && limitedUrlMap.size() > 0) { for (Map.Entry 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的作用是什么,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。