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 ServerStatusManager in nacos

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is to share with you about the role of ServerStatusManager in nacos, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

ServerStatusManager

Nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerStatusManager.java

@ Servicepublic 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 ();}

The init method of ServerStatusManager registers ServerStatusUpdater, which implements the Runnable interface. Its run method executes refreshServerStatus;refreshServerStatus to determine whether consistencyService is available. If the serverStatus is updated, it is ServerStatus.UP, otherwise it is ServerStatus.DOWN.

ServerStatus

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 has the states of UP, DOWN, STARTING, PAUSED, WRITE_ONLY and READ_ONLY

TrafficReviseFilter

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 limitedUrlMap = 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 will read and write routes according to status and httpMethod, and return HttpServletResponse.SC_SERVICE_UNAVAILABLE if the route is not available.

Summary

The init method of ServerStatusManager registers ServerStatusUpdater, which implements the Runnable interface. Its run method executes refreshServerStatus;refreshServerStatus to determine whether consistencyService is available. If the serverStatus is updated, it is ServerStatus.UP, otherwise it is ServerStatus.DOWN.

This is what the role of ServerStatusManager in nacos is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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