1.zuul 1.x的架构如下所示:

线程模型:

其web应用的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><listener><listener-class>com.netflix.zuul.StartServer</listener-class></listener><servlet><servlet-name>ZuulServlet</servlet-name><servlet-class>com.netflix.zuul.http.ZuulServlet</servlet-class></servlet><servlet-mapping><servlet-name>ZuulServlet</servlet-name><url-pattern>/*</url-pattern></servlet-mapping><filter><filter-name>ContextLifecycleFilter</filter-name><filter-class>com.netflix.zuul.context.ContextLifecycleFilter</filter-class></filter><filter-mapping><filter-name>ContextLifecycleFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

从上面可以看出,启动时有三个主类:

1.1. StartServer

    @Overridepublic void contextInitialized(ServletContextEvent sce) {logger.info("starting server");// mocks monitoring infrastructure as we don't need it for this simple app
        MonitoringHelper.initMocks();// initializes groovy filesystem poller
        initGroovyFilterManager();// initializes a few java filter examples
        initJavaFilters();}

1.2. ZuulServlet

@Overridepublic void service(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws ServletException, IOException {try {init((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse);// Marks this request as having passed through the "Zuul engine", as opposed to servlets// explicitly bound in web.xml, for which requests will not have the same data attachedRequestContext 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();}}

1.3. ContextLifecycleFilter

public class ContextLifecycleFilter implements Filter {public void destroy() {}public void init(FilterConfig filterConfig) throws ServletException {}public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {try {chain.doFilter(req, res);} finally {RequestContext.getCurrentContext().unset();}}}

2. zuul2的线程模型

其应用的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><filter><filter-name>guiceFilter</filter-name><filter-class>com.google.inject.servlet.GuiceFilter</filter-class></filter><filter-mapping><filter-name>guiceFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><listener><listener-class>com.netflix.zuul.StartServer</listener-class></listener></web-app>

2.1. StartServer

 /*** Overridden solely so we can tell how much time is being spent in overall initialization. Without* overriding we can't tell how much time was spent in BaseServer doing its own initialization.** @param sce*/@Overridepublic void contextInitialized(ServletContextEvent sce) {try {server.start();} catch (Exception e) {LOG.error("Error while starting karyon.", e);throw Throwables.propagate(e);}try {initialize();} catch (Exception e) {e.printStackTrace();}super.contextInitialized(sce);}

