brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们定制,而ServerRequestInterceptor和ServerResponseInterceptor是在BraveServletFilter部分直接指定就好了(这在任何client技术下都可以复用)。

实际上,ClientRequestInterceptor和ClientResponseInterceptor也是我们的定制点

一、代码

基本参考第二十七章 springboot + zipkin(brave-okhttp实现)

1、service1

1.1、pom.xml

<!-- zipkin brave -->
        <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>
        <!-- async-http-client -->
        <dependency>
            <groupId>com.ning</groupId>
            <artifactId>async-http-client</artifactId>
            <version>1.9.31</version>
        </dependency>

1.2、ZipkinConfig

package com.xxx.service1.zipkin.brave.async;

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.servlet.BraveServletFilter;
import com.ning.http.client.AsyncHttpClient;

@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://localhost:9411",
                                        spanConfig,
                                        new EmptySpanCollectorMetricsHandler());
    }

    @Bean
    public Brave brave(SpanCollector spanCollector) {
        Brave.Builder builder = new Brave.Builder("asyn1 - service1 - 1");//指定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 AsyncHttpClient asyncHttpClient(){
        return new AsyncHttpClient();
    }
}

说明:指定了serviceName:"asyn1 - service1 - 1"

1.3、ZipkinAsyncController

package com.xxx.service1.zipkin.brave.async;

import java.net.URI;
import java.util.concurrent.Future;

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 com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.HttpClientRequest;
import com.github.kristofa.brave.http.HttpClientRequestAdapter;
import com.github.kristofa.brave.http.HttpClientResponseAdapter;
import com.github.kristofa.brave.http.HttpResponse;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Request;
import com.ning.http.client.RequestBuilder;
import com.ning.http.client.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("zipkin brave async api")
@RestController
@RequestMapping("/zipkin/async/service1")
public class ZipkinAsyncController {

    @Autowired
    private AsyncHttpClient asyncHttpClient;
    @Autowired
    private Brave           brave;

    @ApiOperation("trace第一步")
    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String myboot() {

        try {
            RequestBuilder builder = new RequestBuilder();
            String url = "http://localhost:8032/zipkin/async/service2/test2";
            builder.setUrl(url);
            Request request = builder.build();

            clientRequestInterceptor(request);
            Future<Response> response = asyncHttpClient.executeRequest(request);
            clientResponseInterceptor(response.get());

            return response.get().getResponseBody();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private void clientRequestInterceptor(Request request) {
        brave.clientRequestInterceptor().handle(new HttpClientRequestAdapter(new HttpClientRequest() {

            @Override
            public URI getUri() {
                return URI.create(request.getUrl());
            }

            @Override
            public String getHttpMethod() {
                return request.getMethod();
            }

            @Override
            public void addHeader(String headerKey, String headerValue) {
                request.getHeaders().add(headerKey, headerValue);
            }
        }, new DefaultSpanNameProvider()));
    }

    private void clientResponseInterceptor(Response response) {
        brave.clientResponseInterceptor().handle(new HttpClientResponseAdapter(new HttpResponse() {
            public int getHttpStatusCode() {
                return response.getStatusCode();
            }
        }));
    }

}

说明:

  • clientRequestInterceptor(com.ning.http.client.Request request)

    • 其实就是在做cs
  • clientResponseInterceptor(com.ning.http.client.Response response)
    • 其实就是在做cr

2、service2

2.1、pom.xml

<!-- zipkin brave -->
        <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>
        <!-- async-http-client -->
        <dependency>
            <groupId>com.ning</groupId>
            <artifactId>async-http-client</artifactId>
            <version>1.9.31</version>
        </dependency>

2.2、ZipkinConfig

package com.xxx.service1.zipkin.brave.async;

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.servlet.BraveServletFilter;
import com.ning.http.client.AsyncHttpClient;

@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://localhost:9411",
                                        spanConfig,
                                        new EmptySpanCollectorMetricsHandler());
    }

    @Bean
    public Brave brave(SpanCollector spanCollector) {
        Brave.Builder builder = new Brave.Builder("asyn2 - service2 - 2");//指定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 AsyncHttpClient asyncHttpClient(){
        return new AsyncHttpClient();
    }
}

