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

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-okhttp实现)

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

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

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

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

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

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

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

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

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

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

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

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

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

  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. 64位windows 7下配置TortoiseGit(转)

    原文:http://our2848884.blog.163.com/blog/static/146854834201152325233854/ 最近感觉自己电脑上的代码太乱了,东一块.西一块……于是决 ...

  2. css内边距 边框

    /*1 元素的各边都有 10 像素的内边距 四个值上.右.下.左 两个上下,左右 三个值:上,左右,下*/ /*p {padding: 10%;}*/ h1 { padding-top: 10px; ...

  3. (转)FastDFS文件存储

    一.FastDFS介绍 FastDFS开源地址:https://github.com/happyfish100 参考:分布式文件系统FastDFS设计原理 参考:FastDFS分布式文件系统 个人封装 ...

  4. 如何实现在H5里调起高德地图APP?

    http://www.cnblogs.com/milkmap/p/5912350.html 这一篇文章,将讲述如何在H5里调起高德地图APP,并展示兴趣点.适合于展示某个餐馆,商场等,让用户自行选择前 ...

  5. Sequence(priority_queue)

    这题很智慧. VJ上4000多ms #include<cstdio> #include<algorithm> #include<queue> #include &l ...

  6. SQLserver数据库连接问题

    可能安装好之后数据库的端口1433被防火墙拦截了,查看端口是否在监听当中: 在cmd里输入命令 :netstat -an 查看是否处在监听中,如果没有进入下面的设置, C:\Windows\SysWO ...

  7. iOS 网易彩票-2框架搭建-代码重构

    在上一篇中,我们基本已经把整个框架都搭建出来了,下面进行代码重构一下. 思路: 导航按钮,按下时,会变灰,那是系统自带了,通过自定义UIButton,实现按下按钮立即切换效果. MJTabBarCon ...

  8. html03

    快速开发指南:1.新建页面之后,编写外部样式表文件,引入到HTML页面中2.用浏览器打开页面->F12->sources->打开css文件 右边编写样式,左边查看效果3.样式编写完成 ...

  9. CPU VS GPU(性能调优 12.1)

    CPU VS GPU 关于绘图和动画有两种处理的方式:CPU(中央处理器)和GPU(图形处理器).在现代iOS设备中,都有可以运行不同软件的可编程芯片,但是由于历史原因,我们可以说CPU所做的工作都在 ...

  10. javascript中父、子页面间调用

    本文主要转自:http://www.360doc.com/content/11/0525/17/6161903_119333834.shtml                    http://zh ...