SpringCloud Alibaba 核心组件解析:服务链路追踪

SpringCloud Alibaba 核心组件解析:服务链路追踪

技术栈 :Spring Boot 3.2.0 + Spring Cloud Alibaba 2023.0.0.0-RC1 + Micrometer Tracing + Zipkin


5.1 是什么 --- Alibaba 侧的链路追踪

Spring Cloud Alibaba 没有自己独立的链路追踪组件 ,而是直接复用 Spring Cloud 官方的 Micrometer Tracing + Zipkin 方案。

Sentinel 的 Dashboard 提供流量监控 ,但不提供链路追踪(看不到 TraceId 维度的跨服务调用链)。


5.2 为什么 --- 与官方方案的关系

Spring Cloud 官方 Spring Cloud Alibaba
追踪框架 Micrometer Tracing Micrometer Tracing(相同)
传输协议 Zipkin Brave Zipkin Brave(相同)
注册中心 Consul Nacos
流量控制 Resilience4J Sentinel

📌 链路追踪是注册中心无关的:无论 Nacos 还是 Consul,TraceId 都通过 HTTP Header 传播,互不影响。


5.3 怎么做 --- 快速配置

5.3.1 依赖(6 个,由父 POM 统一管理)

xml 复制代码
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-observation</artifactId>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-micrometer</artifactId>
</dependency>
<dependency>
    <groupId>io.zipkin.reporter2</groupId>
    <artifactId>zipkin-reporter-brave</artifactId>
</dependency>

5.3.2 配置

yaml 复制代码
management:
  zipkin:
    tracing:
      endpoint: http://localhost:9411/api/v2/spans  # Zipkin Server
  tracing:
    sampling:
      probability: 1.0   # 开发=1.0,生产=0.1

5.3.3 启动 Zipkin

bash 复制代码
docker run -d -p 9411:9411 openzipkin/zipkin
# 访问 http://localhost:9411

5.4 Nacos + Sentinel + Micrometer 三角关系

复制代码
Nacos(注册发现)
  ├→ 服务启动时注册 IP:Port
  └→ Consumer 通过服务名发现 Provider 地址

Sentinel(流量控制)
  ├→ 根据 QPS/异常 限流和熔断
  └→ 不关心 TraceId,只管流量

Micrometer(链路追踪)
  ├→ 每次请求生成 TraceId → Span
  ├→ 通过 HTTP Header(traceparent)传播
  └→ 异步上报到 Zipkin Server

三者各司其职,互不干扰。Feign 调用时:从 Nacos 获取地址 → Sentinel 判断是否限流 → Micrometer 记录 Span → 发送 HTTP 请求。


5.5 Sleuth → Micrometer 迁移要点

Old (Sleuth) New (Micrometer)
Spring Boot 版本 2.x 3.x
配置前缀 spring.sleuth.* management.tracing.*
日志 MDC 自动注入 需手动配置
Feign 集成 spring-cloud-sleuth feign-micrometer

5.6 面试题

Q:Sleuth 和 Micrometer Tracing 的核心区别?

:Sleuth 用于 Spring Boot 2.x,已停止维护;Micrometer Tracing 是官方继任者,基于 Observation API 统一了 metrics/tracing/logging 三方面。配置前缀从 spring.sleuth.* 变为 management.tracing.*


5.7 踩坑指南

说明
🔴 生产全量采样 probability: 1.0 会产生海量 Span 数据,Zipkin 内存爆炸
🔴 Feign 不传播 TraceId 缺少 feign-micrometer 依赖
🔴 找不到 Zipkin Server 确认 Docker 容器运行中,9411 端口未被占用

5.8 章节总结

  • Alibaba 体系无独立链路追踪组件,直接使用 Micrometer Tracing + Zipkin
  • 完整依赖列表和配置见 Spring Cloud 官方第 5 章
  • Sentinel Dashboard 是流量监控,不是链路追踪,两者互补
相关推荐
码事漫谈6 小时前
类型即世界:一个 C++ 程序员的本体论入门
后端
Rain的Java大神之路6 小时前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
自动化监测Learner6 小时前
Navicat Premium 17 中文版安装教程(2026最新版)
java·linux·数据库
那个松鼠很眼熟w6 小时前
4.Spring-Ai入门案例
java·人工智能·spring
计算机毕设定制辅导-无忧学长6 小时前
《基于SpringBoot青年公寓出租管理系统的设计与实现》
java·vue.js·spring boot·青年公寓出租管理系统
泡海椒7 小时前
JQuick-Curl 二次开发:自定义解析器、扩展点开发指南,如何把框架能力真正变成团队资产
java·开发语言·okhttp·maven
MetaLite8 小时前
AI编程与工程底座-让AI遵守SpringBoot边界
人工智能·spring boot·ai编程
青山木8 小时前
Hot 100 --- 数组中的第K个最大元素
java·数据结构·算法·排序算法
玛卡巴卡ldf8 小时前
【AICoding】笔试提示词设计思路
java·算法·ai编程
IT_陈寒8 小时前
Python的多线程居然是个假把式?搞清GIL让我少熬三天夜
前端·人工智能·后端