一、zipkin作用
- 全鏈路追蹤工具(查看依賴關系)
- 查看每個接口、每個service的執行速度(定位問題發生點或者尋找性能瓶頸)
二、zipkin工作原理
- 創造一些追蹤標識符(tracingId,spanId,parentId),最終將一個request的流程樹構建出來
三、zipkin架構
1、Transport
- transport作用:收集被trace的services的spans,並將它們轉化為zipkin common Span,之后把這些Spans傳遞的存儲層。
- 三種主要的transport:
- HTTP(默認)
- 通過http headers來傳遞追蹤信息
- header中的key
- X-B3-TraceId: 64 encoded bits(id被encode為hex Strings)
- X-B3-SpanId: 64 encoded bits
- X-B3-ParentSpanId: 64 encoded bits
- X-B3-Sampled: Boolean (either “1” or “0”)(下面的調用是否進行采樣)
- X-B3-Flags: a Long
- Scribe
- Kafka
- HTTP(默認)
2、基礎架構(4個組件)
- collector
- 作用:zipkin collector會對一個到來的被trace的數據(span)進行驗證、存儲並設置索引。
- storage
- search
- webUI
四、zipkin核心數據結構
- Annotation(用途:用於定位一個request的開始和結束,cs/sr/ss/cr含有額外的信息,比如說時間點)
- cs:Client Start - This sets the beginning of the span
- 一個span的開始
- sr:Server Receive - The server has received the request and will start processing it
- ss:Server Send - The server has completed processing and has sent the request back to the client
- cr:Client Receive - The client has received the response from the server. This sets the end of the span. The RPC is considered complete when this annotation is recorded
- 一個span的結束
- 當這個annotation被記錄了,這個RPC也被認為完成了
- cs:Client Start - This sets the beginning of the span
- BinaryAnnotation(用途:They are meant to provide extra information about the RPC)
- Span:就是一個請求(包含一組Annotation和BinaryAnnotation)
- Spans contain identifying information such as traceId, spandId, parentId, and RPC name
- Trace:
- Traces are built by collecting all Spans that share a traceId
- 通過traceId、spanId和parentId,被收集到的span會匯聚成一個tree,從而提供出一個request的整體流程。(這也是zipkin的工作原理)
注意:時間點計算
- sr-cs:網絡延遲
- ss-sr:邏輯處理時間
- cr-cs:整個流程時間
五、Trace identifiers
- 含義:通過下邊3個Id,對數據進行重組
- 三個Id(64位 long型數據)
- TraceId
- The overall ID of the trace.
- Every span in a trace will share this ID.
- SpanId
- The ID for a particular span.
- This may or may not be the same as the trace id.
- ParentId
- This is an optional ID that will only be present on child spans.
- That is the span without a parent id is considered the root of the trace.
- TraceId
六:zipkin工作流程圖
說明:
- X和A可以相等
疑問:
- spanId==C的span為什要有,是否可以省掉?
父子span關系:
說明:parentId==null,表示該span就是root span。
七、注意點
1、使用zipkin,必須使用java8
2、在生產環境,不會對每個請求都進行采樣追蹤(降低trace對整個服務的性能損耗)
參考:
https://github.com/openzipkin/zipkin/tree/master/zipkin-server server配置