目录

8.1.1 SpringCloud Sleuth 是什么

  • SpringCloud Sleuth 必知必会

    • SpringCloud Sleuth 实现的功能是:它会自动为当前应用构建起各通信通道的跟踪机制

      1. 通过诸如 RabbitMQ、Kafka(或者其他任何 SpringCloud Sleuth 绑定器实现的消息中间件)传递的请求
      2. 通过 Zuul、Gateway 代理传递的请求
      3. 通过 RestTemplate 发起的请求
  • SpringCloud Sleuth 跟踪实现原理
    • 为了实现请求跟踪:当请求发送到分布式系统的入口端点时,只需要服务跟踪框架为该请求创建一个唯一的跟踪标识 Trace ID
    • 为了统计各处理单元的时间延迟,当请求到达各个服务组件时,或是处理逻辑到达某个状态时,也通过一个唯一标识来标记它的开始、具体过程以及结束,Span ID
#mermaid-svg-01gUNKYLeG2FibyJ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-01gUNKYLeG2FibyJ .label text{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .node rect,#mermaid-svg-01gUNKYLeG2FibyJ .node circle,#mermaid-svg-01gUNKYLeG2FibyJ .node ellipse,#mermaid-svg-01gUNKYLeG2FibyJ .node polygon,#mermaid-svg-01gUNKYLeG2FibyJ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-01gUNKYLeG2FibyJ .node .label{text-align:center;fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .node.clickable{cursor:pointer}#mermaid-svg-01gUNKYLeG2FibyJ .arrowheadPath{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-01gUNKYLeG2FibyJ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-01gUNKYLeG2FibyJ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-01gUNKYLeG2FibyJ .edgeLabel rect{opacity:0.9}#mermaid-svg-01gUNKYLeG2FibyJ .edgeLabel span{color:#333}#mermaid-svg-01gUNKYLeG2FibyJ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-01gUNKYLeG2FibyJ .cluster text{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-01gUNKYLeG2FibyJ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-01gUNKYLeG2FibyJ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-01gUNKYLeG2FibyJ .actor-line{stroke:grey}#mermaid-svg-01gUNKYLeG2FibyJ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-01gUNKYLeG2FibyJ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-01gUNKYLeG2FibyJ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-01gUNKYLeG2FibyJ .sequenceNumber{fill:#fff}#mermaid-svg-01gUNKYLeG2FibyJ #sequencenumber{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-01gUNKYLeG2FibyJ .messageText{fill:#333;stroke:#333}#mermaid-svg-01gUNKYLeG2FibyJ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-01gUNKYLeG2FibyJ .labelText,#mermaid-svg-01gUNKYLeG2FibyJ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-01gUNKYLeG2FibyJ .loopText,#mermaid-svg-01gUNKYLeG2FibyJ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-01gUNKYLeG2FibyJ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-01gUNKYLeG2FibyJ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-01gUNKYLeG2FibyJ .noteText,#mermaid-svg-01gUNKYLeG2FibyJ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-01gUNKYLeG2FibyJ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-01gUNKYLeG2FibyJ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-01gUNKYLeG2FibyJ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-01gUNKYLeG2FibyJ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ .section{stroke:none;opacity:0.2}#mermaid-svg-01gUNKYLeG2FibyJ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-01gUNKYLeG2FibyJ .section2{fill:#fff400}#mermaid-svg-01gUNKYLeG2FibyJ .section1,#mermaid-svg-01gUNKYLeG2FibyJ .section3{fill:#fff;opacity:0.2}#mermaid-svg-01gUNKYLeG2FibyJ .sectionTitle0{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .sectionTitle1{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .sectionTitle2{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .sectionTitle3{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-01gUNKYLeG2FibyJ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ .grid path{stroke-width:0}#mermaid-svg-01gUNKYLeG2FibyJ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-01gUNKYLeG2FibyJ .task{stroke-width:2}#mermaid-svg-01gUNKYLeG2FibyJ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ .taskText:not([font-size]){font-size:11px}#mermaid-svg-01gUNKYLeG2FibyJ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-01gUNKYLeG2FibyJ .task.clickable{cursor:pointer}#mermaid-svg-01gUNKYLeG2FibyJ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-01gUNKYLeG2FibyJ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-01gUNKYLeG2FibyJ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-01gUNKYLeG2FibyJ .taskText0,#mermaid-svg-01gUNKYLeG2FibyJ .taskText1,#mermaid-svg-01gUNKYLeG2FibyJ .taskText2,#mermaid-svg-01gUNKYLeG2FibyJ .taskText3{fill:#fff}#mermaid-svg-01gUNKYLeG2FibyJ .task0,#mermaid-svg-01gUNKYLeG2FibyJ .task1,#mermaid-svg-01gUNKYLeG2FibyJ .task2,#mermaid-svg-01gUNKYLeG2FibyJ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-01gUNKYLeG2FibyJ .taskTextOutside0,#mermaid-svg-01gUNKYLeG2FibyJ .taskTextOutside2{fill:#000}#mermaid-svg-01gUNKYLeG2FibyJ .taskTextOutside1,#mermaid-svg-01gUNKYLeG2FibyJ .taskTextOutside3{fill:#000}#mermaid-svg-01gUNKYLeG2FibyJ .active0,#mermaid-svg-01gUNKYLeG2FibyJ .active1,#mermaid-svg-01gUNKYLeG2FibyJ .active2,#mermaid-svg-01gUNKYLeG2FibyJ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-01gUNKYLeG2FibyJ .activeText0,#mermaid-svg-01gUNKYLeG2FibyJ .activeText1,#mermaid-svg-01gUNKYLeG2FibyJ .activeText2,#mermaid-svg-01gUNKYLeG2FibyJ .activeText3{fill:#000 !important}#mermaid-svg-01gUNKYLeG2FibyJ .done0,#mermaid-svg-01gUNKYLeG2FibyJ .done1,#mermaid-svg-01gUNKYLeG2FibyJ .done2,#mermaid-svg-01gUNKYLeG2FibyJ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-01gUNKYLeG2FibyJ .doneText0,#mermaid-svg-01gUNKYLeG2FibyJ .doneText1,#mermaid-svg-01gUNKYLeG2FibyJ .doneText2,#mermaid-svg-01gUNKYLeG2FibyJ .doneText3{fill:#000 !important}#mermaid-svg-01gUNKYLeG2FibyJ .crit0,#mermaid-svg-01gUNKYLeG2FibyJ .crit1,#mermaid-svg-01gUNKYLeG2FibyJ .crit2,#mermaid-svg-01gUNKYLeG2FibyJ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-01gUNKYLeG2FibyJ .activeCrit0,#mermaid-svg-01gUNKYLeG2FibyJ .activeCrit1,#mermaid-svg-01gUNKYLeG2FibyJ .activeCrit2,#mermaid-svg-01gUNKYLeG2FibyJ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-01gUNKYLeG2FibyJ .doneCrit0,#mermaid-svg-01gUNKYLeG2FibyJ .doneCrit1,#mermaid-svg-01gUNKYLeG2FibyJ .doneCrit2,#mermaid-svg-01gUNKYLeG2FibyJ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-01gUNKYLeG2FibyJ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-01gUNKYLeG2FibyJ .milestoneText{font-style:italic}#mermaid-svg-01gUNKYLeG2FibyJ .doneCritText0,#mermaid-svg-01gUNKYLeG2FibyJ .doneCritText1,#mermaid-svg-01gUNKYLeG2FibyJ .doneCritText2,#mermaid-svg-01gUNKYLeG2FibyJ .doneCritText3{fill:#000 !important}#mermaid-svg-01gUNKYLeG2FibyJ .activeCritText0,#mermaid-svg-01gUNKYLeG2FibyJ .activeCritText1,#mermaid-svg-01gUNKYLeG2FibyJ .activeCritText2,#mermaid-svg-01gUNKYLeG2FibyJ .activeCritText3{fill:#000 !important}#mermaid-svg-01gUNKYLeG2FibyJ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-01gUNKYLeG2FibyJ g.classGroup text .title{font-weight:bolder}#mermaid-svg-01gUNKYLeG2FibyJ g.clickable{cursor:pointer}#mermaid-svg-01gUNKYLeG2FibyJ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-01gUNKYLeG2FibyJ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-01gUNKYLeG2FibyJ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-01gUNKYLeG2FibyJ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-01gUNKYLeG2FibyJ .dashed-line{stroke-dasharray:3}#mermaid-svg-01gUNKYLeG2FibyJ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ .commit-id,#mermaid-svg-01gUNKYLeG2FibyJ .commit-msg,#mermaid-svg-01gUNKYLeG2FibyJ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-01gUNKYLeG2FibyJ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-01gUNKYLeG2FibyJ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-01gUNKYLeG2FibyJ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-01gUNKYLeG2FibyJ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-01gUNKYLeG2FibyJ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-01gUNKYLeG2FibyJ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-01gUNKYLeG2FibyJ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-01gUNKYLeG2FibyJ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-01gUNKYLeG2FibyJ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-01gUNKYLeG2FibyJ .edgeLabel text{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-01gUNKYLeG2FibyJ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-01gUNKYLeG2FibyJ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-01gUNKYLeG2FibyJ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-01gUNKYLeG2FibyJ .note-edge{stroke-dasharray:5}#mermaid-svg-01gUNKYLeG2FibyJ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-01gUNKYLeG2FibyJ .error-icon{fill:#522}#mermaid-svg-01gUNKYLeG2FibyJ .error-text{fill:#522;stroke:#522}#mermaid-svg-01gUNKYLeG2FibyJ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-01gUNKYLeG2FibyJ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-01gUNKYLeG2FibyJ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-01gUNKYLeG2FibyJ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-01gUNKYLeG2FibyJ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-01gUNKYLeG2FibyJ .marker{fill:#333}#mermaid-svg-01gUNKYLeG2FibyJ .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-01gUNKYLeG2FibyJ {color: rgba(0, 0, 0, 0.75);font: ;}

