本文截取自: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 { @Bean
public 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());
} @Bean
public Brave brave(SpanCollector spanCollector) {
Brave.Builder builder = new Brave.Builder("service1");//指定serviceName
builder.spanCollector(spanCollector);
builder.traceSampler(Sampler.create(1));//采集率
return builder.build();
} @Bean
public BraveServletFilter braveServletFilter(Brave brave) {
/**
* 设置sr、ss拦截器
*/
return new BraveServletFilter(brave.serverRequestInterceptor(),
brave.serverResponseInterceptor(),
new DefaultSpanNameProvider());
} @Bean
public 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 { @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.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 { @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;
} }

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

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";
} }

4.3、application.properties

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

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

  3. 《Linux命令行与shell脚本编程大全》 第二十七章 学习笔记

    第二十七章:shell脚本编程进阶 监测系统统计数据 系统快照报告 1.运行时间 uptime命令会提供以下基本信息: 当前时间 系统运行的天数,小时数,分钟数 当前登录到系统的用户数 1分钟,5分钟 ...

  4. Gradle 1.12用户指南翻译——第二十七章. Ear 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  5. “全栈2019”Java多线程第二十七章:Lock获取lock/释放unlock锁

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  6. “全栈2019”Java第二十七章:流程控制语句中循环语句for

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

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

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

  8. SpringBoot | 第二十七章:监控管理之Actuator使用

    前言 随着我们服务越来越多,部署的环境也越来越繁多时,由于各服务都部署在不同的机器上,每当出现问题或者异常时,想快速进行问题的定位就变的麻烦了.所以,本章节开始,开始讲解SpringBoot的监控相关 ...

  9. 第三十七章 springboot+docker(手动部署)

    一.下载centos镜像 docker pull hub.c.163.com/library/centos:latest docker tag containId centos:7 docker ru ...

随机推荐

  1. linux学习笔记-4.系统命令

    1.查看主机名 hostname 2.修改主机名(重启后无效) hostname hadoop 3.修改主机名(重启后永久生效) vi /ect/sysconfig/network 4.修改IP(重启 ...

  2. [转]C++ template —— 模板基础(一)

    <C++ Template>对Template各个方面进行了较为深度详细的解析,故而本系列博客按书本的各章顺序编排,并只作为简单的读书笔记,详细讲解请购买原版书籍(绝对物超所值).---- ...

  3. MOXA的Nport5600初始密码

    今天第一次弄Nport,看了半天手册没找到初始密码,网上也搜不到,按照说明书上想打电话问问,发现根本是空号... 后来灵感一来试了一下,居然是:moxa

  4. android.intent.category.DEFAULT

    我们需要什么时候加android.intent.category.DEFAULT呢? 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐式) intent什么是explicit(显示) i ...

  5. 吴恩达-coursera-机器学习-week3

    六.逻辑回归(Logistic Regression) 6.1 分类问题 6.2 假说表示 6.3 判定边界 6.4 代价函数 6.5 简化的成本函数和梯度下降 6.6 高级优化 6.7 多类别分类: ...

  6. Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs 水题

    A. Dreamoon and Stairs 题目连接: http://www.codeforces.com/contest/476/problem/A Description Dreamoon wa ...

  7. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem F. Turning Grille 暴力

    Problem F. Turning Grille 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c70 ...

  8. 外键的约束(Mysql、PostgreSQL)

    关于外键是什么,具体不再详述,可以自行百度. 讲一下关于外键的 On Delete和On Update的使用 最近在项目的表中看到这些,不懂顺便查了查: ONSTRAINT "c_clust ...

  9. Jmeter关于上传图片接口

    最近接到的一个新的项目,老规矩,开发组开发完接口需要进行接口的测试,其他的很简单,根据限制条件逻辑等设计数据,用浏览器或者工具进行验证就OK. 其中有一个接口涉及到图片的上传,以前没有用过,通过查找资 ...

  10. 利用dynamic简化数据库的访问

    今天写了一个数据库的帮助类,代码如下. public static class DbEx { public static dynamic ReadToObject(this IDataReader r ...