1、zuul简介java
一、做用git
zuul使用一系列的filter实现如下功能github
- 认证和安全 - 对每个resource进行身份认证
- 追踪和监控 - 实时观察后端微服务的TPS、响应时间,失败数量等准确的信息
- 日志 - 记录全部请求的访问日志数据,能够为日志分析和查询提供统一支持
- 动态路由 - 动态的将request路由到后端的服务上去
- 压力测试 - 逐渐的增长访问集群的压力,来测试集群的性能
- 限流 - allocating capacity for each type of request and dropping requests that go over the limit
- 静态响应 - 直接在网关返回一些响应,而不是经过内部的服务返回响应
二、组件:web
- zuul-core:library which contains the core functionality of compiling and executing Filters
- zuul-netflix:library which adds other NetflixOSS components to Zuul - using Ribbon for routing requests, for example.
三、例子:后端
- zuul-simple-webapp:webapp which shows a simple example of how to build an application with zuul-core
- zuul-netflix-webapp:webapp which packages zuul-core and zuul-netflix together into an easy to use package
github地址:https://github.com/Netflix/zuul/安全
2、zuul filter架构
一、关键元素app
- Type:most often defines the stage during the routing flow when the Filter will be applied (although it can be any custom string)
- 值能够是:pre、route、post、error、custom
- Execution Order: filter执行的顺序(applied within the Type, defines the order of execution across multiple Filters)
- Criteria:filter执行的条件(the conditions required in order for the Filter to be executed)
- Action: filter执行的动做(the action to be executed if the Criteria is met)
注意点:webapp
- filters之间不会直接进行通信交流,他们经过一个RequestContext共享一个state
- 该RequestContext对于每个request都是惟一的
- filter当前使用groovy来写的,也可使用java
- The source code for each Filter is written to a specified set of directories on the Zuul server that are periodically polled for changes
- zuul能够动态的read, compile, and run these Filters
- 被更新后的filter会被从disk读取到内存,并动态编译到正在运行的server中,以后能够用于其后的每个请求(Updated filters are read from disk, dynamically compiled into the running server, and are invoked by Zuul for each subsequent request)
二、filter type(与一个典型的request的生命周期相关的filter type)微服务
- PRE Filters
- 执行时机: before routing to the origin.
- 这类filter可能作的事
- request authentication
- choosing origin servers(选机器)
- logging debug info.
- 限流
- ROUTING Filters
- 这类filter可能作的事:真正的向service的一台server(这台server是pre filter选出来的)发请求,handle routing the request to an origin,This is where the origin HTTP request is built and sent using Apache HttpClient or Netflix Ribbon.
- POST Filters
- 执行时机:after the request has been routed to the origin
- 这类filter可能作的事
- adding standard HTTP headers to the response
- gathering statistics and metrics
- streaming the response from the origin to the client
- ERROR Filters
- 执行时机:其余三个阶段任一阶段发生错误时执行(when an error occurs during one of the other phases)
- CUSTOM Filters
- 沿着默认的filter流,zuul容许咱们建立一些自定义的Filter type,而且准确的执行他们。
- 例如:咱们自定义一个STATIC type的filter,用于从zuul直接产生响应,而不是从后边的services(we have a custom STATIC type that generates a response within Zuul instead of forwarding the request to an origin)
3、zuul request lifecycle(filter流)

说明:对应(二)的filter type来看
4、zuul核心架构

zuul的核心就是:filter、filter流与核心架构。这些在下一章会以代码的形式作展现。