微服務(wù)請求存在的問題
微服務(wù)跳來跳去,追蹤難,排查難
微服務(wù)調(diào)?鏈路出現(xiàn)了問題怎么快速排查
微服務(wù)調(diào)?鏈路耗時(shí)?怎么定位是哪個(gè)服務(wù)
鏈路追蹤系統(tǒng)解決的問題
分布式應(yīng)?架構(gòu)雖然滿?了應(yīng)?橫向擴(kuò)展的需求,但是運(yùn)維和診斷的過程變得越來越復(fù)雜,例如會(huì)遇到接?診
斷困難、應(yīng)?性能診斷復(fù)雜、架構(gòu)分析復(fù)雜等難題,傳統(tǒng)的監(jiān)控?具并?法滿?,分布式鏈路系統(tǒng)由此誕?
核?:將?次請求分布式調(diào)?,使?GPS定位串起來,記錄每個(gè)調(diào)?的耗時(shí)、性能等?志,并通過可視化?具展示出來。
常用的鏈路追蹤技術(shù)棧
主要包括以下幾種,結(jié)合其核心特點(diǎn)與應(yīng)用場景進(jìn)行對比分析
Spring Cloud Sleuth + Zipkin
核心功能
Sleuth:作為Spring Cloud生態(tài)的原生組件,負(fù)責(zé)為分布式請求生成唯一的追蹤標(biāo)識(shí)(Trace ID和Span ID),并將這些標(biāo)識(shí)通過日志或HTTP頭傳播到各微服務(wù),實(shí)現(xiàn)調(diào)用鏈路的透明化跟蹤
Zipkin:提供數(shù)據(jù)收集、存儲(chǔ)、分析和可視化功能,接收Sleuth發(fā)送的追蹤數(shù)據(jù),展示完整的調(diào)用鏈路圖,支持性能瓶頸定位和故障排查
集成方式
- 依賴配置:添加spring-cloud-starter-sleuth和spring-cloud-sleuth-zipkin依賴,配置Zipkin服務(wù)器地址即可
- 數(shù)據(jù)采樣:通過spring.sleuth.sampler.probability設(shè)置采樣率(如1.0表示全量采集)
適用場景
- 需要與Spring Cloud深度集成的項(xiàng)目;
- 快速搭建基礎(chǔ)鏈路追蹤能力,適合中小型微服務(wù)系統(tǒng)
Apache SkyWalking
核心功能
- 探針(Agent):無侵入式部署,支持Java、.NET、Node.js等多語言,自動(dòng)采集服務(wù)間調(diào)用、數(shù)據(jù)庫訪問等性能數(shù)據(jù)
- OAP服務(wù):負(fù)責(zé)數(shù)據(jù)聚合、分析和存儲(chǔ),支持Elasticsearch、MySQL等多種存儲(chǔ)后端
- 界面:提供拓?fù)鋱D、調(diào)用鏈詳情、服務(wù)性能指標(biāo)(如響應(yīng)時(shí)間、吞吐量)的可視化
優(yōu)勢
- 低侵入性:僅需通過探針注入,無需修改業(yè)務(wù)代碼;
- 多維度監(jiān)控:支持服務(wù)網(wǎng)格、日志關(guān)聯(lián)、告警功能,適用于復(fù)雜分布式系統(tǒng)
適用場景
- 多語言混合技術(shù)棧的大型微服務(wù)架構(gòu);
- 需要一體化監(jiān)控(APM)與告警能力的場景
阿里云鷹眼
- 阿里云自研的分布式追蹤系統(tǒng),深度集成于阿里云生態(tài),適合云原生環(huán)境,提供商業(yè)化支持
Jaeger
- CNCF孵化的開源項(xiàng)目,支持OpenTracing標(biāo)準(zhǔn),適合云原生環(huán)境,與Kubernetes生態(tài)兼容性較好
技術(shù)選型對比
| 技術(shù)棧 | 侵入性 | 多語言支持 | 可視化能力 | 適用規(guī)模 |
|---|---|---|---|---|
| Sleuth + Zipkin | 低(需依賴) | 僅Java | 基礎(chǔ)鏈路圖 | 中小型Spring Cloud項(xiàng)目 |
| Apache SkyWalking | 無 | 廣泛 | 豐富(拓?fù)?、指?biāo)) | 大型復(fù)雜系統(tǒng) |
| 阿里云鷹眼 | 無 | 有限 | 商業(yè)化高級功能 | 阿里云生態(tài)項(xiàng)目 |
實(shí)踐建議
- 輕量級需求:優(yōu)先選擇 Sleuth + Zipkin,配置簡單且與Spring Cloud無縫集成
- 企業(yè)級監(jiān)控:采用 SkyWalking,尤其適用于多語言、高并發(fā)的分布式系統(tǒng)
- 云原生環(huán)境:結(jié)合 Jaeger 或云廠商提供的追蹤服務(wù)(如阿里鷹眼),利用其與容器化部署的兼容性
什么是Sleuth
?個(gè)組件,專??于記錄鏈路數(shù)據(jù)的開源組件。
?檔:https://spring.io/projects/spring-cloud-sleuth
什么是zipkin
官?
https://zipkin.io/
https://zipkin.io/pages/quickstart.html
?規(guī)模分布式系統(tǒng)的APM?具(Application
Performance Management),基于Google Dapper的
基礎(chǔ)實(shí)現(xiàn),和sleuth結(jié)合可以提供可視化web界?分析
調(diào)?鏈路耗時(shí)情況
使用
spring-cloud-starter-sleuth 在 Spring Cloud 2024.x 中已被 Micrometer Tracing 取代。(就是把sleuth剝離成一個(gè)單獨(dú)的項(xiàng)目)
配置pom
<properties>
<!-- **************************** 分布式核心 **************************** -->
<micrometer-tracing.version>1.3.5</micrometer-tracing.version>
<zipkin-reporter-brave.version>3.4.3</zipkin-reporter-brave.version>
<!-- **************************** 分布式核心 **************************** -->
</properties>
<dependencyManagement>
<dependencies>
......
<!-- **************************** 分布式核心 **************************** -->
<!-- 鏈路追蹤 Micrometer Tracing -->
<!-- 使用 Brave 作為鏈路追蹤實(shí)現(xiàn) -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
<version>${micrometer-tracing.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
<version>${micrometer-tracing.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
<version>${zipkin-reporter-brave.version}</version>
</dependency>
<!-- **************************** 分布式核心 **************************** -->
......
</dependencies>
</dependencyManagement>
配置之前需要將網(wǎng)關(guān)中的一些配置去掉。
online-edu-api-gateway/src/main/java/org/online_edu/filter/UserGlobalFilter.java
//@Component
public class UserGlobalFilter implements GlobalFilter, Ordered {
......
}
online-edu-api-gateway/src/main/resources/application.yml
server:
port: 8888
spring:
application:
name: online-edu-api-gateway
cloud:
nacos:
discovery:
server-addr: xxx.xxx.xxx.xxx:8848
username: nacos
password: xxxxxxxxx
gateway:
routes: #數(shù)組形式
- id: order-service #路由唯?標(biāo)識(shí)
# uri: http://127.0.0.1:8000 #想要轉(zhuǎn)發(fā)到的地址
uri: lb://online-edu-order-service # 從nacos獲取名稱轉(zhuǎn)發(fā),lb是負(fù)載均衡輪訓(xùn)策略
order: 1 #優(yōu)先級,數(shù)字越?優(yōu)先級越?
predicates: #斷? 配置哪個(gè)路徑才轉(zhuǎn)發(fā)
- Path=/order-server/**
# - Before=2025-03-28T01:01:01.000+08:00
# - Query=token
filters: #過濾器,請求在傳遞過程中通過過濾器修改
- StripPrefix=1 #去掉第?層前綴
discovery:
locator:
enabled: true #開啟網(wǎng)關(guān)拉取nacos的服務(wù)
現(xiàn)在可以在訂單服務(wù)中增加鏈路追蹤了。
最后增加子服務(wù)依賴,更新online-edu-order-service/pom.xml。
......
<dependencies>
......
<!-- 鏈路追蹤 Micrometer Tracing -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
</dependencies>
先啟動(dòng)網(wǎng)關(guān)服務(wù)。
在啟動(dòng)訂單服務(wù)。
運(yùn)行接口:

online-edu-order-service: 服務(wù)名稱
abc123def456: traceId(整個(gè)請求鏈路的唯一標(biāo)識(shí))
789xyz: spanId(當(dāng)前操作的標(biāo)識(shí))
服務(wù)器安裝Zipkin
因?yàn)槲已b了docker,直接用dorker安裝。
docker run -d -p 9411:9411 openzipkin/zipkin
這樣只要服務(wù)器放開9411端口,就可以直接訪問到了。
需要追蹤鏈路的微服務(wù)都需要加,網(wǎng)關(guān)服務(wù)必加。

online-edu-api-gateway/src/main/java/org/online_edu/GatewayApplication.java

需要追蹤鏈路的微服務(wù)都需要加,網(wǎng)關(guān)服務(wù)必加。

