Zipkin是什么
Zipkin分布式跟踪系统;它能够帮助收集时间数据,解决在microservice架构下的延迟问题;它管理这些数据的收集和查找;Zipkin的设计是基于谷歌的Google Dapper论文。
每一个应用程序向Zipkin报告定时数据,Zipkin UI呈现了一个依赖图表来展现多少跟踪请求通过了每一个应用程序;若是想解决延迟问题,能够过滤或者排序全部的跟踪请求,而且能够查看每一个跟踪请求占总跟踪时间的百分比。html
为何使用Zipkin
随着业务愈来愈复杂,系统也随之进行各类拆分,特别是随着微服务架构和容器技术的兴起,看似简单的一个应用,后台可能有几十个甚至几百个服务在支撑;一个前端的请求可能须要屡次的服务调用最后才能完成;当请求变慢或者不可用时,咱们没法得知是哪一个后台服务引发的,这时就须要解决如何快速定位服务故障点,Zipkin分布式跟踪系统就能很好的解决这样的问题。前端
Zipkin下载和启动
官方提供了三种方式来启动,这里使用第二种方式来启动;java
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec' java -jar zipkin.jar
首先下载zipkin.jar,而后直接使用-jar命令运行,要求jdk8以上版本;git
[root@localhost ~]# java -jar zipkin.jar ******** ** ** * * ** ** ** ** ** ** ** ** ******** **** **** **** **** ****** **** *** **************************************************************************** ******* **** *** **** **** ** ** ***** ** ***** ** ** ** ** ** ** ** ** * *** ** **** ** ** ** ***** **** ** ** *** ****** ** ** ** ** ** ** ** :: Powered by Spring Boot :: (v1.5.8.RELEASE) ...... on || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal) 2017-12-06 22:09:17.498 INFO 7555 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-12-06 22:09:17.505 INFO 7555 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0 2017-12-06 22:09:17.789 INFO 7555 --- [ main] b.c.e.u.UndertowEmbeddedServletContainer : Undertow started on port(s) 9411 (http) 2017-12-06 22:09:17.794 INFO 7555 --- [ main] zipkin.server.ZipkinServer : Started ZipkinServer in 16.867 seconds (JVM running for 19.199)
基于Undertow WEB服务器,提供对外端口:9411,能够打开浏览器访问http://ip:9411github
详细参考:https://zipkin.io/pages/quick...web
Zipkin架构
跟踪器(Tracer)位于你的应用程序中,并记录发生的操做的时间和元数据,提供了相应的类库,对用户的使用来讲是透明的,收集的跟踪数据称为Span;
将数据发送到Zipkin的仪器化应用程序中的组件称为Reporter,Reporter经过几种传输方式之一将追踪数据发送到Zipkin收集器(collector),
而后将跟踪数据进行存储(storage),由API查询存储以向UI提供数据。
架构图以下:spring
1.Trace
Zipkin使用Trace结构表示对一次请求的跟踪,一次请求可能由后台的若干服务负责处理,每一个服务的处理是一个Span,Span之间有依赖关系,Trace就是树结构的Span集合;apache
2.Span
每一个服务的处理跟踪是一个Span,能够理解为一个基本的工做单元,包含了一些描述信息:id,parentId,name,timestamp,duration,annotations等,例如:json
{ "traceId": "bd7a977555f6b982", "name": "get-traces", "id": "ebf33e1a81dc6f71", "parentId": "bd7a977555f6b982", "timestamp": 1458702548478000, "duration": 354374, "annotations": [ { "endpoint": { "serviceName": "zipkin-query", "ipv4": "192.168.1.2", "port": 9411 }, "timestamp": 1458702548786000, "value": "cs" } ], "binaryAnnotations": [ { "key": "lc", "value": "JDBCSpanStore", "endpoint": { "serviceName": "zipkin-query", "ipv4": "192.168.1.2", "port": 9411 } } ] }
traceId:标记一次请求的跟踪,相关的Spans都有相同的traceId;
id:span id;
name:span的名称,通常是接口方法的名称;
parentId:可选的id,当前Span的父Span id,经过parentId来保证Span之间的依赖关系,若是没有parentId,表示当前Span为根Span;
timestamp:Span建立时的时间戳,使用的单位是微秒(而不是毫秒),全部时间戳都有错误,包括主机之间的时钟误差以及时间服务从新设置时钟的可能性,
出于这个缘由,Span应尽量记录其duration;
duration:持续时间使用的单位是微秒(而不是毫秒);
annotations:注释用于及时记录事件;有一组核心注释用于定义RPC请求的开始和结束;浏览器
cs:Client Send,客户端发起请求; sr:Server Receive,服务器接受请求,开始处理; ss:Server Send,服务器完成处理,给客户端应答; cr:Client Receive,客户端接受应答从服务器;
binaryAnnotations:二进制注释,旨在提供有关RPC的额外信息。
3.Transport
收集的Spans必须从被追踪的服务运输到Zipkin collector,有三个主要的传输方式:HTTP, Kafka和Scribe;
4.Components
有4个组件组成Zipkin:collector,storage,search,web UI
collector:一旦跟踪数据到达Zipkin collector守护进程,它将被验证,存储和索引,以供Zipkin收集器查找;
storage:Zipkin最初数据存储在Cassandra上,由于Cassandra是可扩展的,具备灵活的模式,并在Twitter中大量使用;可是这个组件可插入,除了Cassandra以外,还支持ElasticSearch和MySQL;
search:一旦数据被存储和索引,咱们须要一种方法来提取它。查询守护进程提供了一个简单的JSON API来查找和检索跟踪,主要给Web UI使用;
web UI:建立了一个GUI,为查看痕迹提供了一个很好的界面;Web UI提供了一种基于服务,时间和注释查看跟踪的方法。
实战
使用Zipkin和Brave实现http服务调用的跟踪,Brave 是用来装备Java程序的类库,提供了面向标准Servlet、Spring MVC、Http Client、JAX RS、Jersey、Resteasy 和 MySQL 等接口的装备能力,能够经过编写简单的配置和代码,让基于这些框架构建的应用能够向 Zipkin 报告数据。同时 Brave 也提供了很是简单且标准化的接口,在以上封装没法知足要求的时候能够方便扩展与定制。
提供四个工程,分别对应四个服务分别是:zipkin1,zipkin2,zipkin3,zipkin4;zipkin1经过httpclient调用zipkin2,而后zipkin2经过httpclient调用zipkin3和zipkin4,造成一个调用链;四个服务都是基于spring-boot来实现,对应的端口分别是8081,8082,8083,8084;
1.公共maven依赖库
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-core</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-spancollector-http</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-web-servlet-filter</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-apache-http-interceptors</artifactId> <version>3.9.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> </dependencies>
2.核心类ZipkinBean提供须要使用的Bean
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.kristofa.brave.Brave; import com.github.kristofa.brave.Brave.Builder; import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler; import com.github.kristofa.brave.Sampler; import com.github.kristofa.brave.SpanCollector; import com.github.kristofa.brave.http.DefaultSpanNameProvider; import com.github.kristofa.brave.http.HttpSpanCollector; import com.github.kristofa.brave.http.HttpSpanCollector.Config; import com.github.kristofa.brave.httpclient.BraveHttpRequestInterceptor; import com.github.kristofa.brave.httpclient.BraveHttpResponseInterceptor; import com.github.kristofa.brave.servlet.BraveServletFilter; @Configuration public class ZipkinBean { /** * 配置收集器 * * @return */ @Bean public SpanCollector spanCollector() { Config config = HttpSpanCollector.Config.builder().compressionEnabled(false).connectTimeout(5000) .flushInterval(1).readTimeout(6000).build(); return HttpSpanCollector.create("http://192.168.237.128:9411", config, new EmptySpanCollectorMetricsHandler()); } /** * Brave各工具类的封装 * * @param spanCollector * @return */ @Bean public Brave brave(SpanCollector spanCollector) { Builder builder = new Builder("service1");// 指定serviceName builder.spanCollector(spanCollector); builder.traceSampler(Sampler.create(1));// 采集率 return builder.build(); } /** * 拦截器,须要serverRequestInterceptor,serverResponseInterceptor 分别完成sr和ss操做 * * @param brave * @return */ @Bean public BraveServletFilter braveServletFilter(Brave brave) { return new BraveServletFilter(brave.serverRequestInterceptor(), brave.serverResponseInterceptor(), new DefaultSpanNameProvider()); } /** * httpClient客户端,须要clientRequestInterceptor,clientResponseInterceptor分别完成cs和cr操做 * * @param brave * @return */ @Bean public CloseableHttpClient httpClient(Brave brave) { CloseableHttpClient httpclient = HttpClients.custom() .addInterceptorFirst(new BraveHttpRequestInterceptor(brave.clientRequestInterceptor(), new DefaultSpanNameProvider())) .addInterceptorFirst(new BraveHttpResponseInterceptor(brave.clientResponseInterceptor())).build(); return httpclient; } }
3.核心类ZipkinController对外接口
@RestController public class ZipkinController { @Autowired private CloseableHttpClient httpClient; @GetMapping("/service1") public String service() throws Exception { Thread.sleep(100); HttpGet get = new HttpGet("http://localhost:8082/service2"); CloseableHttpResponse response = httpClient.execute(get); return EntityUtils.toString(response.getEntity(), "utf-8"); } }
分别启动四个服务,而后浏览器访问:http://localhost:8081/service1,正常调用结果返回:
service3 service4
能够观察zipkin web ui,查看服务的调用链: