Spring Cloud Sleuth 文档介绍
Spring Cloud 文档介绍
code.7z

一、Introduce

Spring cloud 官方文档

Spring Cloud Sleuth implements a distributed tracing solution for Spring Cloud.

Spring Cloud Sleuth 为 Spring Cloud 提供的分布式追踪解决方案

二、Terminology

Spring Cloud Sleuth borrows Dapper’s terminology.

Spring Cloud Sleuth 借鉴了 Dapper 论文的相关术语概念。

Span

The basic unit of work. For example, sending an RPC is a new span, as is sending a response to an RPC. Spans are identified by a unique 64-bit ID for the span and another 64-bit ID for the trace the span is a part of. Spans also have other data, such as descriptions, timestamped events, key-value annotations (tags), the ID of the span that caused them, and process IDs (normally IP addresses).

大意如下:

span 分布式追踪链路的基本组成单位。每一次的 RPC 调用都会产生一个新的 span

通过一个唯一的 64位 span ID 和另一个 64 位 span 所属的 trace ID 可以唯一标识一个 spanspan 还包含了其他数据,如:描述,时间戳,键值标记,进程ID(通常为 IP地址)

小贴士 The initial span that starts a trace is called a root span. The value of the ID of that span is equal to the trace ID. 大意如下: 链路中的第一个 span 也称为 root span ,该 root spanspan id = trace id

Trace

A set of spans forming a tree-like structure. For example, if you run a distributed big-data store, a trace might be formed by a PUT request.

Trace 就是追踪链,每次请求所产生的 span 组合而成的一个树形结构链,就是一个追踪链(Trace)。

Annotation

Used to record the existence of an event in time. With Brave instrumentation, we no longer need to set special events for Zipkin to understand who the client and server are, where the request started, and where it ended. For learning purposes, however, we mark these events to highlight what kind of an action took place.

  • cs: Client Sent. The client has made a request. This annotation indicates the start of the span.
  • sr: Server Received: The server side got the request and started processing it. Subtracting the cs timestamp from this timestamp reveals the network latency.
  • ss: Server Sent. Annotated upon completion of request processing (when the response got sent back to the client). Subtracting the sr timestamp from this timestamp reveals the time needed by the server side to process the request.
  • cr: Client Received. Signifies the end of the span. The client has successfully received the response from the server side. Subtracting the cs timestamp from this timestamp reveals the whole time needed by the client to receive the response from the server.

Annotation 是用来标记 sapn 所处状态:

  • CS(Client Send): 表示该 span 起始发送。
  • SR(Server Received):标记 span 为服务端接收状态
  • SS(Server Send):标记 span 服务端处理完成后的发送状态
  • CR(Client Received):标记 span 客户端接收的状态,此时 span 结束。

在使用 Brave 时,信息在推送给 zipkin 时可以不再需要特殊标注 sapn 状态。

三、简单链路逻辑图

请求调用图

03.png

链路调用简图

02.png

链路调用详图

01.png

四、Sleuth Getting Started

项目结构如下
04.png
项目关系如下
05.png

引入 sleuth 依赖

  1. <!-- sleuth -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-sleuth</artifactId>
  5. </dependency>

sleuth 简单配置

  1. ## sleuth 配置
  2. spring.sleuth.web.client.enabled = true
  3. spring.sleuth.feign.enabled = true
  4. spring.sleuth.feign.processor.enabled = true
  5. spring.sleuth.sampler.probability = 1
  6. ## 打印日志
  7. logging.level.org.springframework.cloud.openfeign = debug
  8. logging.level.org.springframework.cloud.sleuth = debug

控制台日志

日志打印格式:服务名 , TraceId,spanId,flag=true:将消息推送到其他服务

06.png

zuul 中的 span 为起始 span 所以 tranceId = spanId