说明:指定了serviceName:"asyn2 - service2 - 2"

2.3、ZipkinAsyncController

package com.xxx.service2.zipkin.brave.async;

import java.net.URI;
import java.util.concurrent.Future;

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 com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.HttpClientRequest;
import com.github.kristofa.brave.http.HttpClientRequestAdapter;
import com.github.kristofa.brave.http.HttpClientResponseAdapter;
import com.github.kristofa.brave.http.HttpResponse;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Request;
import com.ning.http.client.RequestBuilder;
import com.ning.http.client.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("zipkin brave async api")
@RestController
@RequestMapping("/zipkin/async/service2")
public class ZipkinAsyncController {

    @Autowired
    private AsyncHttpClient asyncHttpClient;
    @Autowired
    private Brave           brave;

    @ApiOperation("trace第2步")
    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String myboot2() {

        try {
            /*****************************serivce3*******************************/
            RequestBuilder builder3 = new RequestBuilder();
            String url3 = "http://localhost:8033/zipkin/async/service3/test3";
            builder3.setUrl(url3);
            Request request3 = builder3.build();

            clientRequestInterceptor(request3);
            Future<Response> response3 = asyncHttpClient.executeRequest(request3);
            clientResponseInterceptor(response3.get());

            /*****************************serivce4*******************************/
            RequestBuilder builder4 = new RequestBuilder();
            String url4 = "http://localhost:8034/zipkin/async/service4/test4";
            builder4.setUrl(url4);
            Request request4 = builder4.build();

            clientRequestInterceptor(request4);
            Future<Response> response4 = asyncHttpClient.executeRequest(request4);
            clientResponseInterceptor(response4.get());

            return response3.get().getResponseBody() + "=====" + response4.get().getResponseBody();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private void clientRequestInterceptor(Request request) {
        brave.clientRequestInterceptor().handle(new HttpClientRequestAdapter(new HttpClientRequest() {

            @Override
            public URI getUri() {
                return URI.create(request.getUrl());
            }

            @Override
            public String getHttpMethod() {
                return request.getMethod();
            }

            @Override
            public void addHeader(String headerKey, String headerValue) {
                request.getHeaders().add(headerKey, headerValue);
            }
        }, new DefaultSpanNameProvider()));
    }

    private void clientResponseInterceptor(Response response) {
        brave.clientResponseInterceptor().handle(new HttpClientResponseAdapter(new HttpResponse() {
            public int getHttpStatusCode() {
                return response.getStatusCode();
            }
        }));
    }

}

3、service3

3.1、pom.xml

<!-- zipkin brave -->
        <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>

3.2、ZipkinConfig

package com.xxx.service3.zipkin.brave.async;

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.servlet.BraveServletFilter;

@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://localhost:9411",
                                        spanConfig,
                                        new EmptySpanCollectorMetricsHandler());
    }

    @Bean
    public Brave brave(SpanCollector spanCollector) {
        Brave.Builder builder = new Brave.Builder("asyn3 - service3 - 3");//指定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());
    }
}

说明:指定了serviceName:"asyn3 - service3 - 3"

3.3、ZipkinAsyncController

package com.xxx.service3.zipkin.brave.async;

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 async api")
@RestController
@RequestMapping("/zipkin/async/service3")
public class ZipkinAsyncController {

    @ApiOperation("trace第3步")
    @RequestMapping(value = "/test3", method = RequestMethod.GET)
    public String myboot3() {

        try {
            return "async - service3";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}

4、service4

4.1、pom.xml

<!-- zipkin brave -->
        <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>

4.2、ZipkinConfig

package com.xxx.service4.zipkin.brave.async;

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.servlet.BraveServletFilter;

@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://localhost:9411",
                                        spanConfig,
                                        new EmptySpanCollectorMetricsHandler());
    }

    @Bean
    public Brave brave(SpanCollector spanCollector) {
        Brave.Builder builder = new Brave.Builder("asyn4 - service4 - 4");//指定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());
    }

}

