本文截取自:http://blog.csdn.net/liaokailin/article/details/52077620
一、前提
1、zipkin基本知识:附8 zipkin
2、启动zipkin server:
2.1、在官网下载服务jar,http://zipkin.io/pages/quickstart.html,之后使用命令启动服务jar即可。
-
nohup java -jar zipkin-server-1.5.1-exec.jar &
之后,查看与该jar同目录下的nohup.out日志文件,发现其实该jar也是由springboot打成的,且内置的tomcat的启动端口是9411,如下:
2.2、打开浏览器,http://ip:9411/(host为服务启动的host)。
二、代码实现
1、service1
1.1、pom.xml
<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-okhttp</artifactId><version>3.9.0</version></dependency>
1.2、ZipkinConfig
package com.xxx.service1.zipkin;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import com.github.kristofa.brave.Brave; 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.okhttp.BraveOkHttpRequestResponseInterceptor; import com.github.kristofa.brave.servlet.BraveServletFilter;import okhttp3.OkHttpClient;/*** zipkin配置*/ @Configuration public class ZipkinConfig {@Beanpublic SpanCollector spanCollector() {HttpSpanCollector.Config spanConfig = HttpSpanCollector.Config.builder().compressionEnabled(false)//默认false,span在transport之前是否会被gzipped。.connectTimeout(5000)//5s,默认10s.flushInterval(1)//1s.readTimeout(6000)//5s,默认60s .build();return HttpSpanCollector.create("http://ip:9411", spanConfig,new EmptySpanCollectorMetricsHandler());}@Beanpublic Brave brave(SpanCollector spanCollector) {Brave.Builder builder = new Brave.Builder("service1");//指定serviceName builder.spanCollector(spanCollector);builder.traceSampler(Sampler.create(1));//采集率return builder.build();}@Beanpublic BraveServletFilter braveServletFilter(Brave brave) {/*** 设置sr、ss拦截器*/return new BraveServletFilter(brave.serverRequestInterceptor(), brave.serverResponseInterceptor(),new DefaultSpanNameProvider());}@Beanpublic OkHttpClient okHttpClient(Brave brave){/*** 设置cs、cr拦截器*/return new OkHttpClient.Builder().addInterceptor(new BraveOkHttpRequestResponseInterceptor(brave.clientRequestInterceptor(), brave.clientResponseInterceptor(), new DefaultSpanNameProvider())).build();}}
说明:
- HttpSpanCollector:span信息收集器
- Brave:基本实例,注意传入的参数是serviceName
- BraveServletFilter:设置sr(server receive),ss(server send)拦截器
- OkHttpClient:构建client实例,添加拦截器,设置cs(client send),cr(client receive)拦截器
1.3、ZipkinBraveController
package com.xxx.service1.zipkin;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response;@Api("zipkin brave api") @RestController @RequestMapping("/zipkin/brave/service1") public class ZipkinBraveController {@Autowiredprivate OkHttpClient okHttpClient;@ApiOperation("trace第一步")@RequestMapping("/test1")public String myboot() throws Exception {Thread.sleep(100);//100msRequest request = new Request.Builder().url("http://localhost:8032/zipkin/brave/service2/test2").build();/** 1、执行execute()的前后,会执行相应的拦截器(cs,cr)* 2、请求在被调用方执行的前后,也会执行相应的拦截器(sr,ss)*/Response response = okHttpClient.newCall(request).execute();return response.body().string();}}
1.4、application.properties
server.port=8031
2、service2
2.1、pom.xml、ZipkinConfig与service1相似,config部分需要换掉serviceName为"service2"
2.2、controller
package com.xxx.service2.zipkin;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response;@Api("zipkin brave api") @RestController @RequestMapping("/zipkin/brave/service2") public class ZipkinBraveController {@Autowiredprivate OkHttpClient okHttpClient;@ApiOperation("trace第二步")@RequestMapping(value="/test2",method=RequestMethod.GET)public String myboot() throws Exception {Thread.sleep(200);//100msRequest request3 = new Request.Builder().url("http://localhost:8033/zipkin/brave/service3/test3").build();Response response3 = okHttpClient.newCall(request3).execute();String response3Str = response3.body().string();Request request4 = new Request.Builder().url("http://localhost:8034/zipkin/brave/service4/test4").build();Response response4 = okHttpClient.newCall(request4).execute();String response4Str = response4.body().string();return response3Str + "-" +response4Str;}}
2.3、application.properties
server.port=8032
3、service3
3.1、pom.xml、ZipkinConfig与service1相似,config部分需要换掉serviceName为"service3"
3.2、controller
package com.xxx.service3.zipkin;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation;@Api("zipkin brave api") @RestController @RequestMapping("/zipkin/brave/service3") public class ZipkinBraveController {@ApiOperation("trace第三步")@RequestMapping(value="/test3",method=RequestMethod.GET)public String myboot() throws Exception {Thread.sleep(300);//100msreturn "service3";}}
3.3、application.properties
server.port=8033
4、service4
4.1、pom.xml、ZipkinConfig与service1相似,config部分需要换掉serviceName为"service4"
4.2、controller
package com.xxx.service4.zipkin;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation;@Api("zipkin brave api") @RestController @RequestMapping("/zipkin/brave/service4") public class ZipkinBraveController {@ApiOperation("trace第4步")@RequestMapping(value = "/test4", method = RequestMethod.GET)public String myboot() throws Exception {Thread.sleep(400);//100msreturn "service4";}}
4.3、application.properties
server.port=8034
三、测试
swagger访问localhost:8031/zipkin/brave/service1/test1,之后查看zipkin webUI即可。
结果:
1、查看调用依赖:
2、查看调用时间对比
点击第4个span,查看调用详情: