环境启动的时候会通过springboot加载自动配置 org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration#zuulServlet
执行此方法,会将zuulServlet 加载到springweb中
zuulServlet 的路径配置,默认是/*
Servet中的代码如下
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { try { this.init((HttpServletRequest)servletRequest, (HttpServletResponse)servletResponse); RequestContext e = RequestContext.getCurrentContext(); e.setZuulEngineRan(); try { this.preRoute(); } catch (ZuulException var13) { this.error(var13); this.postRoute(); return; } try { this.route(); } catch (ZuulException var12) { this.error(var12); this.postRoute(); return; } try { this.postRoute(); } catch (ZuulException var11) { this.error(var11); } } catch (Throwable var14) { this.error(new ZuulException(var14, 500, "UNHANDLED_EXCEPTION_" + var14.getClass().getName())); } finally { RequestContext.getCurrentContext().unset(); } }
按顺序执行了 preRoute route postRoute 等规则路由
这些执行都给了 zuulRunner ---> FilterProcessor
processor处理的逻辑代码如下
public Object runFilters(String sType) throws Throwable { if (RequestContext.getCurrentContext().debugRouting()) { Debug.addRoutingDebug("Invoking {" + sType + "} type filters"); } boolean bResult = false; List<ZuulFilter> list = FilterLoader.getInstance().getFiltersByType(sType); if (list != null) { for (int i = 0; i < list.size(); i++) { ZuulFilter zuulFilter = list.get(i); Object result = processZuulFilter(zuulFilter); if (result != null && result instanceof Boolean) { bResult |= ((Boolean) result); } } } return bResult; }
List<ZuulFilter> list = FilterLoader.getInstance().getFiltersByType(sType); 这句可以获取所有的Filter类型
是从 FilterRegistry. filters 属性中来的
FilterRegistry 中的filters 是启动的时候org.springframework.cloud.netflix.zuul.ZuulFilterInitializer#contextInitialized 注入的
ZuulFilterInitializer又是从下面的代码中获取的
org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration.ZuulFilterConfiguration#zuulFilterInitializer 代码如下
@Configuration protected static class ZuulFilterConfiguration { @Autowired private Map<String, ZuulFilter> filters; @Bean public ZuulFilterInitializer zuulFilterInitializer( CounterFactory counterFactory, TracerFactory tracerFactory) { FilterLoader filterLoader = FilterLoader.getInstance(); FilterRegistry filterRegistry = FilterRegistry.instance(); return new ZuulFilterInitializer(this.filters, counterFactory, tracerFactory, filterLoader, filterRegistry); } }
filters 聚合 ZuulServerAutoConfiguration ZuulProxyAutoConfiguration 这两个自动配置类的 filter实例
这里比较重要的
RibbonRoutingFilter
SimpleHostRoutingFilter