Service A - traceId, spanIdA
Service B - traceId, spanIdA1
Service C - traceId, spanIdA12
Service D - traceId, spanIdA11

Span ID 如果要计算时间延迟,可以通过 spanIdA11 - spanIdA1

8.1.2 Zipkin 是什么

  • Zipkin 的基础概念

    • Zipkin 解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现
    • Zipkin 有四大核心组件构成
      1. Collector:收集器组件
      2. Storge:存储组件
      3. API:RESTFul API,提供外部访问接口
      4. UI:Web UI,提供可视化查询页面

8.2.1 集成 SpringCloud Sleuth 实现微服务通信跟踪

8.2.1.1 集成步骤

  • 保证服务与服务之间存在跨进程通信
  • Maven 依赖

8.2.1.2 编写测试代码

sca-commerce-gateway 与 sca-commerce-alibaba-nacos-client 添加 Maven 依赖

<!-- 通过 Sleuth 实现链路跟踪 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

sca-commerce-alibaba-nacos-client 编写测试业务代码与控制层代码

SleuthTraceInfoService

package com.edcode.commerce.service;import brave.Tracer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;/*** @author eddie.lee* @blog blog.eddilee.cn* @description 使用代码更直观的看到 Sleuth 生成的相关跟踪信息*/
@Slf4j
@Service
@RequiredArgsConstructor
public class SleuthTraceInfoService {/** brave.Tracer 跟踪对象 */private final Tracer tracer;/*** 打印当前的跟踪信息到日志中*/public void logCurrentTraceInfo() {log.info("Sleuth trace id: [{}]", tracer.currentSpan().context().traceId());log.info("Sleuth span id: [{}]", tracer.currentSpan().context().spanId());}
}