2.2. ZuulServlet

   @Overridepublic void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException{try {zuulProcessor.process(servletRequest, servletResponse).doOnNext(msg -> {// Store this response as an attribute for any later ServletFilters that may want access to info in it.servletRequest.setAttribute("_zuul_response", msg);}).subscribe();}catch (Throwable e) {LOG.error("Unexpected error running ZuulHttpProcessor for this request.", e);throw new ServletException("Unexpected error running ZuulHttpProcessor for this request.");}}

2.3 ZuulHttpProcessor

/*** The main processing class for Zuul.** 1. Translates the inbound native request (ie. HttpServletRequest, or rxnetty HttpServerRequest) into a zuul HttpRequestMessage.* 2. Builds the filter chain and passes the request through it.* 3. Writes out the HttpResponseMessage to the native response object.*/

处理过程:

  public Observable<ZuulMessage> process(final I nativeRequest, final O nativeResponse){// Setup the context for this request.final SessionContext context;// Optionally decorate the context.if (decorator == null) {context = new SessionContext();} else {context = decorator.decorate(new SessionContext());}return Observable.defer((Func0<Observable<ZuulMessage>>) () -> {// Build a ZuulMessage from the netty request.final ZuulMessage request = contextFactory.create(context, nativeRequest, nativeResponse);// Start timing the request.
            request.getContext().getTimings().getRequest().start();/** Delegate all of the filter application logic to {@link FilterProcessor}.* This work is some combination of synchronous and asynchronous.*/Observable<ZuulMessage> chain = filterProcessor.applyFilterChain(request);return chain.flatMap(msg -> {// Wrap this in a try/catch because we need to ensure no exception stops the observable, as// we need the following doOnNext to always run - as it records metrics.try {// Write out the response.return contextFactory.write(msg, nativeResponse);}catch (Exception e) {LOG.error("Error in writing response! request=" + request.getInfoForLogging(), e);// Generate a default error response to be sent to client.return Observable.just(new HttpResponseMessageImpl(context, ((HttpResponseMessage) msg).getOutboundRequest(), 500));}finally {// End the timing.
                            msg.getContext().getTimings().getRequest().end();}}).doOnError(e -> {LOG.error("Unexpected error in filter chain! request=" + request.getInfoForLogging(), e);}).doOnNext(msg -> {// Notify requestComplete listener if configured.try {if (requestCompleteHandler != null)requestCompleteHandler.handle(((HttpRequestMessage) request).getInboundRequest(), (HttpResponseMessage) msg);}catch (Exception e) {LOG.error("Error in RequestCompleteHandler.", e);}});}).finallyDo(() -> {// Cleanup any resources related to this request/response.
            sessionCleaner.cleanup(context);});}
}

参考文献:

【1】http://techblog.netflix.com/2013/06/announcing-zuul-edge-service-in-cloud.html

【2】http://techblog.netflix.com/2016/09/zuul-2-netflix-journey-to-asynchronous.html?utm_source=tuicool&utm_medium=referral

转载于:https://www.cnblogs.com/davidwang456/p/6421025.html

netflix zuul 1.x与zuul2.x之比较相关推荐

  1. Zuul spring cloud zuul com.netflix.zuul.exception.ZuulException GENERAL解决方案

    Zuul spring cloud zuul com.netflix.zuul.exception.ZuulException GENERAL解决方案 参考文章: (1)Zuul spring clo ...

  2. Spring Cloud Netflix Zuul中的速率限制

    来源:SpringForAll社区 1.引言 Spring Cloud Netflix Zuul 是一个包含Netflix Zuul的开源网关.它为Spring Boot应用增加了一些特别的特性.不幸 ...

  3. com.netflix.zuul.exception.ZuulException: Hystrix Readed time out

    通过API网关路由来访问用户服务,zuul默认路由规则 :http://zuul的Host地址:zuul端口/要调用的服务名/服务方法地址 浏览器中打开http://127.0.0.1:8000/wa ...

  4. Netflix Zuul与Nginx的性能对比

    2019独角兽企业重金招聘Python工程师标准>>> 这是一篇翻译,关于大家经常质疑的一个问题:API网关Zuul的性能. 原文:NETFLIX ZUUL VS NGINX PER ...

  5. 积分和人民币比率_通过比率路由到旧版和现代应用程序–通过Spring Cloud的Netflix Zuul...

    积分和人民币比率 从应用程序的旧版本迁移到应用程序的现代化版本时,一个非常常见的要求是能够将用户缓慢迁移到新应用程序. 在本文中,我将介绍通过Spring Cloud使用对Netflix Zuul的支 ...

  6. 基于比率的路由到旧版和现代应用程序–通过Spring Cloud的Netflix Zuul

    从应用程序的旧版本迁移到应用程序的现代化版本时,一个非常普遍的要求是能够将用户缓慢迁移到新应用程序. 在本文中,我将介绍通过Spring Cloud使用对Netflix Zuul的支持编写的这种路由层 ...

  7. springcloud 网关_Spring Cloud 系列之 Netflix Zuul 服务网关(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里: 哈喽沃德先生:Spring Cloud 系列之 Netflix Zuul 服务网关(一)​zhuanlan.zhihu.com 本篇文章讲解 Zuul ...

  8. 聊聊 API Gateway 和 Netflix Zuul

    转自:http://www.scienjus.com/api-gateway-and-netflix-zuul/?hmsr=toutiao.io&utm_medium=toutiao.io&a ...

  9. spring clude ---服务网关组件Netflix Zuul

    介绍:服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能. Zuul是Netflix开源的微服 ...

最新文章

  1. 【POJ】2503 Babelfish(字典树,map,指针)
  2. [APIO2018]铁人两项——圆方树+树形DP
  3. spectral hashing--谱哈希源码解析
  4. 谷歌笔试题(Google十二岁生日晚)
  5. [LeetCode] Minimum Depth of Binary Tree
  6. kafka 脚本发送_Kafka笔记归纳(第五部分:一致性保证,消息重复消费场景及解决方式)...
  7. activemq 开启监听_SpringBoot集成ActiveMQ怎么实现Topic发布/订阅模式通信?
  8. vasp软件全名是什么_Materials Studio软件常见问题与解答
  9. ViewPager通过自定义适配器MyPagerAdapter实现界面导航(上标题)
  10. Unity3D游戏开发之仿仙剑奇侠传一2D游戏 (一)
  11. 最适合人工智能开发的5种编程语言首选Python的原因
  12. 如何把手机投影到电脑,电脑可以当作电视一样被投屏
  13. unity摄像机带碰撞检测 摄像机碰到带碰撞物体自动拉近
  14. dnsmasq-ipv6测试
  15. wp文件转shp_ArcGIS教程:MapGIS转换shp攻略
  16. 关闭msmpeng_关闭Windows Defender与性能提升测试
  17. android google map 标记,android,在googlemap上从位图添加标记
  18. 放开后经济会变好吗?越南是怎样度过的?
  19. 格兰杰因果检验如何分析?
  20. 文件中查找并删除feff

热门文章

  1. mysql php 增删数据,php学习之mysql数据的增删改查
  2. python数据库操作nosql_用Python写一个NoSQL数据库
  3. python接口自动化测试面试题_Python接口自动化面试题总结
  4. 文本编辑器实现打开帮助文件的功能
  5. 蓝桥杯java最小公倍数_蓝桥杯算法训练 最大最小公倍数
  6. 华为魔术手机拆机图解_【连载二十二】手机维修电路基础卡电路
  7. java 之在校期间最后一次实训记录
  8. Job for network.service failed because the control process exited with error code问题
  9. C++ 命名空间 实战(二)之 直接数组访问迭代器访问
  10. C++ 命名空间 实战(一)嵌套的命名空间