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. Java核心编程快速入门

    Java核心编程部分的基础学习内容就不一一介绍了,本文的重点是JAVA中相对复杂的一些概念,主体内容如下图所示. 反射reflect是理解Java语言工作原理的基础,Java编译器首先需要将我们编写的 ...

  2. clob字段超过4000转String类型

    上次提到listagg()和wm_concat()方法合并过的字段类型为clob,要是字段长度超过4000,直接使用to_char()方法转会报错. 解决方法可以在java代码中使用流的方式转化成字符 ...

  3. JAVA JMX协议监控

    JMX协议监控,可通过JMX协议远程监控,实时监控线上jvm情况,并通过平台管理界面进行 展示,可以通过监控实时获得线上服务器运行情况. 可以监控内存.实时线程.共享内存等各种信息. 获取实时线程信息 ...

  4. python语法(五)—函数

    前面几天学习了python的基础语法,判断,循环,以及文件操作等等内容,对python也是有了一个认识.今天开始学习python的函数和模块. 函数 函数是什么?我的理解就是,他和java中的方法是一 ...

  5. android Service oncreate 在UI线程 何时用service,何时用thread

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 服务的生命周期 各个方法 都是在主线程中的. 这里的操作可以导致主线程阻塞. 这些方法, ...

  6. BZOJ.2456.mode(绝对众数)

    题目链接 \(Description\) 限制空间(只能保留两个变量),求给定n个数中出现次数超过\(\frac{n}{2}\)的数. \(Solution\) 维护两个变量,\(now\)和\(cn ...

  7. hdu 4442 37届金华赛区 A题

    题意:给出一些队伍,每个队伍有初始等待时间和每秒增加的时间,求最短时间 假设有两个队初始时间和每秒增加时间为a1,b1和a2,b2 若第选择第一个的时间小于第二个,则 a1+a2+a1*b2<a ...

  8. Linux学习笔记04—IP配置

    一.自动获取IP只有一种情况可以自动获取IP地址,那就是你的Linux所在的网络环境中有DHCP服务.只要你的真机可以自动获取IP,那么安装在虚拟机的Linux同样也可以自动获取IP. 方法很简单,只 ...

  9. GitLab目录迁移方法

    在生产环境上迁移GitLab的目录需要注意一下几点: 1.目录的权限必须为755或者775 2.目录的用户和用户组必须为git:git 3.如果在深一级的目录下,那么git用户必须添加到上一级目录的账 ...

  10. General PE format layout