SleuthTraceInfoController

package com.edcode.commerce.controller;import com.edcode.commerce.service.SleuthTraceInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author eddie.lee* @blog blog.eddilee.cn* @description 打印跟踪信息*/
@Slf4j
@RestController
@RequestMapping("/sleuth")
@RequiredArgsConstructor
public class SleuthTraceInfoController {private final SleuthTraceInfoService traceInfoService;/*** 打印日志跟踪信息*/@GetMapping("/trace-info")public void logCurrentTraceInfo() {traceInfoService.logCurrentTraceInfo();}
}

8.2.1.2 测试请求与查看控制台日志

发起请求

### 查看 Sleuth 跟踪信息
GET http://127.0.0.1:9001/edcode/scacommerce-nacos-client/sleuth/trace-info
Accept: application/json
sca-commerce-user: eyJhbGciOiJSUzI1NiJ9.eyJzY2EtY29tbWVyY2UtdXNlciI6IntcImlkXCI6MTEsXCJ1c2VybmFtZVwiOlwiZWRkaWVAcXEuY29tXCJ9IiwianRpIjoiZjQ3M2NhZjctY2RjMi00ZmE4LWExNzQtZjZhYmQ5ZDFjMzAzIiwiZXhwIjoxNjM1ODY4ODAwfQ.iTtQE2gHzjPxVP5SEFHrDBkvrzI-yt6oy-w1x--Q3ahhTvYLTiYnvndtIx7IIyYipr_ayZnAQyluPt3oiLaS80r9qByaN3zQF-6gBW_wu_fd0yd89hIjPnQeP1mY2NcchV2FaMUW7Jlq8CUDPurEhW4GUDXOqBXgmxai5UTu4yoXBUfyXUXznKTx697cGo5aoVKTAKvMReJg-77n5sQuafZNDu6pz2D1KMvEucNyZtbXw0JRIl1CsK777Jt3IG1bnOnwRBt8o1tkodZ3zJbfgTGVCHJmfEuUnXwdf4DLAq568pNVvylPLh4_r-UUGGxE6Az9XwOtl1w4vzK1M2ATzw
token: edcode

