Zipkin和Brave实现http服务调用的跟踪
使用Zipkin和Brave实现http服务调用的跟踪,Brave 是用来装备Java程序的类库,提供了面向标准Servlet、Spring MVC、Http Client、JAX RS、Jersey、Resteasy 和 MySQL 等接口的装备能力,可以通过编写简单的配置和代码,让基于这些框架构建的应用可以向 Zipkin 报告数据。同时 Brave 也提供了非常简单且标准化的接口,在以上封装无法满足要求的时候可以方便扩展与定制。
提供四个工程,分别对应四个服务分别是:zipkin1,zipkin2,zipkin3,zipkin4;zipkin1通过httpclient调用zipkin2,然后zipkin2通过httpclient调用zipkin3和zipkin4,形成一个调用链;四个服务都是基于spring-boot来实现,对应的端口分别是8081,8082,8083,8084;
1.公共maven依赖库
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-apache-http-interceptors</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
2.核心类ZipkinBean提供需要使用的Bean
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.Brave.Builder;
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.http.HttpSpanCollector.Config;
import com.github.kristofa.brave.httpclient.BraveHttpRequestInterceptor;
import com.github.kristofa.brave.httpclient.BraveHttpResponseInterceptor;
import com.github.kristofa.brave.servlet.BraveServletFilter;
@Configuration
public class ZipkinBean {
/**
* 配置收集器
*
* @return
*/
@Bean
public SpanCollector spanCollector() {
Config config = HttpSpanCollector.Config.builder().compressionEnabled(false).connectTimeout(5000)
.flushInterval(1).readTimeout(6000).build();
return HttpSpanCollector.create("http://192.168.237.128:9411", config, new EmptySpanCollectorMetricsHandler());
}
/**
* Brave各工具类的封装
*
* @param spanCollector
* @return
*/
@Bean
public Brave brave(SpanCollector spanCollector) {
Builder builder = new Builder("service1");// 指定serviceName
builder.spanCollector(spanCollector);
builder.traceSampler(Sampler.create(1));// 采集率
return builder.build();
}
/**
* 拦截器,需要serverRequestInterceptor,serverResponseInterceptor 分别完成sr和ss操作
*
* @param brave
* @return
*/
@Bean
public BraveServletFilter braveServletFilter(Brave brave) {
return new BraveServletFilter(brave.serverRequestInterceptor(), brave.serverResponseInterceptor(),
new DefaultSpanNameProvider());
}
/**
* httpClient客户端,需要clientRequestInterceptor,clientResponseInterceptor分别完成cs和cr操作
*
* @param brave
* @return
*/
@Bean
public CloseableHttpClient httpClient(Brave brave) {
CloseableHttpClient httpclient = HttpClients.custom()
.addInterceptorFirst(new BraveHttpRequestInterceptor(brave.clientRequestInterceptor(),
new DefaultSpanNameProvider()))
.addInterceptorFirst(new BraveHttpResponseInterceptor(brave.clientResponseInterceptor())).build();
return httpclient;
}
}
3.核心类ZipkinController对外接口
@RestController
public class ZipkinController {
@Autowired
private CloseableHttpClient httpClient;
@GetMapping("/service1")
public String service() throws Exception {
Thread.sleep(100);
HttpGet get = new HttpGet("http://localhost:8082/service2");
CloseableHttpResponse response = httpClient.execute(get);
return EntityUtils.toString(response.getEntity(), "utf-8");
}
}
分别启动四个服务,然后浏览器访问:http://localhost:8081/service1,正常调用结果返回:
service3 service4
可以观察zipkin web ui,查看服务的调用链:
Zipkin和Brave实现http服务调用的跟踪的更多相关文章
- 调用链系列二、Zipkin 和 Brave 实现(springmvc、RestTemplate)服务调用跟踪
Brave介绍 1.Brave简介 Brave 是用来装备 Java 程序的类库,提供了面向标准Servlet.Spring MVC.Http Client.JAX RS.Jersey.Resteas ...
- zipkin微服务调用链分析
1.zipkin的作用 在微服务架构下,一个http请求从发出到响应,中间可能经过了N多服务的调用,或者N多逻辑操作, 如何监控某个服务,或者某个逻辑操作的执行情况,对分析耗时操作,性能瓶颈具有很大价 ...
- Spring Cloud 微服务六:调用链跟踪Spring cloud sleuth +zipkin
前言:随着微服务系统的增加,服务之间的调用关系变得会非常复杂,这给运维以及排查问题带来了很大的麻烦,这时服务调用监控就显得非常重要了.spring cloud sleuth实现了对分布式服务的监控解决 ...
- zipkin微服务调用链分析(python)
一,概述 zipkin的作用 在微服务架构下,一个http请求从发出到响应,中间可能经过了N多服务的调用,或者N多逻辑操作,如何监控某个服务,或者某个逻辑操作的执行情况,对分析耗时操作,性能瓶颈具有很 ...
- 调用链系列一、Zipkin架构介绍、Springboot集承(springmvc,HttpClient)调用链跟踪、Zipkin UI详解
1.Zipkin是什么 Zipkin分布式跟踪系统:它可以帮助收集时间数据,解决在microservice架构下的延迟问题:它管理这些数据的收集和查找:Zipkin的设计是基于谷歌的Google Da ...
- 微服务之分布式跟踪系统(springboot+zipkin+mysql)
通过上一节<微服务之分布式跟踪系统(springboot+zipkin)>我们简单熟悉了zipkin的使用,但是收集的数据都保存在内存中重启后数据丢失,不过zipkin的Storage除了 ...
- zipkin之brave
brave是同步收集信息,及计算调用时间,但是异步发送日志信息给zipkin:所以很多时候你无法在第一时间获取日志数据可能需要等一会.另外在写一个demo的时候,因为最后睡了1秒,经常会发现丢了一些日 ...
- 使用zipkin2在SpringCloud2.0环境下追踪服务调用情况
1.目的: 使用zipkin2.0在Spring Cloud 2.0环境下,追踪服务调用情况. 2.所需组件: zipkin2.0,Spring Cloud 2.0,Eureka Server,Eur ...
- 手把手教你学Dapr - 4. 服务调用
上一篇:手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序 介绍 通过使用服务调用,您的应用程序可以使用标准的gRPC或HTTP协议与其他应用程序可靠.安全地通信. 为什么不直接用Ht ...
随机推荐
- day 014 python 内置函数
1. lamda匿名函数2. sorted()3. filter()4. map()5. 递归函数 一 匿名函数(lambda) 函数名= lambda 参数: 返回值 简单算法 a+b 常规算 ...
- git使用之放弃本地修改
一,未使用 git add 缓存代码时. 可以使用 git checkout -- filepathname (比如: git checkout -- readme.md ,不要忘记中间的 “-- ...
- window.location.herf=url参数有中文,到后台乱码问题解决
js中的代码: /*将中文的参数进行两次编码 */ function queryByName(){ //获取查询条件的用户名 ...
- sofa graphql 2 rest api webhook 试用
sofa 的webhook实际上就是将graphql 的subscription 进行了扩展,当接受到sub 请求的时候 再做一次http 的转发处理,方便rest api 的访问 环境准备 环境还是 ...
- 用Hi3518EV200板当spi烧录器
1. setenv bootargs setenv bootcmd 2.ddr烧录uboot 3.uboot下tftp下载文件 mw.b ff ;tftp ;sf erase ;sf write ; ...
- 线性代数笔记10——矩阵的LU分解
在线性代数中, LU分解(LU Decomposition)是矩阵分解的一种,可以将一个矩阵分解为一个单位下三角矩阵和一个上三角矩阵的乘积(有时是它们和一个置换矩阵的乘积).LU分解主要应用在数值分析 ...
- ssh证书免认证登录
思路: 客户端私钥存放于客户端,/root/.ssh/id_rsa 将客户端公钥存放于要远程控制服务器上:将客户在公钥id_rsa.pub内容追加到 /root/.ssh/authorized_key ...
- c++获取键盘输入cin、scanf使用详解
cin是c++标准,scanf是在c中使用的 #include<cstdio> #include<iostream> #include<cstring> using ...
- Nginx + Tomcat搭建集群
一.Tomcat集群带来的好处 1.提高服务的性能,并发能力,以及高可用性 2.提供项目架构的横向扩展能力 二.Tomcat集群实现原理 通过Nginx负载均衡进行请求转发 三.Nginx + Tom ...
- C++Builder XE7 up1 简单测试
很久没用BCB了, 新装了BCBXE7up1试试了,发现有点找不到北了,好像与BCB6的一些默认设置项不一样,编译了一个空APP,提示找不到bpl 和 dll. 设置为不带包编译后,还是提示DLL找不 ...