十四、微服务跟踪之Spring Cloud Sleuth

为什么要实现微服务跟踪

微服务之间通过网络进行通信,同时网络常常很脆弱,网络资源也有限。如果能够跟踪每个请求,了解请求经过哪些微服务、请求耗费时间、网络延迟、业务逻辑耗费时间等指标、那么久能更好地分析系统瓶颈、解决系统问题。

Sping Cloud Sleuth简介

Sping Cloud SleuthSpring Cloud提供了分布式跟踪的解决方案,它大量借用了Google DapperTwitter ZipkinApache HTrace的设计。

Sleuth借用了Dapper的术语。

  • span(跨度):基本工作单元,span 用一个 64 位 ID 唯一标识,span 还有其他数据信息,比如描述、时间戳事件、键值对的注解(标签)、span ID、span 父 ID 等。
    span 在启动和停止时,记录了时间信息,初始化 span 被称为”root span”,该 span 的 ID 和 trace 的 ID 相同。
  • trace(跟踪):一组共享”root span”组成的一个树状结构,trace 也用一个 64 位的 ID 唯一标识,trace 中的所有 span 都共享该 trace 的 ID。
  • Annotation(标注):用来记录事件的存在,核心 annotations 用来定义一个请求的开始和结束。
    • CS(Client Sent 客户端发送):客户端发起一个请求,这个 annotion 描述了这个 span 的开始。
    • SR(Server Received 服务端接收):服务端获得请求并准备开始处理它,如果用 SR 减去 CS 时间戳便可得到网络延迟。
    • SS(Server Sent 服务端发送):表明请求处理的完成(当请求返回客户端),如果 SS 减去 SR 时间戳便可得到服务端需要的处理请求时间。
    • CR(Client Received 客户端接收):span 的结束,客户端成功接收到服务端的回复,如果 CR 减去 CS 时间戳便可得到客户端从服务端获取回复的所有所需时间。

下图描述请求依次经过 SERVICE1 - SERVICE2 - SERVICE3 - SERVICE4 时,span、trace、annotation 的变化。
微服务追踪

整合Sping Cloud Sleuth

  1. pom添加依赖:
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

2.

  • application.yml中配置:
1
2
3
4
5
6
7
spring:
application:
name: micorservice-provider-user
logging:
level:
root: info
org.springframework.web.servlet.DispatcherServlet: debug

访问http://127.0.0.1:8000/1:

1
2
3
4
5
DEBUG [micorservice-provider-user,39dc793c320ef0ea,39dc793c320ef0ea,false] 14741 --- [nio-8000-exec-6] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/1]
DEBUG [micorservice-provider-user,39dc793c320ef0ea,39dc793c320ef0ea,false] 14741 --- [nio-8000-exec-6] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/1] is: -1
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
DEBUG [micorservice-provider-user,39dc793c320ef0ea,39dc793c320ef0ea,false] 14741 --- [nio-8000-exec-6] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
DEBUG [micorservice-provider-user,39dc793c320ef0ea,39dc793c320ef0ea,false] 14741 --- [nio-8000-exec-6] o.s.web.servlet.DispatcherServlet : Successfully completed request
  • 也可设置:
1
2
3
4
5
6
7
spring:
application:
name: micorservice-provider-user
logging:
level:
root: info
org.springframework.cloud.sleuth: debug

可以了解 span 从创建到关闭的详细过程:

1
2
3
4
5
6
7
DEBUG [micorservice-provider-user,,,] 14762 --- [nio-8000-exec-3] o.s.c.sleuth.instrument.web.TraceFilter  : Received a request to uri [/1] that should not be sampled [false]
DEBUG [micorservice-provider-user,dcbdd058ab08ee57,dcbdd058ab08ee57,false] 14762 --- [nio-8000-exec-3] o.s.c.sleuth.instrument.web.TraceFilter : No parent span present - creating a new span
DEBUG [micorservice-provider-user,dcbdd058ab08ee57,dcbdd058ab08ee57,false] 14762 --- [nio-8000-exec-3] o.s.c.s.i.web.TraceHandlerInterceptor : Handling span [Trace: dcbdd058ab08ee57, Span: dcbdd058ab08ee57, Parent: null, exportable:false]
DEBUG [micorservice-provider-user,dcbdd058ab08ee57,dcbdd058ab08ee57,false] 14762 --- [nio-8000-exec-3] o.s.c.s.i.web.TraceHandlerInterceptor : Adding a method tag with value [findId] to a span [Trace: dcbdd058ab08ee57, Span: dcbdd058ab08ee57, Parent: null, exportable:false]
DEBUG [micorservice-provider-user,dcbdd058ab08ee57,dcbdd058ab08ee57,false] 14762 --- [nio-8000-exec-3] o.s.c.s.i.web.TraceHandlerInterceptor : Adding a class tag with value [UserController] to a span [Trace: dcbdd058ab08ee57, Span: dcbdd058ab08ee57, Parent: null, exportable:false]
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
DEBUG [micorservice-provider-user,dcbdd058ab08ee57,dcbdd058ab08ee57,false] 14762 --- [nio-8000-exec-3] o.s.c.sleuth.instrument.web.TraceFilter : Closing the span [Trace: dcbdd058ab08ee57, Span: dcbdd058ab08ee57, Parent: null, exportable:false] since the response was successful

该文章摘自《Spring Cloud 与 Docker 微服务架构实战(第二版)》

以上

LeoQin wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
0%