响应信息

GET http://127.0.0.1:9001/edcode/scacommerce-nacos-client/sleuth/trace-infoHTTP/1.1 200 OK
transfer-encoding: chunked
Content-Type: application/json
Date: Tue, 02 Nov 2021 13:04:55 GMT{"code": 0,"message": "","data": null
}Response code: 200 (OK); Time: 1160ms; Content length: 35 bytes

查看日志

sca-commerce-gateway

2021-11-02 21:04:55.332  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true] 1060 --- [ctor-http-nio-2] c.netflix.config.ChainedDynamicProperty  : Flipping property: sca-commerce-nacos-client.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2021-11-02 21:04:55.347  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true] 1060 --- [ctor-http-nio-2] c.netflix.loadbalancer.BaseLoadBalancer  : Client: sca-commerce-nacos-client instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=sca-commerce-nacos-client,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2021-11-02 21:04:55.353  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true] 1060 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2021-11-02 21:04:55.372  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true] 1060 --- [ctor-http-nio-2] c.netflix.config.ChainedDynamicProperty  : Flipping property: sca-commerce-nacos-client.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2021-11-02 21:04:55.374  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true] 1060 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client sca-commerce-nacos-client initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=sca-commerce-nacos-client,current list of Servers=[192.168.3.192:8000],Load balancer stats=Zone stats: {unknown=[Zone:unknown; Instance count:1;   Active connections count: 0;    Circuit breaker tripped count: 0;   Active connections per server: 0.0;]
},Server stats: [[Server:192.168.3.192:8000;    Zone:UNKNOWN;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 08:00:00 CST 1970;  First connection made: Thu Jan 01 08:00:00 CST 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
]}ServerList:com.alibaba.cloud.nacos.ribbon.NacosServerList@72186c8f
2021-11-02 21:04:55.592  INFO [sca-commerce-gateway,353ea734cc43d6ee,353ea734cc43d6ee,true] 1060 --- [ctor-http-nio-2] c.e.c.filter.GlobalElapsedLogFilter      : [/edcode/scacommerce-nacos-client/sleuth/trace-info] elapsed: [1034ms]
2021-11-02 21:04:56.358  INFO [sca-commerce-gateway,,,] 1060 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: sca-commerce-nacos-client.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647

sca-commerce-alibaba-nacos-client

2021-11-02 21:04:55.543  INFO [sca-commerce-nacos-client,353ea734cc43d6ee,c85be2c1bb127558,true] 33740 --- [nio-8000-exec-1] c.e.c.service.SleuthTraceInfoService     : Sleuth trace id: [3836687777773377262]
2021-11-02 21:04:55.543  INFO [sca-commerce-nacos-client,353ea734cc43d6ee,c85be2c1bb127558,true] 33740 --- [nio-8000-exec-1] c.e.c.service.SleuthTraceInfoService     : Sleuth span id: [-4009361721548180136]

解析:[sca-commerce-nacos-client,353ea734cc43d6ee,c85be2c1bb127558,true]
第一行:service name
第二行:trace id
第三行:span id

8.3.1 搭建 Zipkin Server 实现对跟踪信息的收集

8.3.1.1 ZS搭建步骤

  • Tips:SpringCloud Finchley 版本(包含)之后,官方不建议自己搭建 Zipkin-Server,提供了已经打包好的jar文件(SpringBoot工程),直接下载启动即可
  • 下载地址
    • curl -sSL https://zipkin.io/quickstart.sh | bash -s
    1. 选择自己需要的版本即可
    2. 选择 *.exec.jar 结尾的 jar

8.3.1.2 Linux 终端

