第二十八章 springboot + zipkin(brave定制-AsyncHttpClient)
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)的更多相关文章
- 【第二十八章】 springboot + zipkin(brave定制-AsyncHttpClient)
brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...
- Gradle 1.12用户指南翻译——第二十八章. Jetty 插件
其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...
- “全栈2019”Java多线程第二十八章:公平锁与非公平锁详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java第二十八章:数组详解(上篇)
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- SpringBoot | 第二十八章:监控管理之Spring Boot Admin使用
前言 上一章节,我们介绍了Actuator的使用,知道了可通过访问不同的端点路径,获取相应的监控信息.但使用后也能发现,返回的监控数据都是以JSON串的形式进行返回的,对于实施或者其他人员来说,不是很 ...
- 第二十七章 springboot + zipkin(brave-okhttp实现)
本文截取自:http://blog.csdn.net/liaokailin/article/details/52077620 一.前提 1.zipkin基本知识:附8 zipkin 2.启动zipki ...
- 第十八章 springboot + thymeleaf
代码结构: 1.ThymeleafController package com.xxx.firstboot.web; import org.springframework.stereotype.Con ...
- 第三十八章 springboot+docker(maven)
回顾上一章的整个部署过程: 使用"mvn install"进行打包jar 将jar移动到与Dockerfile文件相同的文件夹下 编写Dockerfile文件 使用"do ...
- 我的学习之路_第二十八章_JQuery 和validator插件
jQuery 利用jQuery进行遍历 js原生: for(var i=0;i>?;i++){ } jQuery: 方式一: 想要遍历的jQuery对象.each(function(index, ...
随机推荐
- Xamarin iOS教程之编辑界面编写代码
Xamarin iOS教程之编辑界面编写代码 Xamarin iOS的Interface Builder Interface Builder被称为编辑界面.它是一个虚拟的图形化设计工具,用来为iOS应 ...
- tesseract_ocr+pytesseract图像识别
一.windows安装配置 其他系统安装配置参考github:https://github.com/tesseract-ocr/tesseract/wiki 下载tesseract-ocr参考:htt ...
- 分类器评估方法:ROC曲线
注:本文是人工智能研究网的学习笔记 ROC是什么 二元分类器(binary classifier)的分类结果 ROC空间 最好的预测模型在左上角,代表100%的灵敏度和0%的虚警率,被称为完美分类器. ...
- tomcat 启动 关闭 重启脚本
启动 #!/bin/bash # Author:wanglan # Mail:@qq.com # Fuction:Tomcat Start/stop/restart script # Version: ...
- rsync使用sudo权限
1.在etc/sudoers增加,比如www-data这个账户的 www-data ALL=NOPASSWD:/usr/bin/rsync 2.使用时增加--rsync-path="sudo ...
- C++ Primer笔记3_默认实參_类初探_名字查找与类的作用域
1.默认函数实參 在C++中,能够为參数指定默认值,C语言是不支持默认參数的,Java也不支持! 默认參数的语法与使用: (1)在函数声明或定义时,直接对參数赋值.这就是默认參数: (2)在函数调用时 ...
- 《Go语言实战》摘录:7.1 并发模式 - runner
7.1 并发模式 - runner
- 深入System.Web.Caching命名空间 教你Hold住缓存管理
一,System .Web.Caching与缓存工作机制简介 System.Web.Caching是用来管理缓存的命名空间,其父级空间是System.Web,由此可见,缓存通常用于Web网站的开发,包 ...
- 基于设备树的TQ2440触摸屏驱动移植
平台 开发板:tq2440 内核:Linux-4.9 u-boot:u-boot-2015.04 概述 之前移植了LCD驱动,下面继续移植触摸屏驱动,然后将tslib也移植上去. 正文 一.移植触 ...
- url提交参数类
url提交参数类 type /// <summary> /// 准备url /// </summary> TynUrl = class private FUrl, FComma ...