赵计刚
每天进步一点点

本文截取自: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

 1         <dependency>
 2             <groupId>io.zipkin.brave</groupId>
 3             <artifactId>brave-core</artifactId>
 4             <version>3.9.0</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>io.zipkin.brave</groupId>
 8             <artifactId>brave-spancollector-http</artifactId>
 9             <version>3.9.0</version>
10         </dependency>
11         <dependency>
12             <groupId>io.zipkin.brave</groupId>
13             <artifactId>brave-web-servlet-filter</artifactId>
14             <version>3.9.0</version>
15         </dependency>
16         <dependency>
17             <groupId>io.zipkin.brave</groupId>
18             <artifactId>brave-okhttp</artifactId>
19             <version>3.9.0</version>
20         </dependency>
View Code

1.2、ZipkinConfig

 1 package com.xxx.service1.zipkin;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 
 6 import com.github.kristofa.brave.Brave;
 7 import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler;
 8 import com.github.kristofa.brave.Sampler;
 9 import com.github.kristofa.brave.SpanCollector;
10 import com.github.kristofa.brave.http.DefaultSpanNameProvider;
11 import com.github.kristofa.brave.http.HttpSpanCollector;
12 import com.github.kristofa.brave.okhttp.BraveOkHttpRequestResponseInterceptor;
13 import com.github.kristofa.brave.servlet.BraveServletFilter;
14 
15 import okhttp3.OkHttpClient;
16 
17 /**
18  * zipkin配置
19  */
20 @Configuration
21 public class ZipkinConfig {
22 
23     @Bean
24     public SpanCollector spanCollector() {
25         HttpSpanCollector.Config spanConfig = HttpSpanCollector.Config.builder()
26                                               .compressionEnabled(false)//默认false,span在transport之前是否会被gzipped。
27                                               .connectTimeout(5000)//5s,默认10s
28                                               .flushInterval(1)//1s
29                                               .readTimeout(6000)//5s,默认60s
30                                               .build();
31         return HttpSpanCollector.create("http://ip:9411", 
32                                         spanConfig,
33                                         new EmptySpanCollectorMetricsHandler());
34     }
35 
36     @Bean
37     public Brave brave(SpanCollector spanCollector) {
38         Brave.Builder builder = new Brave.Builder("service1");//指定serviceName
39         builder.spanCollector(spanCollector);
40         builder.traceSampler(Sampler.create(1));//采集率
41         return builder.build();
42     }
43 
44     @Bean
45     public BraveServletFilter braveServletFilter(Brave brave) {
46         /**
47          * 设置sr、ss拦截器
48          */
49         return new BraveServletFilter(brave.serverRequestInterceptor(), 
50                                       brave.serverResponseInterceptor(),
51                                       new DefaultSpanNameProvider());
52     }
53 
54     @Bean
55     public OkHttpClient okHttpClient(Brave brave){
56         /**
57          * 设置cs、cr拦截器
58          */
59         return new OkHttpClient.Builder()
60                    .addInterceptor(new BraveOkHttpRequestResponseInterceptor(brave.clientRequestInterceptor(), 
61                                                                              brave.clientResponseInterceptor(), 
62                                                                              new DefaultSpanNameProvider()))
63                    .build();
64     }
65     
66 }

说明:

  • HttpSpanCollector:span信息收集器
  • Brave:基本实例,注意传入的参数是serviceName
  • BraveServletFilter:设置sr(server receive),ss(server send)拦截器
  • OkHttpClient:构建client实例,添加拦截器,设置cs(client send),cr(client receive)拦截器

1.3、ZipkinBraveController

 1 package com.xxx.service1.zipkin;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import io.swagger.annotations.Api;
 8 import io.swagger.annotations.ApiOperation;
 9 import okhttp3.OkHttpClient;