[root@localhost opt]# curl -sSL https://zipkin.io/quickstart.sh | bash -s
Thank you for trying Zipkin!
This installer is provided as a quick-start helper, so you can try Zipkin out
without a lengthy installation process.Fetching version number of latest io.zipkin:zipkin-server release...
Latest release of io.zipkin:zipkin-server seems to be 2.23.4Downloading io.zipkin:zipkin-server:2.23.4:exec to zipkin.jar...
> curl -fL -o 'zipkin.jar' 'https://repo1.maven.org/maven2/io/zipkin/zipkin-server/2.23.4/zipkin-server-2.23.4-exec.jar'% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100 59.0M  100 59.0M    0     0  34146      0  0:30:14  0:30:14 --:--:-- 33309Verifying checksum...
> curl -fL -o 'zipkin.jar.md5' 'https://repo1.maven.org/maven2/io/zipkin/zipkin-server/2.23.4/zipkin-server-2.23.4-exec.jar.md5'% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100    32  100    32    0     0     17      0  0:00:01  0:00:01 --:--:--    17
> md5sum -c <<< "$(cat zipkin.jar.md5)  zipkin.jar"
zipkin.jar: OK
Checksum for zipkin.jar passes verificationVerifying GPG signature of zipkin.jar...
> curl -fL -o 'zipkin.jar.asc' 'https://repo1.maven.org/maven2/io/zipkin/zipkin-server/2.23.4/zipkin-server-2.23.4-exec.jar.asc'% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100   833  100   833    0     0     91      0  0:00:09  0:00:09 --:--:--   180GPG signing key is not known, skipping signature verification.
Use the following commands to manually verify the signature of zipkin.jar:gpg --keyserver keyserver.ubuntu.com --recv FF31B515# Optionally trust the key via 'gpg --edit-key FF31B515', then typing 'trust',# choosing a trust level, and exiting the interactive GPG session by 'quit'gpg --verify zipkin.jar.asc zipkin.jarYou can now run the downloaded executable jar:java -jar zipkin.jar[root@localhost opt]# nohup java -jar zipkin.jar &
[1] 30238
[root@localhost opt]# nohup: ignoring input and appending output to ‘nohup.out’

8.3.1.3 Zipkin Web UI

访问:IP:9411

8.3.2 配置 Zipkin Server 实现对跟踪信息的收集

  • 配置 ZS

    • 为什么需要对 ZS 做自定义配置?

      1. 默认情况下, ZS 将 跟踪信息存储在内存中(JVM),重启会丢失
      2. ZS 默认使用 HTTP 方式上报跟踪数据,性能较差
    • ZS 配置 MySQL 跟踪数据持久化 (同时它也支持 ES)
      1. MySQL 中添加数据表:https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql-v1/src/main/resources/mysql.sql
      2. ZS 启动指定 MySQL 路径

8.4.1 SpringCloud Sleuth 整合 Zipkin 实现分布式链路跟踪、收集

  • SpringCloud Sleuth 整合 Zipkin 步骤

    • 简单的两个步骤(Zipkin Server 使用 MySQL 实现跟踪数据持久化)

      • Maven 依赖
      • bootstrap.yml 中增加 Zipkin 的配置
  • 下载、安装 Kafka

    • 下载 Kafka:https://kafka.apache.org/quickstart
    • 解压、启动 ZK 和 Kafka Server 即可 (使用默认配置)

8.4.1.1 下载与解压 Kafka

Downloads

https://kafka.apache.org/downloads

Linux 步骤

[root@localhost opt]# wget https://dlcdn.apache.org/kafka/3.0.0/kafka_2.13-3.0.0.tgz
[root@localhost opt]# tar -zxf kafka_2.13-3.0.0.tgz
[root@localhost opt]# ls -la | grep kafka
drwxr-xr-x.  8 root root       134 Nov  3 01:48 kafka_2.13-3.0.0
-rw-r--r--.  1 root root  86396520 Sep 20 04:46 kafka_2.13-3.0.0.tgz

8.4.1.2 启动 zookeeper 与 Kafka

对外访问需要修改 Kafka 的 server.properties

找到 advertised.listeners 或者 自行添加 advertised.listeners 指定 IP 地址

[root@localhost kafka_2.13-3.0.0]# vim /opt/kafka_2.13-3.0.0/config/server.propertiesadvertised.listeners=PLAINTEXT://192.168.3.250:9092
后台启动 zookeeper
[root@localhost kafka_2.13-3.0.0]# nohup /opt/kafka_2.13-3.0.0/bin/zookeeper-server-start.sh config/zookeeper.properties &
[1] 31998
[root@localhost kafka_2.13-3.0.0]# nohup: ignoring input and appending output to ‘nohup.out’
后台启动 kafka
[root@localhost kafka_2.13-3.0.0]# nohup /opt/kafka_2.13-3.0.0/bin/kafka-server-start.sh config/server.properties &
[1] 32574
[root@localhost kafka_2.13-3.0.0]# nohup: ignoring input and appending output to ‘nohup.out’

