Fork me on GitHub

实用技巧:快速定位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参数

1
2
3
4
5
6
7
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 ,即可看到类似如下的结果:

1
"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,该怎么办呢?

也非常简答,只需在上文的基础上,添加如下配置即可:

1
2
3
zuul:
debug:
request: true

这样,即使不添加debug=true ,Zuul也会Debug。

相关源码

相关源码其实比较简单,就一个类: org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter,有兴趣的童鞋可以了解一下。

相关文章

评论系统未开启,无法评论!