微服务网关Zuul源码分析

网关介绍

请参考我上一篇文章:
微服务关键系统网关及实现技术Zuul和Spring Cloud Gateway的区别

Zuul 是 Netflix 公司的开源项目,Spring Cloud 在 Netflix 项目中也已经集成了 Zuul,依赖名叫:spring-cloud-starter-netflix-zuul。

Zuul是基于 Servlet 框架构建,采用的是阻塞和多线程方式,即一个线程处理一次连接请求,

Zuul源码分析

本文来分析Zuul作为网关的核心源码。
对于Servlet,我们分析源码一般从HttpServlet的实现类开始分析。

对于Zuul,我们从ZuulServlet开始分析,它是HttpServlet的实现类。

/**
 * zuul的核心servlet,进行初始化并协调zul过滤器的执行
 */
public class ZuulServlet extends HttpServlet {
     

    private static final long serialVersionUID = -3374242278843351500L;
    private ZuulRunner zuulRunner;


    @Override
    public void init(ServletConfig config) throws ServletException {
     
        super.init(config);

        String bufferReqsStr = config.getInitParameter("buffer-requests");
        boolean bufferReqs = bufferReqsStr != null && bufferReqsStr.equals("true") ? true : false;

        zuulRunner = new ZuulRunner(bufferReqs);
    }

    @Override
    public void service(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws ServletException, IOException {
     
        try {
     
            init((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse);

            
            // 将此请求标记为已通过“Zuul引擎”,而不是显式绑定到servlet中的web.xml文件,请求将不会附加相同的数据
            RequestContext context = RequestContext.getCurrentContext();
            context.setZuulEngineRan();

            try {
     
                preRoute();
            } catch (ZuulException e) {
     
                error(e);
                postRoute();
                return;
            }
            try {
     
                route();
            } catch (ZuulException e) {
     
                error(e);
                postRoute();
                return;
            }
            try {
     
                postRoute();
            } catch (ZuulException e) {
     
                error(e);
                return;
            }

        } catch (Throwable e) {
     
            error(new ZuulException(e, 500, "UNHANDLED_EXCEPTION_" + e.getClass().getName()));
        } finally {
     
            RequestContext.getCurrentContext().unset();
        }
    }

主要的几步:
// 过滤前执行
preRoute()
// 执行过滤
route()
// 过滤后执行
postRoute()

还有一个error()方法
这三步的方法类似,我们选择route源码来看

public void route() throws ZuulException {
     
        try {
     
            runFilters("route");  //preRoute方法输入"pre",postRoute方法输入"post",error方法输入"error"
        } catch (ZuulException e) {
     // 执行过滤不通过会抛出ZuulException异常,则过滤失败
            throw e;
        } catch (Throwable e) {
      // 抛出其他异常也是失败,
            throw new ZuulException(e, 500, "UNCAUGHT_EXCEPTION_IN_ROUTE_FILTER_" + e.getClass().getName());
        }
    }

这三步包括error都会调用到runFilters核心方法,源码如下:

    /**
     * 运行filterType类型的所有筛选器
     * 在筛选器中使用此方法可按类型运行自定义筛选器
     */
    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;
    }

再来看看processZuulFilter源码

/**
* 处理一个单独的ZuulFilter。
*  此方法添加调试信息。
*  任何未捕获的thowable都会被这个方法捕获,并转换为带有500个状态代码的ZuulException。
*/
public Object processZuulFilter(ZuulFilter filter) throws ZuulException {
     

        RequestContext ctx = RequestContext.getCurrentContext();
        boolean bDebug = ctx.debugRouting();
        final String metricPrefix = "zuul.filter-";
        long execTime = 0;
        String filterName = "";
        try {
     
            long ltime = System.currentTimeMillis();
            filterName = filter.getClass().getSimpleName();
            
            RequestContext copy = null;
            Object o = null;
            Throwable t = null;

            if (bDebug) {
     
                Debug.addRoutingDebug("Filter " + filter.filterType() + " " + filter.filterOrder() + " " + filterName);
                copy = ctx.copy();
            }
            
            ZuulFilterResult result = filter.runFilter();
            ExecutionStatus s = result.getStatus();
            execTime = System.currentTimeMillis() - ltime;

            switch (s) {
     
                case FAILED:
                    t = result.getException(); // 过滤失败抛出异常
                    ctx.addFilterExecutionSummary(filterName, ExecutionStatus.FAILED.name(), execTime);
                    break;
                case SUCCESS:
                    o = result.getResult(); //过滤成功
                    ctx.addFilterExecutionSummary(filterName, ExecutionStatus.SUCCESS.name(), execTime);
                    if (bDebug) {
     
                        Debug.addRoutingDebug("Filter {" + filterName + " TYPE:" + filter.filterType() + " ORDER:" + filter.filterOrder() + "} Execution time = " + execTime + "ms");
                        Debug.compareContextState(filterName, copy);
                    }
                    break;
                default:
                    break;
            }
            
            if (t != null) throw t;

            usageNotifier.notify(filter, s); //通知成功消息
            return o;

        } catch (Throwable e) {
     
            if (bDebug) {
     
                Debug.addRoutingDebug("Running Filter failed " + filterName + " type:" + filter.filterType() + " order:" + filter.filterOrder() + " " + e.getMessage());
            }
            usageNotifier.notify(filter, ExecutionStatus.FAILED);//通知失败消息
            if (e instanceof ZuulException) {
     
                throw (ZuulException) e;
            } else {
     
                ZuulException ex = new ZuulException(e, "Filter threw Exception", 500, filter.filterType() + ":" + filterName);
                ctx.addFilterExecutionSummary(filterName, ExecutionStatus.FAILED.name(), execTime);
                throw ex;
            }
        }
    }

你可能感兴趣的:(微服务,软件架构师,企业架构)