8.4.1.3 运行 ZipKin 关联 Kafka 与 MySQL

[root@localhost opt]# nohup java -DKAFKA_BOOTSTRAP_SERVERS=127.0.0.1:9092 -jar zipkin.jar --STORAGE_TYPE=mysql --MYSQL_USER=root --MYSQL_PASS=123456 --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin &
[1] 601
[root@localhost opt]# nohup: ignoring input and appending output to ‘nohup.out’

连接kafka:-DKAFKA_BOOTSTRAP_SERVERS=127.0.0.1:9092

连接MySQL:–STORAGE_TYPE=mysql --MYSQL_USER=root --MYSQL_PASS=123456 --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin

8.4.1.4 检查 Linux 服务是否启动

[root@localhost opt]# ps -aux | grep -E 'nacos|zipkin|kafka|zookeeper'
[root@localhost opt]# netstat -ltnp | grep -E '8848|9092|9411|2181'
tcp6       0      0 :::8848                 :::*                    LISTEN      1932/java
tcp6       0      0 :::9411                 :::*                    LISTEN      601/java
tcp6       0      0 :::9092                 :::*                    LISTEN      32574/java
tcp6       0      0 :::2181                 :::*                    LISTEN      31998/java

8.4.1.4 IDEA 启动服务与测试发起请求

Maven 依赖 (zipkin 与 kafka)

<!-- 通过 Sleuth 实现链路跟踪 -->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-sleuth</artifactId>-->
<!--        </dependency>-->
<!-- zipkin = spring-cloud-starter-sleuth + spring-cloud-sleuth-zipkin-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>2.5.0.RELEASE</version>
</dependency>

修改 Gateway 与 Nacos-Client 服务的配置文件

spring:kafka:bootstrap-servers: ${KAFKA_SERVER:127.0.0.1}:${KAFKA_PORT:9092}producer:retries: 3consumer:auto-offset-reset: latestzipkin:sender:type: ${ZIPKIN_KAFKA_SENDER:web} # 默认是 webbase-url: http://${ZIPKIN_URL:localhost}:${ZIPKIN_PORT:9411}/

两个服务都需要添加 kafka 与 zipkin 的连接信息

启动服务

  • 启动以下服务

    • AuthorityCenterApplication :7000/ # 如果 token 过期,需要重新签发
    • NacosClientApplication :8000/
    • GatewayApplication :9001/

请求测试

sca-commerce-gateway/src/main/resources/http/nacos-client.http

### 查询服务
GET http://127.0.0.1:9001/edcode/scacommerce-nacos-client/nacos-client/service-instance?serviceId=sca-commerce-gateway
Accept: application/json
sca-commerce-user: eyJhbGciOiJSUzI1NiJ9.eyJzY2EtY29tbWVyY2UtdXNlciI6IntcImlkXCI6MTEsXCJ1c2VybmFtZVwiOlwiZWRkaWVAcXEuY29tXCJ9IiwianRpIjoiMWU1MGI2ZWYtNmUzOS00YmY2LWJlMjktZDc4NWU3NWQyNmY1IiwiZXhwIjoxNjM1OTU1MjAwfQ.P7GxZuMUrgiMUbD4dNYzQiV3A6YkaFpvlzg8cpBdu_hvxqDsVEuuiYODQSzZPQeN3xTQPbJ70zkSY084HV7Vsk929en1lqNiX_dpQEuGSbz2JSPqyJuLZ6v7hRX9GI32sPrZAnaKVXMdeHUXCMMmaS1L3osimSvAlaoDE0n2UukDLgu83xRlL3bddHIJbmFD5BrV6Y-u9d-blqXPOpxFEYkdwS_XrljYiULTH7Olr71TAwODUPdttnmVhHPXB0_dnOG5DZMOC0OxqokHGZJ7CC86paE4TvdNPwqotB6u6zh_d_YCCBWM3t1LmKYB6E_bnz2taL5Q4AYHlRaZZotaAA
token: edcode ## HeaderTokenGatewayFilter###

8.4.1.5 Zipkin Web UI

如何简单的使用

打开 http://192.168.3.250:9411


