您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Springcloud中ZuulController如何使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
Springcloud的版本是Greenwich.SR2,Springboot版本是2.1.6.release.
最近使用到Springcloud的zuul,分析了下源碼,記錄下,如下的List-1,主要是zuulHandlerMapping方法,構(gòu)造ZuulHandlerMapping時,傳入的RouteLocator是CompositeRouteLocator,而this.zuulController()返回的是ZuulController類。
List-1
@Bean public ZuulController zuulController() { return new ZuulController(); } @Bean public ZuulHandlerMapping zuulHandlerMapping(RouteLocator routes) { ZuulHandlerMapping mapping = new ZuulHandlerMapping(routes, this.zuulController()); mapping.setErrorController(this.errorController); mapping.setCorsConfigurations(this.getCorsConfigurations()); return mapping; }
如下圖1所示,ZuulHandlerMapping繼承了AbstractUrlHandlerMapping,如果了解Springmvc,就對這個應(yīng)該熟悉點了。ZuulHandlerMapping復(fù)寫了父類的lookupHandler,目的是將url-->controller這個映射關(guān)系注冊到Springmvc中,如List-2所示。
圖1
如下List-2,registerHandlers方法中,遍歷所有的Route,之后將這些Route的處理類設(shè)置為ZuulController。routeLocator的實現(xiàn)有三個,分別是SimpleRouteLocator、DiscoveryClientRouteLocator、DiscoveryClientRouteLocator。
List-2
private final ZuulController zuul; @Override protected 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<Route> 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); } } }
上面分析,知道了由ZuulController來處理,那么來看下ZuulController,如下List-3所示,如果去看ZuulController的父類ServletWrappingController,會看到,是將請求交給ZuulServlet來處理,ZuulServlet和ZuulFilter是如何結(jié)合起來處理的,看另一篇博客。
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(); } } }
到此,我們知道Zuul是如何將Route路由信息(我們在springboot application.yml中配置的)映射到ZuulController,而后ZuulController委托給ZuulServlet來處理。
看完上述內(nèi)容,你們對Springcloud中ZuulController如何使用有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。