聊聊 SpringBoot3 的 Micrometer Tracing
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这是个很尴尬的<span style="color: black;">专题</span>,<span style="color: black;">为何</span>这么说呢?当<span style="color: black;">咱们</span>在讨论卷和造轮子两个事情的时候,SpringBoot 的<span style="color: black;">研发</span>人员<span style="color: black;">亦</span>正在做着<span style="color: black;">一样</span>的事情...</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">回顾</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">之前在 微服务-分布式链路 这篇<span style="color: black;">文案</span>中我介绍了 Dapper、OpenTracing,并以蚂蚁分布式链路组件 SOFATracer 为例,较为<span style="color: black;">仔细</span>的说明了分布式链路组件中的<span style="color: black;">有些</span>技术细节和实现方式,有兴趣的<span style="color: black;">朋友</span><span style="color: black;">能够</span><span style="color: black;">自动</span>查看。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Micrometer Tracing</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">近期</span>关注 SpringBoot3 的发布,在 Production-ready Features 中,首次将 tracing <span style="color: black;">做为</span>一个单独的功能项写进官方文档中;<span style="color: black;">这里</span>之前 SpringBoot 仅<span style="color: black;">供给</span>了 Http Tracing 的能力,并且默认<span style="color: black;">状况</span>下,是在内存中存储 近 100 次请求的记录;在 2.7.x 的官方文档中,Http Tracing 只是<span style="color: black;">意见</span>在<span style="color: black;">研发</span>环境<span style="color: black;">运用</span>,<span style="color: black;">针对</span>生产环境,官方文档<span style="color: black;">亦</span><span style="color: black;">知道</span>的指出,<span style="color: black;">意见</span><span style="color: black;">运用</span> Zipkin 或 Spring Cloud Sleuth 这类比较成熟的可观测性<span style="color: black;">处理</span><span style="color: black;">方法</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从 Dapper 到 OpenTracing,<span style="color: black;">处理</span>了厂商无关和统一链路 API 的问题,从 OpenTracing 到 OpenTelemetry,<span style="color: black;">处理</span>统一可观测性 API 的问题,并且 OpenTelemetry 从 <strong style="color: blue;">2019-5.7</strong>被 CNCF Accepted 之后,<span style="color: black;">亦</span>在<span style="color: black;">持续</span>地孵化和完善。<span style="color: black;">倘若</span>说 SpringBoot 面向 OpenTelemetry <span style="color: black;">供给</span> Tracing,可能更便于接受,<span style="color: black;">然则</span> SpringBoot 却<span style="color: black;">运用</span>了 Micrometer tracing,并且<span style="color: black;">经过</span> Facade 进行了桥接 Brave 和 otel。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>一点是,SpringBoot 将 Tracing 在 v3 系列<span style="color: black;">供给</span>出来比较容易理解,在可观测性上,tracing <span style="color: black;">始终</span>都是 SpringBoot 缺失的,在 SpringCloud 中,它<span style="color: black;">经过</span> Spring Cloud Slueth 整合了 Brave 补缺了这一环。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">比较疑惑的是,SpringBoot <span style="color: black;">供给</span>的 tracing <span style="color: black;">为何</span>会<span style="color: black;">选取</span><span style="color: black;">运用</span>一个新的 facade Micrometer tracing 来实现,这一点我尝试从 issue 中去找答案,但<span style="color: black;">亦</span><span style="color: black;">无</span>找到比较有说服力的,<span style="color: black;">例如</span> Add auto-configuration for Micrometer 2.0 Observation API 和 Add support for Micrometer tracing。<span style="color: black;">不外</span>有意思的是,在 spring.io/projects/sp… <span style="color: black;">这儿</span>给出了<span style="color: black;">有些</span>信息,意思<span style="color: black;">便是</span> Spring Cloud Slueth 将迁移到 Micrometer tracing。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>关于<span style="color: black;">为何</span> SpringBoot <span style="color: black;">运用</span> Micrometer 而不是 OpenTelemetry,可能有以下几点<span style="color: black;">原由</span>(一家之言,欢迎<span style="color: black;">指点</span>):</p>1、在之前的<span style="color: black;">有些</span>版本中,Micrometer 关注更<span style="color: black;">大都是</span> metrics,而 OpenTelemetry 则<span style="color: black;">更加多</span>关注 tracing,<span style="color: black;">然则</span>随着版本的迭代完善,Micrometer 和 OpenTelemetry 在 metrics 和 tracing API 上基本都具备了。重要的是,<span style="color: black;">这里</span>之前,SpringBoot <span style="color: black;">已然</span>对 Micrometer Metrics 进行了支持,<span style="color: black;">所说</span>近水楼台先得月。2、OpenTelemetry 的<span style="color: black;">目的</span>是厂商无关,语言无关,penTelemetry 更适合在异构技术栈中发挥<span style="color: black;">功效</span>;而 Micrometer <span style="color: black;">始终</span><span style="color: black;">败兴</span>都是基于 Java 语言,这与 Spring 体系从根上是一致的。3、Micrometer API 进行<span style="color: black;">海量</span>更改。最重要的变化是引入了一个新的 API:Observation API,<span style="color: black;">这般</span>便于<span style="color: black;">运用</span>者能够<span style="color: black;">运用</span>统一的 API 来观测业务代码,<span style="color: black;">包含</span> metrics, tracing 以及 logging。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面就 SpringBoot3 Tracing 展开聊聊。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">SpringBoot3 Tracing 剖析</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SpringBoot3 在 Spring Boot Actuator 中为 Micrometer Tracing 的依赖性管理和自动配置。Micrometer Tracing 充当了类似日志<span style="color: black;">行业</span>内 slf4j 门面的角色。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Micrometer Tracing API</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Micrometer Tracing 中的<span style="color: black;">关联</span>概念<span style="color: black;">亦</span>是借用 Dapper 的,<span style="color: black;">例如</span> <strong style="color: blue;">Span</strong>,<strong style="color: blue;">Trace</strong> 等,<span style="color: black;">这儿</span>不做<span style="color: black;">太多</span>介绍。总体<span style="color: black;">来讲</span>,Micrometer Tracing <span style="color: black;">包含</span>以下几个部分:</p>核心模块<span style="color: black;">重点</span><span style="color: black;">包括</span> instrumentation SPI用于适配其它 tracing 实现的 bridge 桥接器用于上报 span 数据的 reportor 扩展机制测试模块<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">功能划分和代码模块结构组织是一致的</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/8dab1da3e552413b84d72f2a325c8bf6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815845&x-signature=mtoXSCSeRfiTE55QPVcBmyYOH%2FQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 Micrometer Tracing 中,一个完整的 tracing 大致如下图所示</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/32fbc7db1bc14cb0ab754d642e03f10c~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815845&x-signature=XcOlAflGO2Av211Jt9Xo8ATbnRg%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">bridge 桥接器</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当前版本,Micrometer Tracing 支持两种 Tracers</p><strong style="color: blue;">OpenZipkin Brave</strong><strong style="color: blue;">OpenTelemetry</strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">运用</span>时,你只能<span style="color: black;">选取</span>一个 bridge 桥接实现,<span style="color: black;">倘若</span>在你的 classpath 中<span style="color: black;">包含</span>两个,可能会有<span style="color: black;">有些</span>意想不到的问题,<span style="color: black;">例如</span>重复的 trace <span style="color: black;">或</span> span。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Reporters</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当前版本,Micrometer Tracing 支持两种 Reporters</p><strong style="color: blue;">Tanzu Observability by Wavefront</strong><strong style="color: blue;">OpenZipkin Zipkin</strong>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">怎样</span>和 Brave 集成的</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不管是 brave 还是 otel,它们都是完整的分布式链路<span style="color: black;">处理</span><span style="color: black;">方法</span>,<span style="color: black;">包含</span> API 和 instrumentation;Micrometer Tracing 与 brave、otel 的集成是典型的 接口继承 + <span style="color: black;">拜托</span> 的形式实现的,以 span 为例:</p>import io.micrometer.tracing.Span;
/**
* Brave implementation of a {@link Span}.
*
* @author Marcin Grzejszczak
* @since 1.0.0
*/
public class BraveSpan implements Span {
final brave.Span delegate;
// 省略其它无关代码 ...
}
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这种方式<span style="color: black;">亦</span>是 skywalking、zipkin-brave、sofatracer1.0、jaeger 等组件在<span style="color: black;">初期</span>支持 opentrcing API 的实现方式。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">怎样</span>上报给 zipkin 的</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我在 原理 | 分布式链路跟踪组件 SOFATracer 和 Zipkin 模型转换 这篇<span style="color: black;">文案</span><span style="color: black;">中间商</span>绍过 SOFATracer 是<span style="color: black;">怎样</span>将 span 数据上报给 zipkin 的,它的做法是:<strong style="color: blue;">在 span 结束的时候,<span style="color: black;">经过</span>预留的 reporter 扩展机制,使得用户<span style="color: black;">能够</span><span style="color: black;">经过</span>自定义 reporter 来实现 span 数据的上报</strong>。两个关键点:</p>上报<span style="color: black;">机会</span>:span 结束时上报方式:<span style="color: black;">经过</span>自定义 reporter 上报<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Micrometer Tracing 本身<span style="color: black;">包含</span>了对 span 完整生命周期管理的 API</p>/**
* Starts this span.
* @return this span
*/
Span start();
/**
* Ends the span. The span gets stopped and recorded if not noop.
*/
void end();
/**
* Ends the span. The span gets stopped and recorded if not noop.
* @param time timestamp
* @param timeUnit time unit of the timestamp
*/
void end(long time, TimeUnit timeUnit);
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此呢</span>上报<span style="color: black;">机会</span>都是<span style="color: black;">同样</span>的,在 span 结束时<span style="color: black;">出现</span>。上报方式,Brave <span style="color: black;">供给</span>了一个 SpanHandler 扩展接口,对应的<span style="color: black;">详细</span>实现是 ZipkinSpanHandler,otel 中<span style="color: black;">供给</span>的是 SpanExporter 扩展接口,对应的实现是 ZipkinSpanExporter。下面用一张简单的图描述下</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/e67472023ddd48cb9095ea2a9b09df73~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815845&x-signature=2pMlkOo1H0AWmVUB8x4izmaVfKo%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">a simple guides for you</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>给一个小案例,<span style="color: black;">重点</span>是官方文档中并<span style="color: black;">无</span><span style="color: black;">供给</span> step by step 的集成方式。以 Micrometer tacing + brave + zipkin 的方式为例。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">依赖</h1><span style="color: black;">增多</span>依赖,下面给出的依赖均为必须<span style="color: black;">供给</span>的<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-actuator</artifactId>
</dependency>
<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.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">供给</span>一个测试 REST API</h1>@RequestMapping("api")
@RestController()
public class MyController {
private static final Log logger = LogFactory.getLog(MyController.class);
@RequestMapping("test")
public String test() {
// 配置了在日志中输出 traceId
logger.info("this is test log ....");
return "SUCCESS";
}
}
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">基本配置</h1># 采样比率
management.tracing.sampling.probability=1.0
# 将 traceId 和 spanId 和 log 绑定
logging.pattern.level=%5p [${spring.application.name:test},%X{traceId:-},%X{spanId:-}]`
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">测试</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">拜访</span> curl http://localhost:8080/api/test,</p>日志输出如下:2022-12-06T12:06:36.971+08:00 INFO
61043 --- [nio-8080-
exec-1] c.g.bridge.boot.controller.MyController : this is test log ....
复制代码zipkin 界面<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/7bd34fee6ec844ea8683a93a991736ec~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728815845&x-signature=jXtYKvEvtcKAuhVpMfpVq2Zm%2Byw%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PS:你可能<span style="color: black;">无</span>看到任何关于 zipkin 的配置,<span style="color: black;">由于</span>默认<span style="color: black;">状况</span>下,zipkin 的上报<span style="color: black;">位置</span>是 127.0.0.1:9411,我在本地<span style="color: black;">经过</span> docker <span style="color: black;">起步</span>了一个 zipkin 实例,<span style="color: black;">因此</span>我并不需要做额外的配置。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">总结</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本篇对 SpringBoot3 tracing 做了<span style="color: black;">有些</span>介绍,<span style="color: black;">经过</span>本篇<span style="color: black;">文案</span>,希望你<span style="color: black;">能够</span><span style="color: black;">认识</span>到 SpringBoot3 中<span style="color: black;">供给</span> tracing 的初衷,以及在众多事实标准存在<span style="color: black;">状况</span>下<span style="color: black;">选取</span> Micrometer tracing <span style="color: black;">做为</span> API 的<span style="color: black;">原由</span>。这篇<span style="color: black;">文案</span>并不会<span style="color: black;">触及</span>到<span style="color: black;">太多</span><span style="color: black;">针对</span> tracing <span style="color: black;">基本</span>概念的介绍,<span style="color: black;">倘若</span>你有兴趣<span style="color: black;">能够</span><span style="color: black;">自动</span>查阅。最后我给出了 Micrometer tacing + brave + zipkin 一个小案例,<span style="color: black;">期盼</span><span style="color: black;">能够</span><span style="color: black;">帮忙</span>到你。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">作者:磊叔的技术博客</p>链接:https://juejin.cn/post/7173914390352101412
一看到楼主的气势,我就觉得楼主同在社区里灌水。
页:
[1]