The concept and principle of zuul

1. What is zuul

Zuul is an API Gateway server open sourced by netflix, which is essentially a web servlet application.

Zuul provides a framework for dynamic routing, monitoring, elasticity, security and other edge services on the cloud platform. Zuul is equivalent to being the front door for all requests to the backend of the web site on the device and the Netflix streaming app.

For the example of zuul, please refer to the simple webapp of netflix on github, which can be used according to the documentation of netflix on the github wiki.

Second, the working principle of zuul

1. Filter mechanism

The core of zuul is a series of filters , its role can be analogous to the Filter of the Servlet framework, or AOP.

In the process of zuul routing the Request to the user processing logic, these filters participate in some filtering processing, such as Authentication, Load Shedding, etc.  

 

Zuul provides a framework to dynamically load, compile, and run filters.

There is no direct communication between Zuul's filters, and they pass data through a static class of RequestContext. There is a ThreadLocal variable in the RequestContext class to record the data that each Request needs to pass.

Zuul's filters are written by Groovy. These filter files are placed under specific directories on Zuul Server. Zuul will poll these directories regularly, and the modified filters will be dynamically loaded into Zuul Server for filtering requests.

There are several standard filter types:

Most of Zuul's functions are implemented through filters. There are four standard filter types defined in Zuul that correspond to the typical lifecycle of a request.

(1) PRE: This filter is invoked before the request is routed. We can use this filter to implement authentication, select requested microservices in the cluster, log debug information, and more.

(2) ROUTING: This filter routes requests to microservices. This filter is used to construct requests to microservices and request microservices using Apache HttpClient or Netfilx Ribbon.

(3) POST: This filter is executed after routing to the microservice. Such filters can be used to add standard HTTP headers to responses, collect statistics and metrics, send responses from microservices to clients, and more.

(4) ERROR: This filter is executed when an error occurs in other stages.

Built-in special filter

Zuul also provides a special kind of filter, namely: StaticResponseFilter and SurgicalDebugFilter

StaticResponseFilter: StaticResponseFilter allows to generate the response from Zuul itself instead of forwarding the request to the origin.

SurgicalDebugFilter: SurgicalDebugFilter allows specific requests to be routed to separate debug clusters or hosts.

custom filter

In addition to the default filter types, Zuul also allows us to create custom filter types.

For example, we can customize a STATIC type filter to generate the response directly in Zuul without forwarding the request to the backend microservice.

 

2, the life cycle of the filter

The life cycle of a Zuul request is shown in the figure, which details the execution order of various types of filters.

 

3. Filter scheduling process

 4. Dynamic loading of filters

 

3. What can zuul do?

Zuul can implement the following functions by loading a dynamic filtering mechanism:

  • Authentication and Security: Identify authentication requirements for various resources and reject those that do not.
  • Review and Monitoring: Track meaningful data and statistics at the edge, giving us accurate conclusions about production status.
  • Dynamic routing: Route requests to different backend clusters dynamically as needed.
  • Stress Test: Gradually increase the load traffic to the cluster to calculate the performance level.
  • Load distribution: Allocate the corresponding capacity for each load type, and discard requests that exceed the limit.
  • Static response handling: Partial responses are built directly at the edge location, avoiding their flow into the internal cluster.
  • 多区域弹性: 跨越AWS区域进行请求路由,旨在实现ELB使用多样化并保证边缘位置与使用者尽可能接近。

除此之外,Netflix公司还利用Zuul的功能通过金丝雀版本实现精确路由与压力测试。

四、zuul 与应用的集成方式

1、ZuulServlet - 处理请求(调度不同阶段的filters,处理异常等) 

ZuulServlet类似SpringMvc的DispatcherServlet,所有的Request都要经过ZuulServlet的处理

三个核心的方法preRoute(),route()postRoute(),zuul对request处理逻辑都在这三个方法里

ZuulServlet交给ZuulRunner去执行。

由于ZuulServlet是单例,因此ZuulRunner也仅有一个实例。

ZuulRunner直接将执行逻辑交由FilterProcessor处理,FilterProcessor也是单例,其功能就是依据filterType执行filter的处理逻辑

FilterProcessor对filter的处理逻辑。

  • 首先根据Type获取所有输入该Type的filter,List<ZuulFilter> list
  • 遍历该list,执行每个filter的处理逻辑,processZuulFilter(ZuulFilter filter)
  • RequestContext对每个filter的执行状况进行记录,应该留意,此处的执行状态主要包括其执行时间、以及执行成功或者失败,如果执行失败则对异常封装后抛出。 
  • 到目前为止,zuul框架对每个filter的执行结果都没有太多的处理,它没有把上一filter的执行结果交由下一个将要执行的filter,仅仅是记录执行状态,如果执行失败抛出异常并终止执行。

2、ContextLifeCycleFilter - RequestContext 的生命周期管理 

ContextLifecycleFilter的核心功能是为了清除RequestContext; 请求上下文RequestContext通过ThreadLocal存储,需要在请求完成后删除该对象。 

RequestContext提供了执行filter Pipeline所需要的Context,因为Servlet是单例多线程,这就要求RequestContext即要线程安全又要Request安全

context使用ThreadLocal保存,这样每个worker线程都有一个与其绑定的RequestContext,因为worker仅能同时处理一个Request,这就保证了Request Context 即是线程安全的由是Request安全的。

3、GuiceFilter - GOOLE-IOC(Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。) 

4、StartServer - 初始化 zuul 各个组件 (ioc、插件、filters、数据库等)

5、FilterScriptManagerServlet -  uploading/downloading/managing scripts, 实现热部署

Filter源码文件放在zuul 服务特定的目录, zuul server会定期扫描目录下的文件的变化,动态的读取\编译\运行这些filter,

如果有Filter文件更新,源文件会被动态的读取,编译加载进入服务,接下来的Request处理就由这些新加入的filter处理。

 

 

http://www.cnblogs.com/lexiaofei/p/7080257.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326155384&siteId=291194637