一、前提

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. Python-02 生成器表达式,列表推导式

    列表推导式和生成器表达式 列表推导式,生成器表达式1,列表推导式比较直观,占内存2,生成器表达式不容易看出内容,省内存. [ 变量(加工后的数据) for  变量i  in 可迭代的数据类型 ] 列表 ...

  2. 在同一个项目中灵活运用application/json 和application/x-www-form-urlencoded 两种传输格式(配合axios,同时配置loading)

    'use strict' import axios from 'axios' // import qs from 'qs' import { Notification} from 'element-u ...

  3. Manjaro配置中国源

    1.自动寻找中国源 sudo pacman-mirrors -i -c China -m rank//更新镜像排名sudo vim /etc/pacman.d/mirrorlist //查看选择的源s ...

  4. Centos6安装破解Confluence6.3.1

    confluence是一个专业的企业知识管理与协同软件,可以用于构建企业wiki.通过它可以实现团队成员之间的协作和知识共享 安装和破解包百度网盘地址: 链接:https://pan.baidu.co ...

  5. StringUtils的Join函数

    有一天看到同事用了这么个函数,然而我并没有见过,所以查了查,以后说不定用得到. 包路径:org.apache.commons.lang3.StringUtils; 函数名:StringUtils.jo ...

  6. redis数据操作篇

    服务器端 服务器端的命令为redis-server 可以使⽤help查看帮助⽂档 redis-server --help 个人习惯 ps aux | grep redis 查看redis服务器进程su ...

  7. 【LeetCode】水题(刚开始重新刷题找感觉用的)

    [9] Palindrome Number [Easy] 给一个数字,用不转化成字符串的方式判断它是否是回文. 先求数字长度,然后把数字的后半段做翻转(就是不断地取模,除10这种方式),然后判断前后半 ...

  8. Java基本数据类型的类型转换规则

    基本类型转换分为自动转换和强制转换. 自动转换规则:容量小的数据类型可以自动转换成容量大的数据类型,也可 以说低级自动向高级转换.这儿的容量指的不是字节数,而是指类型表述的范围. 强制转换规则:高级变 ...

  9. python_django_分页

    分页:把从数据库中的数据分为多页在客户端显示. 在django中,可通过这两个对象来实现: Paginator对象 Page对象 Paginator对象与Page对象的关系: paginator对象调 ...

  10. mysql查找字段空、不为空的方法总结

    1.不为空 Select * From table_name Where id<>'' Select * From table_name Where id!='' 2.为空 Select ...