默认 all,然后直接查找,会显示所有的请求信息,点击其中一条

就会看到该请求的所有经过哪些服务,耗时多少

同样,也可以在终端拿 trace Id:669b59f38adf2c38 去跟踪链路

trace Id 搜索框:669b59f38adf2c38

如何查看服务之间的依赖关系

点击上方的【依赖】

8.5.1 Spring Cloud Sleuth 设置采样率、抽样收集策略

8.5.1.1 Spring Cloud Sleuth 采样收集

  • 收集跟踪信息是一把双刃剑,需要做好权衡

    • 收集的跟踪信息越多,越能反映出系统的实际运行情况
    • 高并发场景下,大量的请求调用会产生海量的跟踪日志信息,性能开销太大

开发与测试环境可以使用高的采样率,但是生产环境建议不要这么做。

  • 可以自由选择 Zipkin brave 自带的两个抽样策略

    • ProbabilityBasedSampler 采样率策略

      • 默认使用的策略,以请求百分比 的方式配置和手机跟踪信息:它的默认值为 0.1,代表手机 10% 的请求跟踪信息
      • spring.sleuth.sampler.probability=0.5
    • RateLimitingSampler 抽样策略
      • 限速采集,也就是说它可以用来限制每秒追踪请求的最大数量,优先级更高
      • spring.sleuth.sampler.rate=10 ## 一秒最大只有10个跟踪策略给采集

8.5.1.2 bootstrap.yml 配置 Sleuth

sca-commerce-alibaba-nacos-client

spring:sleuth:sampler:# RateLimitingSampler 抽样策略,设置了限速采样,spring.sleuth.sampler.probability 属性值无效rate: 100 # 每秒间隔接受的 trace 量# Probability 抽样策略probability: 1.0 # 采样比例,1.0 表示 100%, 默认:0.1

8.5.1.3 代码配置 Sleuth

package com.edcode.commerce.sampler;import brave.sampler.RateLimitingSampler;
import brave.sampler.Sampler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author eddie.lee* @blog blog.eddilee.cn* @description 使用配置的方式设定抽样率 (二选一)*/
@Configuration
public class SamplerConfig {/*** 限速采集(推荐)*/@Beanpublic Sampler sampler() {return RateLimitingSampler.create(100);}//    /**
//     * 概率采集, 默认的采样策略, 默认值是 0.1
//     */
//    @Bean
//    public Sampler defaultSampler() {//        return ProbabilityBasedSampler.create(0.5f);
//    }
}

代码Yaml文件配置是二选一,方便简洁肯定是 Yaml配置

限速采集概率采集无论是代码还是 Yaml文件 都是二选一

8.6.1 SpringCloud Sleuth+Zipkin 分布式日志追踪总结

8.6.1.1 SpringCloud Sleuth+Zipkin 逻辑架构图

  • 跟踪、收集所涉及的三个组件(模块)Sleuth、Zipkin、Brave
  • 三个组件之间的关系
    • Brave 是一个 tracer 库,提供的是 tracer 接口
    • Sleuth 采用了 Brave 作为 tracer 库
    • Sleuth 可以不使用 Zipkin

8.6.1.2 Brave 解读

  • Brave 的两个最基本、也是最核心的概念

    • trace:以看作是一个逻辑执行过程中的整个链条 (可以看作一棵树)
    • span:是 trace 跟踪的基本单位
  • Brave 中常用的数据结构以及说明

    • Tracing:工具类,用于生成 Tracer 类实例
    • Tracer:也是工具类,用于生成 Span
    • Span:实际记录每个功能块执行信息的类
    • TraceContext:记录 trace 的执行过程中的元数据信息类
    • Propagation:用于在分布式环境或者跨进程条件下的 trace 跟踪时实现 TraceContext 传递的工具类

8.6.1.2 SpringCloud Sleuth 如何实现跨服务Trace 追踪

  • SpringCloud Sleuth 实现跨服务 Trace 追踪

    • SpringCloud Sleuth 和 Brave 提供了很多不同的分布式框架的支持,例如 gRPC、Kafka、HTTP等

