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流與核心架構。這些在下一章會以代碼的形式作展現。