Spring Cloud Sleuth Zipkin(一)

9 篇文章 0 订阅
4 篇文章 0 订阅

Spring Cloud Sleuth Zipkin(一)

下载 ZipKin

curl -sSL https://zipkin.io/quickstart.sh | bash -s
java -jar zipkin.jar

页面访问地址 http://192.168.79.8:9411

Spring Cloud Nacos
项目改造
client-service, product-service 分别增加 sleuth, zipkin相关依赖

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-sleuth</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-zipkin</artifactId>
 </dependency>

分别增加相关的sleuth, zipkin application.yml 配置项

spring:
  zipkin:
    base-url: http://192.168.79.8:9411
  sleuth:
    sampler:
      # 全部采样, 默认 0.1
      probability: 1.0

启动client-service 1个实例, product-service 2实例
请求 http://localhost:9001/product/1http://localhost:9001/products
在这里插入图片描述
通过界面Zipkin 可以做部分定义查询,如过去多少小时及时间范围,从而更容易定位问题相关的请求耗时及异常。

client-service log

2022-09-05 15:06:48.248  INFO [client-service,94246097198123e7,94246097198123e7] 94160 --- [nio-9001-exec-2] p.i.f.c.controller.ProductController     : value={"data":{"id":"2","name":"product02"},"status":0,"message":null}
2022-09-05 15:12:52.980  INFO [client-service,041df398e16734a0,041df398e16734a0] 94160 --- [nio-9001-exec-5] p.i.f.c.controller.ProductController     : value={"data":[{"id":"1","name":"product01"},{"id":"2","name":"product02"},{"id":"3","name":"product03"},{"id":"4","name":"product04"},{"id":"5","name":"product05"}],"status":0,"message":null}

[client-service,94246097198123e7,94246097198123e7] => 对应 application.name, traceId, spanId
[client-service,041df398e16734a0,041df398e16734a0] => 对应 application.name, traceId, spanId

product-service 9007 log

2022-09-05 15:06:48.215  INFO [product-service,94246097198123e7,a666e25102a883d3] 148520 --- [nio-9007-exec-1] p.i.f.p.controller.ProductController     : ProductController#getProduct()

product-service 9006 log

2022-09-05 15:12:52.942  INFO [product-service,041df398e16734a0,6a1c5b2841ba3ada] 147756 --- [nio-9006-exec-1] p.i.f.p.controller.ProductController     : ProductController#list()

点击 client-service: get /product/{id} Show 查看链路详细
在这里插入图片描述

什么是链路追踪

在分布式系统,尤其是微服务系统中,一次外部请求往往被分发到多个服务,多个中间件,多台机器的相互调用才能完成。在这一系列的调用中,可能是串行的、并行的。我们需要确定这整个请求调用了哪些服务,哪些节点,以及对应的先后顺序及响应时间(性能)。从而引入链路追踪。

微服务架构是一个分布式架构,按业务划分不同的服务单元,一个分布式系统往往有多个服务单元。由于服务单元数量众多,业务的复杂性,如果出现了错误和异常,很难去定位。主要体现在,一个请求可能需要调用很多个服务,而内部服务的调用复杂性,决定了问题难以定位。所以微服务架构中,必须实现分布式链路追踪,去跟进一个请求到底有哪些服务参与,参与的顺序又是怎样的,从而达到每个请求的步骤清晰可见,出了问题,快速定位。

如上面 client-service -> product-service (instance 1, instance 2) 哪一次请求出了异常或者耗时过长,都可以通过链路追踪找到,而不需要找到对应时间排除法逐步分析

Google开源的 Dapper链路追踪组件,并在2010年发表了论文《Dapper, a Large-Scale Distributed Systems Tracing Infrastructure》,这篇文章是业内实现链路追踪的标杆和理论基础,具有非常大的参考价值。
目前,链路追踪组件有Google的Dapper,Twitter 的Zipkin,以及阿里的Eagleeye 等。

Spring Cloud Sleuth

Spring Cloud Sleuth is able to trace your requests and messages so that you can correlate that communication to corresponding log entries. You can also export the tracing information to an external system to visualize latency. Spring Cloud Sleuth supports OpenZipkin compatible systems directly。

基本术语

Spring Cloud Sleuth采用的是Google的开源项目Dapper的专业术语。

  • Span:基本工作单元,发送一个远程调度任务 就会产生一个Span,Span是一个64位ID唯一标识的,Trace是用另一个64位ID唯一标识的,Span还有其他数据信息,比如摘要、时间戳事件、Span的ID、以及进度ID。
  • Trace:一系列Span组成的一个树状结构。请求一个微服务系统的API接口,这个API接口,需要调用多个微服务,调用每个微服务都会产生一个新的Span,所有由这个请求产生的Span组成了这个Trace。
  • Annotation:用来及时记录一个事件的,一些核心注解用来定义一个请求的开始和结束 。这些注解包括以下:
    • cs - Client Sent -客户端发送一个请求,这个注解描述了这个Span的开始
    • sr - Server Received -服务端获得请求并准备开始处理它,如果将其sr减去cs时间戳便可得到网络传输的时间。
    • ss - Server Sent (服务端发送响应)–该注解表明请求处理的完成(当请求返回客户端),如果ss的时间戳减去sr时间戳,就可以得到服务器请求的时间。
    • cr - Client Received (客户端接收响应)-此时Span的结束,如果cr的时间戳减去cs时间戳便可以得到整个请求所消耗的时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值