【Spring Cloud Alibaba 温故而知新】(五)SpringCloud Sleuth + Zipkin:分布式日志追踪相关推荐

  1. Spring Cloud Alibaba 高级特性 基于 Sleuth+Zipkin 实施链路跟踪体系

    介绍微服务链路追踪的原理: 讲解基于 Spring Cloud Sleuth 实现链路追踪: 构建 Zipkin Server 实现链路追踪的可视化管理. 下面咱们先来介绍什么是微服务链路追踪. 微服 ...

  2. Spring Cloud 5分钟搭建教程(附上一个分布式日志系统项目作为参考) - 推荐

    http://blog.csdn.net/lc0817/article/details/53266212/ https://github.com/leoChaoGlut/log-sys 上面是我基于S ...

  3. Spring Cloud 5分钟搭建教程(附上一个分布式日志系统项目作为参考)

    Spring Cloud 5分钟搭建教程(附上一个分布式日志系统项目作为参考) 上面是我基于Spring Cloud ,Spring Boot 和 Docker 搭建的一个分布式日志系统. 目前已在我 ...

  4. spring cloud alibaba + tx-lcn 实现微服务的分布式事务管理

    Spring Cloud Alibaba官方文档:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md Sp ...

  5. 1:微服务和Spring Cloud Alibaba介绍

    Spring Cloud Alibaba系列目录 提示:这里是第一章:微服务和Spring Cloud Alibaba介绍 微服务和Spring Cloud Alibaba介绍 Alibaba微服务组 ...

  6. 一、微服务和Spring Cloud Alibaba介绍

    微服务和Spring Cloud Alibaba介绍 1.微服务介绍 1.1 系统架构演变 随着互联网的发展,网站应用的规模也在不断的扩大,进而导致系统架构也在不断的进行变化. 从互联网早起到现在,系 ...

  7. 从 Netflix 到 Spring Cloud Alibaba 差距不知一点点

    Spring Cloud 自 2015 年 3 月推出之后,很快就在 Java 微服务生态中,成为开发人员的首选技术栈. Spring Cloud 在 Spring Boot 的基础上,保留 Java ...

  8. Spring Cloud Alibaba(13)---Sleuth概述

    Spring Cloud Alibaba(13)-Sleuth概述 Sleuth概述 前言 在微服务架构中,众多的微服务之间互相调用,如何清晰地记录服务的调用链路是一个需要解决的问题.同时,由于各种原 ...

  9. Spring Cloud Alibaba - 02 SpringCloud 、 SpringCloud Alibaba 、SpringBoot的生产版本选择

    文章目录 Spring Cloud 生态圈概览 Spring Cloud Spring Cloud Alibaba 版本选择 Spring Boot 版本说明 Spring Cloud 版本说明 Sp ...

最新文章

  1. P3168 [CQOI2015]任务查询系统 差分+主席树
  2. 程序员福音-定时提醒bat
  3. 数据结构链表——JavaScript的实现
  4. 灰色简约三级CSS下拉菜单代码
  5. Oracle入门(十二)之SQL的DDL
  6. 第一次创建springboot框架项目
  7. 安卓+4.0.4+java模拟器_Android 4.0.4模拟器安装完全教程(图文)
  8. 文件管理搜不到Android 里的文件,Android:在原始文件夹中添加文件后窗口找不到内容容器视图...
  9. CodeForces 682E Alyona and Triangles (计算几何)
  10. Mac VM 虚拟机固定IP
  11. 访问共享服务器的没有权限文件夹权限设置,访问共享文件夹的时候提示没有权限访问网络资源怎么解决...
  12. 集合论中关系矩阵的布尔乘法运算与优化
  13. MySQL传统无损同步
  14. 微信上传图片没反应_365微信编辑器图片上传不了怎么办?
  15. 视频中的声音如何处理,如何添加背景音乐?
  16. qq音乐 (轮播图) 详细介绍
  17. Tensorflow2.x框架-神经网络实现鸢尾花分类
  18. 阿里云域名购买至备案流程
  19. 【格灵深瞳】电话面试
  20. c语言除法留小数点两位小数,高精度除法小数点位数

热门文章

  1. 物流手持PDA,无线巴枪
  2. 【C++】洛谷P1873 [COCI 2011/2012 #5] EKO / 砍树
  3. 中止执行后超过2年_申请执行超过2年时效,法院还处理吗?
  4. 计算机软件技术有哪些,1.1 什么是计算机软件技术
  5. Freesurfer的简单使用和一些基础知识
  6. Elastic认证特训营 难点解读04——集群问题排查实战指南
  7. 记一次完整的用户测试
  8. WiFi基本概念(八)(信道估计 L-STF,L-LTF,Pilots)
  9. 那些与三维激光扫描有关的建模
  10. 洛谷P3387 【模板】缩点 (tarjan算法)