首页  

Spring Cloud Sleuth Zipkin原理     所属分类 springcloud 浏览量 1169
微服务化应用链路追踪
Google 2010年 论文《Dapper, a Large-Scale Distributed Systems Tracing Infrastructure》
Google Dapper,Twitter  Zipkin, 阿里 Eagleeye
Spring Cloud Sleuth
Sleuth + Zipkin

Sleuth 作用
链路追踪 , 请求调用路径 , 理清服务间的调用关系
可视化错误 
分析耗时

Trace
由一组有相同TraceID的Span串联形成的树状结构
请求到达分布式系统的入口端点时,服务跟踪框架为该请求创建一个唯一的跟踪标识(TraceID),
在分布式系统内部流转的时候,传递该唯一标识,直到返回请求为止,通过它将所有请求过程中的日志关联起来


Span
代表一个基础的工作单元 ,例如 一次服务调用
唯一标识 SpanID ,通过span的开始和结束的时间戳,就能统计该span的时间延迟 ,还可以获取如事件名称、请求信息等元数据

Annotation 用于记录一段时间内的事件
cs - Client Sent  一个Span的开始
sr - Server Received  网络传输时间 = sr – cs 
ss - Server Sent   服务器处理时间= ss – sr 
cr - Client Received  Span的结束,整个请求耗时 = cr – cs 

采样率
如果服务的流量很大,全部采集对传输、存储压力比较大
spring.sleuth.sampler.probability=0.1 ( 1.0 采样率100%,采集服务的全部追踪数据)
默认 0.1 10%
bean 配置 全部采样 AlwaysSampler 或 不采样 NeverSampler

@Bean
public Sampler defaultSampler() {
    return new AlwaysSampler();
}

采样算法实现
spring-cloud-sleuth-core-2.0.0.RELEASE 
org.springframework.cloud.sleuth.sampler.ProbabilityBasedSampler

org.springframework.cloud.sleuth.instrument 包
支持的组件
async hystrix messaging websocket rxjava scheduling
web(SpringWebMvc,Spring WebFlux, Servlet)
webclient(Spring RestTemplate)
feign
zuul


web
webflux TraceWebFilter
webmvc HandlerInterceptorAdapter
Servlet 定义AOP切面对@RestController @Controller Callable 请求进行trace拦截,完成span的新建、传递和销毁
spring.sleuth.web.enabled 


async
通过TraceAsyncAspect对@Async注解进行拦截
通过 TraceRunnable 和 TraceCallable来对runnable和callable进行包装
利用LazyTraceExecutor来代替java的Executor 
spring.sleuth.async.enabled 


hystrix
使用HystrixPlugins添加trace相关的plugin,自定义了一个HystrixConcurrencyStrategy子类SleuthHystrixConcurrencyStrategy 
spring.sleuth.hystrix.strategy.enable

messaging
TracingChannelInterceptor,基于Spring message的ChannelInterceptorAdapter/ExecutorChannelInterceptor
spring.sleuth.integration.enabled 

websocket
TracingChannelInterceptor拦截类注册到ChannelRegistration中进行trace拦截


rxjava
自定义RxJavaSchedulersHook的子类SleuthRxJavaSchedulersHook,使用TraceAction来包装实例中Action0。
spring.sleuth.rxjava.schedulers.hook.enabled
可以定义一组正则表达式来对线程名进行过滤,选择哪些线程不需要跟踪


scheduling
使用 TraceSchedulingAspect 切面对Scheduled注解进行trace拦截
spring.sleuth.scheduled.enabled 

feign
TraceFeignClientAutoConfiguration
spring.sleuth.feign.enabled 

zuul
TracePostZuulFilter 传递tracing信息 
spring.sleuth.zuul.enabled 


zipkin 4个核心组件 Collector 负责收集外部系统跟踪信息,转化为Zipkin内部的Span格式 Storage 跟踪信息的存储,默认存储在内存中,支持 Mysql ElasticSearch API(Query) 负责查询Storage中存储的数据,提供简单的JSON API获取数据,主要提供给web UI使用 Web UI 提供简单的web界面,方便进行跟踪信息的查看以及查询,同时进行相关的分析 一个简单的过程 记录tags信息 将当前调用链的Trace信息记录到Http Headers中 记录当前调用的时间戳(timestamp) 发送http请求,并携带Trace相关的Header,如TraceId:traceidA, SpanId:tradeIdB; 调用结束后,记录当次调用所花的时间(duration) 将步骤1-5,汇总成一个Span(最小的Trace单元),上报该Span信息给Zipkin Collector
spring cloud sleuth可以结合zipkin,将信息发送到zipkin,zipkin存储信息,利用zipkin ui来展示数据 也可以简单的将数据记在日志中 仅使用sleuth+log配置 加入spring-cloud-starter-sleuth依赖即可 [appname,traceId,spanId,exportable] appname 服务名 traceId spanId exportable 是否发送给zipkin sleuth+zipkin+http sletuh+streaming+zipkin spring cloud streaming 支持kafka和rabbitmq Collector从消息中间件中读取数据并存储

上一篇     下一篇
ThreadLocal原理及最佳实践

分布式一致性算法 Paxos Raft ZAB

springcloud sleuth zipkin 实例

《人性的弱点》53条经典总结

kafka基础面试题

kafka核心知识点