In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use ZuulController in Springcloud. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
The version of Springcloud is Greenwich.SR2,Springboot and the version is 2.1.6.release.
Recently using the zuul of Springcloud, I analyzed the following source code and recorded the following List-1, mainly the zuulHandlerMapping method. When constructing ZuulHandlerMapping, the RouteLocator passed in is CompositeRouteLocator, while this.zuulController () returns the ZuulController class.
List-1
@ Beanpublic ZuulController zuulController () {return new ZuulController ();} @ Beanpublic ZuulHandlerMapping zuulHandlerMapping (RouteLocator routes) {ZuulHandlerMapping mapping = new ZuulHandlerMapping (routes, this.zuulController ()); mapping.setErrorController (this.errorController); mapping.setCorsConfigurations (this.getCorsConfigurations ()); return mapping;}
As shown in figure 1 below, ZuulHandlerMapping inherits AbstractUrlHandlerMapping, and if you know Springmvc, you should be familiar with this. ZuulHandlerMapping overrides the lookupHandler of the parent class in order to register the mapping of url-- > controller with Springmvc, as shown in List-2.
Figure 1
In the following List-2,registerHandlers method, iterate through all the Route, and then set the processing class of those Route to ZuulController. There are three implementations of routeLocator, namely, SimpleRouteLocator, DiscoveryClientRouteLocator, and DiscoveryClientRouteLocator.
List-2
Private final ZuulController zuul;@Overrideprotected Object lookupHandler (String urlPath, HttpServletRequest request) throws Exception {if (this.errorController! = null & & urlPath.equals (this.errorController.getErrorPath () {return null;} if (isIgnoredPath (urlPath, this.routeLocator.getIgnoredPaths () {return null;} RequestContext ctx = RequestContext.getCurrentContext (); if (ctx.containsKey ("forward.to")) {return null } if (this.dirty) {synchronized (this) {if (this.dirty) {registerHandlers (); this.dirty = false;} return super.lookupHandler (urlPath, request);} private void registerHandlers () {Collection routes = this.routeLocator.getRoutes () If (routes.isEmpty ()) {this.logger.warn ("No routes found from RouteLocator");} else {for (Route route: routes) {registerHandler (route.getFullPath (), this.zuul);}
According to the above analysis, we know that ZuulController will handle it, so let's take a look at ZuulController, as shown in the following List-3. If you look at ServletWrappingController, the parent class of ZuulController, you will see that the request is handed over to ZuulServlet for processing, and how ZuulServlet and ZuulFilter are handled together. Read another blog.
List-3
Public class ZuulController extends ServletWrappingController {public ZuulController () {setServletClass (ZuulServlet.class); setServletName ("zuul"); setSupportedMethods ((String []) null) / / Allow all} @ Override public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception {try {/ / We don't care about the other features of the base class, just want to / / handle the request return super.handleRequestInternal (request, response) } finally {/ @ see com.netflix.zuul.context.ContextLifecycleFilter.doFilter RequestContext.getCurrentContext () .unset ();}
At this point, we know how Zuul maps Route routing information (which we configured in springboot application.yml) to ZuulController, which is then delegated to ZuulServlet by ZuulController.
After reading the above, do you have any further understanding of how to use ZuulController in Springcloud? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.