1. 程式人生 > >實用技巧:快速定位Zuul的效能瓶頸

實用技巧:快速定位Zuul的效能瓶頸

開發十年,就只剩下這套架構體系了! >>>   

Zuul的效能不是特別好,特別是,某些專案對Zuul進行了一些擴充套件,程式碼還不那麼考究時。

如何快速定位出Zuul的效能瓶頸呢?我們知道,Zuul的核心是過濾器,Zuul大多功能都是基於過濾器實現的。

一次請求,會經過若干過濾器,如何檢視每個過濾器執行的耗時呢?只需開啟Zuul的Debug能力即可。

本文基於Spring Cloud Finchley,同樣適用於Spring Cloud Greenwich。

Edgware及更早版本,需配置 management.security.enabled = false ,並本文的 /actuator/httptrace 改為 /trace

TIPS

如果您對Zuul的過濾器不太瞭解,可先閱讀如下兩篇文章:

開啟Debug引數

zuul:
  include-debug-header: true
management:
  endpoints:
    web:
      exposure:
        include: '*'

這樣,只需在訪問Zuul時,新增 ?debug=true

即可對Zuul進行Debug。例如監控路徑ZUUL_HOST:ZUUL_PORT/SOME_PATH 經過了哪些過濾器,效能瓶頸出現在哪個過濾器,只需構造 ZUUL_HOST:ZUUL_PORT/SOME_PATH?debug=true 即可。

請求後,訪問 ZUUL_HOST:ZUUL_PORT/actuator/httptrace ,即可看到類似如下的結果:

"X-Zuul-Debug-Header": ["[[[Filter pre 5 PreDecorationFilter]]][[[Filter {PreDecorationFilter TYPE:pre ORDER:5} Execution time = 1ms]]][[[{PreDecorationFilter} added retryable=false]]][[[{PreDecorationFilter} added ignoredHeaders=[authorization, set-cookie, cookie]]]][[[{PreDecorationFilter} added originResponseHeaders=[com.netflix.util.Pair@d68cf7e9]]]][[[{PreDecorationFilter} added zuulRequestHeaders={x-forwarded-host=localhost:8040, x-forwarded-proto=http, x-forwarded-prefix=/microservice-provider-user, x-forwarded-port=8040, x-forwarded-for=0:0:0:0:0:0:0:1}]]][[[{PreDecorationFilter} added requestURI=/users/1]]][[[{PreDecorationFilter} added proxy=microservice-provider-user]]][[[{PreDecorationFilter} changed executedFilters=ServletDetectionFilter[SUCCESS][0ms], Servlet30WrapperFilter[SUCCESS][0ms], DebugFilter[SUCCESS][0ms], PreDecorationFilter[SUCCESS][1ms]]]][[[{PreDecorationFilter} added serviceId=microservice-provider-user]]][[[Invoking {route} type filters]]][[[Filter route 10 RibbonRoutingFilter]]][[[Filter {RibbonRoutingFilter TYPE:route ORDER:10} Execution time = 9ms]]][[[{RibbonRoutingFilter} changed originResponseHeaders=[com.netflix.util.Pair@d68cf7e9, com.netflix.util.Pair@694b84a6, com.netflix.util.Pair@a4baea16, com.netflix.util.Pair@99438774]]]][[[{RibbonRoutingFilter} added responseDataStream=org.apache.http.conn.EofSensorInputStream@1145027a]]][[[{RibbonRoutingFilter} added zuulResponseHeaders=[com.netflix.util.Pair@694b84a6, com.netflix.util.Pair@99438774]]]][[[{RibbonRoutingFilter} added responseStatusCode=200]]][[[{RibbonRoutingFilter} added responseGZipped=false]]][[[{RibbonRoutingFilter} added ribbonResponse=org.springframework.cloud.netflix.ribbon.apache.RibbonApacheHttpResponse@5e2ce130]]][[[{RibbonRoutingFilter} changed executedFilters=ServletDetectionFilter[SUCCESS][0ms], Servlet30WrapperFilter[SUCCESS][0ms], DebugFilter[SUCCESS][0ms], PreDecorationFilter[SUCCESS][1ms], RibbonRoutingFilter[SUCCESS][9ms]]]][[[{RibbonRoutingFilter} added zuulResponse=org.springframework.cloud.netflix.ribbon.RibbonHttpResponse@1e0eabde]]][[[Filter route 100 SimpleHostRoutingFilter]]][[[Filter route 500 SendForwardFilter]]][[[Invoking {post} type filters]]][[[Filter post 1000 SendResponseFilter]]]"],

由結果可知,該端點依次列印了請求經過了哪些過濾器、每個過濾器的耗時。簡單分析一下,就能瞭解Zuul的效能瓶頸了。

開啟預設Debug

經過上面的配置,已實現對Zuul的Debug,但每次都要新增一個debug=true 的小尾巴,也是挺煩的,如果不想新增,而想讓Zuul預設就對請求開啟Debug,該怎麼辦呢?

也非常簡答,只需在上文的基礎上,新增如下配置即可:

zuul:
  debug:
    request: true

這樣,即使不新增debug=true ,Zuul也會Debug。

相關原始碼

相關原始碼其實比較簡單,就一個類: org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter,有興趣的童鞋可以瞭解一下。

本文首發

http://www.itmuch.com/spring-cloud-sum/debug-zuul/

乾貨分享