说明:指定了serviceName:"asyn4 - service4 - 4"

4.3、ZipkinAsyncController

package com.xxx.service4.zipkin.brave.async;

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 async api")
@RestController
@RequestMapping("/zipkin/async/service4")
public class ZipkinAsyncController {

    @ApiOperation("trace第4步")
    @RequestMapping(value = "/test4", method = RequestMethod.GET)
    public String myboot3() {

        try {
            return "async - service4";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}

二、测试结果

1、依赖关系图:

2、span时间消耗图

3、详情图

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

【第二十八章】 springboot + zipkin(brave定制-AsyncHttpClient)的更多相关文章

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

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

  2. Gradle 1.12用户指南翻译——第二十八章. Jetty 插件

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

  3. “全栈2019”Java多线程第二十八章:公平锁与非公平锁详解

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

  4. “全栈2019”Java第二十八章:数组详解(上篇)

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

  5. SpringBoot | 第二十八章:监控管理之Spring Boot Admin使用

    前言 上一章节,我们介绍了Actuator的使用,知道了可通过访问不同的端点路径,获取相应的监控信息.但使用后也能发现,返回的监控数据都是以JSON串的形式进行返回的,对于实施或者其他人员来说,不是很 ...

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

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

  7. 第十八章 springboot + thymeleaf

    代码结构: 1.ThymeleafController package com.xxx.firstboot.web; import org.springframework.stereotype.Con ...

  8. 第三十八章 springboot+docker(maven)

    回顾上一章的整个部署过程: 使用"mvn install"进行打包jar 将jar移动到与Dockerfile文件相同的文件夹下 编写Dockerfile文件 使用"do ...

  9. 我的学习之路_第二十八章_JQuery 和validator插件

    jQuery 利用jQuery进行遍历 js原生: for(var i=0;i>?;i++){ } jQuery: 方式一: 想要遍历的jQuery对象.each(function(index, ...

随机推荐

  1. mysql 数据类型 目录

    mysql 数据类型 mysql 整数类型 数值类型 tinyint mysql int 整数类型 解释显示宽度 和 存储宽度 mysql float 浮点型 mysql 日期类型 mysql 字符串 ...

  2. python 基础 列表生成式 生成器

    列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式 举个例子,要生成list [1, 2, 3, 4, 5, 6, 7, ...

  3. android动态设置边框颜色

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  4. DNS解析原理和流程

    DNS解析原理和流程   DNS解析其实就是将IP地址(202.96.134.133)变成域名(www.xxxxx.com)   网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所 ...

  5. 让声音更清晰,用PR去掉视频中的噪音

    Premiere V7.0没有支持当前文件类型的输入源的问题 25 我是新手,把DVD影片放到电脑里,显示的是VOB文件,导入类型显示的是所有支持格式,于是我就点击了VOB文件,但是说没有支持当前文件 ...

  6. shell篇(一)

    login shell与non-login shell: login shell:取得shell时,需要完整的登入流程.如:tty1~tty6登入时,需要输入用户名和密码.此时取得的shell就称为l ...

  7. Perl中的正则表达式(五)

    正则表达式(Regular Expression),在Perl里边通常也叫做模式(Pattern),用来表示匹配(或不匹配)某个字符串的特征模板. 使用简单模式:若模式匹配的对象是$_的内容,只要把模 ...

  8. linux安装composer及安装yii2

    wget https://getcomposer.org/download/1.3.2/composer.phar mv composer.phar /usr/local/bin/composer c ...

  9. poj2932 Coneology

    地址:http://poj.org/problem?id=2932 题目: Coneology Time Limit: 5000MS   Memory Limit: 65536K Total Subm ...

  10. python3 字典的常用方法

    字典的方法(可能需要重新整理) 函数 说明 D代表字典对象   D.clear() 清空字典 D.pop(key) 移除键,同时返回此键所对应的值 D.copy() 返回字典D的副本,只复制一层(浅拷 ...