一、前提

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         <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>

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

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 {

@Autowired
private OkHttpClient okHttpClient;

@ApiOperation("trace第一步")
@RequestMapping("/test1")
public String myboot() throws Exception {
Thread.sleep(100);//100ms
Request 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 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 }

1.4、application.properties

1 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 {

@Autowired
private OkHttpClient okHttpClient;

@ApiOperation("trace第二步")
@RequestMapping(value="/test2",method=RequestMethod.GET)
public String myboot() throws Exception {
Thread.sleep(200);//100ms
Request 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;
}

}

 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 }

2.3、application.properties

1 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);//100ms
return "service3";
}

}

 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 }

3.3、application.properties

1 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);//100ms
return "service4";
}

}

 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 }

4.3、application.properties

1 server.port=8034

三、测试

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

结果:

1、查看调用依赖:

2、查看调用时间对比

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

springboot + zipkin(brave-okhttp实现)的更多相关文章

  1. 第二十八章 springboot + zipkin(brave定制-AsyncHttpClient)

    brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...

  2. 【第二十八章】 springboot + zipkin(brave定制-AsyncHttpClient)

    brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...

  3. 第二十七章 springboot + zipkin(brave-okhttp实现)

    本文截取自:http://blog.csdn.net/liaokailin/article/details/52077620 一.前提 1.zipkin基本知识:附8 zipkin 2.启动zipki ...

  4. 【第二十七章】 springboot + zipkin(brave-okhttp实现)

    本文截取自:http://blog.csdn.net/liaokailin/article/details/52077620 一.前提 1.zipkin基本知识:附8 zipkin 2.启动zipki ...

  5. 第二十九章 springboot + zipkin + mysql

    zipkin的数据存储可以存在4个地方: 内存(仅用于测试,数据不会持久化,zipkin-server关掉,数据就没有了) 这也是之前使用的 mysql 可能是最熟悉的方式 es Cassandra ...

  6. 【第二十九章】 springboot + zipkin + mysql

    zipkin的数据存储可以存在4个地方: 内存(仅用于测试,数据不会持久化,zipkin-server关掉,数据就没有了) 这也是之前使用的 mysql 可能是最熟悉的方式 es Cassandra ...

  7. springboot + zipkin + mysql

    zipkin的数据存储可以存在4个地方: 内存(仅用于测试,数据不会持久化,zipkin-server关掉,数据就没有了) 这也是之前使用的 mysql 可能是最熟悉的方式 es Cassandra ...

  8. 微服务之分布式跟踪系统(springboot+zipkin+mysql)

    通过上一节<微服务之分布式跟踪系统(springboot+zipkin)>我们简单熟悉了zipkin的使用,但是收集的数据都保存在内存中重启后数据丢失,不过zipkin的Storage除了 ...

  9. SpringBoot集成Zipkin实现分布式全链路监控

    目录 Zipkin 简介 Springboot 集成 Zipkin 安装启动 zipkin 版本说明 项目结构 工程端口分配 引入 Maven 依赖 配置文件.收集器的设置 编写 Controller ...

随机推荐

  1. CentOS6.5源码安装MySQL5.6.35

    CentOS6.5源码安装MySQL5.6.35 一.卸载旧版本 1.使用下面的命令检查是否安装有mysql [root@localhost tools]# rpm -qa|grep -i mysql ...

  2. 用户界面样式(cursor,resize,vertical-align,outline,文字超出显示省略号)

    1. 鼠标样式 cursor default: 小白(箭头)默认 pointer:小手 move:移动 text:文本 not-allowed:禁止 2. 轮廓线(表单外发光)outline 给表单添 ...

  3. Android Studio在Ubuntu下离线安装Gradle

    更新android studio3.0后又要升级gradle了,估计又要很长时间,晚上临走前跟开始更新下载,第二天一早发现又卡了,吐血. 在某CSDN下载gradle-4.1-all.zip,直接手动 ...

  4. Samba服务的安装

    Samba的安装 1.准备环境 Centos7 [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce ...

  5. element 的时间快捷键

    1. <div> <el-date-picker v-model="value4" type="month" :picker-options= ...

  6. AngularJS 项目开发实战

    目录 啰嗦一下 你问我答 目录结构 压缩 其他 啰嗦一下 最近在开发一个项目时,调研了一下AngularJS,发现这个框架功能很丰富,而且用起来也很方便,所以深入了解了一下,在此分享一下我的感悟. A ...

  7. Cross platform

    值得学习的C/C++语言开源项目 (1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨平台. http://www.cs.wustl.edu/~schmidt/ACE.html ...

  8. Spring boot自定义拦截器和拦截器重定向配置简单介绍

    大家好: 本文简单介绍一下用于权限控制的Spring boot拦截器配置,拦截器重定向问题. 开发工具:jdk1.8   idea2017(付费版,网上找的破解教程) 1,首先使用idea创建一个Sp ...

  9. 【NIO】IO与NIO的区别

    一.概念 NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套N ...

  10. DOM学习总结(三)DOM访问/操作

    DOM访问理解:找到这个标签元素,然后才能对它进行操作 1.getElementById() 方法document.getElementById(""); //通过id名字来找到 ...