10 import okhttp3.Request;
11 import okhttp3.Response;
12 
13 @Api("zipkin brave api")
14 @RestController
15 @RequestMapping("/zipkin/brave/service1")
16 public class ZipkinBraveController {
17 
18     @Autowired
19     private OkHttpClient okHttpClient;
20 
21     @ApiOperation("trace第一步")
22     @RequestMapping("/test1")
23     public String myboot() throws Exception {
24         Thread.sleep(100);//100ms
25         Request request = new Request.Builder().url("http://localhost:8032/zipkin/brave/service2/test2").build();
26         /*
27          * 1、执行execute()的前后,会执行相应的拦截器(cs,cr)
28          * 2、请求在被调用方执行的前后,也会执行相应的拦截器(sr,ss)
29          */
30         Response response = okHttpClient.newCall(request).execute();
31         return response.body().string();
32     }
33 
34 }
View Code

1.4、application.properties

1 server.port=8031

 

2、service2

2.1、pom.xml、ZipkinConfig与service1相似,config部分需要换掉serviceName为"service2"

2.2、controller

 1 package com.xxx.service2.zipkin;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 import io.swagger.annotations.Api;
 9 import io.swagger.annotations.ApiOperation;
10 import okhttp3.OkHttpClient;
11 import okhttp3.Request;
12 import okhttp3.Response;
13 
14 @Api("zipkin brave api")
15 @RestController
16 @RequestMapping("/zipkin/brave/service2")
17 public class ZipkinBraveController {
18 
19     @Autowired
20     private OkHttpClient okHttpClient;
21 
22     @ApiOperation("trace第二步")
23     @RequestMapping(value="/test2",method=RequestMethod.GET)
24     public String myboot() throws Exception {
25         Thread.sleep(200);//100ms
26         Request request3 = new Request.Builder().url("http://localhost:8033/zipkin/brave/service3/test3").build();
27         Response response3 = okHttpClient.newCall(request3).execute();
28         String response3Str = response3.body().string();
29 
30         Request request4 = new Request.Builder().url("http://localhost:8034/zipkin/brave/service4/test4").build();
31         Response response4 = okHttpClient.newCall(request4).execute();
32         String response4Str = response4.body().string();
33 
34         return response3Str + "-" +response4Str;
35     }
36 
37 }
View Code

2.3、application.properties

1 server.port=8032

 

3、service3

3.1、pom.xml、ZipkinConfig与service1相似,config部分需要换掉serviceName为"service3"

3.2、controller

 1 package com.xxx.service3.zipkin;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RequestMethod;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import io.swagger.annotations.Api;
 8 import io.swagger.annotations.ApiOperation;
 9 
10 @Api("zipkin brave api")
11 @RestController
12 @RequestMapping("/zipkin/brave/service3")
13 public class ZipkinBraveController {
14 
15     @ApiOperation("trace第三步")
16     @RequestMapping(value="/test3",method=RequestMethod.GET)
17     public String myboot() throws Exception {
18         Thread.sleep(300);//100ms
19         return "service3";
20     }
21 
22 }
View Code

3.3、application.properties

1 server.port=8033

 

4、service4

4.1、pom.xml、ZipkinConfig与service1相似,config部分需要换掉serviceName为"service4"

4.2、controller

 1 package com.xxx.service4.zipkin;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RequestMethod;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import io.swagger.annotations.Api;
 8 import io.swagger.annotations.ApiOperation;
 9 
10 @Api("zipkin brave api")
11 @RestController
12 @RequestMapping("/zipkin/brave/service4")
13 public class ZipkinBraveController {
14 
15     @ApiOperation("trace第4步")
16     @RequestMapping(value = "/test4", method = RequestMethod.GET)
17     public String myboot() throws Exception {
18         Thread.sleep(400);//100ms
19         return "service4";
20     }
21 
22 }
View Code

4.3、application.properties

1 server.port=8034

 

三、测试

swagger访问localhost:8031/zipkin/brave/service1/test1,之后查看zipkin webUI即可。

结果:

1、查看调用依赖:

2、查看调用时间对比

点击第4个span,查看调用详情:

 

posted on 2016-09-04 11:00  赵计刚  阅读(8976)  评论(3编辑  收藏  举报