Brave介绍

1、Brave简介

Brave 是用来装备 Java 程序的类库,提供了面向标准Servlet、Spring MVC、Http Client、JAX RS、Jersey、Resteasy 和 MySQL 等接口的装备能力,可以通过编写简单的配置和代码,让基于这些框架构建的应用可以向 Zipkin 报告数据。同时 Brave 也提供了非常简单且标准化的接口,在以上封装无法满足要求的时候可以方便扩展与定制。

虽然Brave提供了默认的实现,结合项目实际情况,基本上是需要定制才能满足要求的,本文针对默认实现就不再啰嗦,直接针对定制进行讲解。

由于项目中用到SpringMvc,HttpClient,Jprotobuf-Rpc-Socket,本文主要介绍针对SpringMvc,HttpClient,Jprotobuf-Rpc-Socket的扩展与定制。

2、服务调用常用的两种方式

1、服务以Http方式提供Rest接口,服务与服务之间通过HttpClient互相调用,对外以Http方式提供Rest接口,这里Rest以SpringMvc为例。

2、服务以jprotobufrpcsocket方式提供Rpc接口,服务与服务之间通过RPC互相调用,对外以Http方式提供Rest接口,这里Rest以SpringMvc为例,RPC以jprotobufrpcsocket为例。

3、Brave环境准备

1、Maven引入

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.</modelVersion>
  4.  
  5. <groupId>io.zipkin.brave</groupId>
  6. <artifactId>brave-webmvc-example</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9.  
  10. <name>brave-webmvc-examplea</name>
  11. <description>Example using Brave to trace RPCs from Spring Web MVC</description>
  12. <url>https://github.com/openzipkin/brave-webmvc-example</url>
  13.  
  14. <properties>
  15. <project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
  16. <spring.version>4.3..RELEASE</spring.version>
  17. <jetty.version>8.1..v20160902</jetty.version>
  18. <brave.version>3.16.</brave.version>
  19. <zipkin-reporter.version>0.6.</zipkin-reporter.version>
  20. </properties>
  21.  
  22. <dependencies>
  23. <dependency>
  24. <groupId>junit</groupId>
  25. <artifactId>junit</artifactId>
  26. <version>4.10</version>
  27. <scope>test</scope>
  28. </dependency>
  29. <dependency>
  30. <groupId>io.zipkin.brave</groupId>
  31. <artifactId>brave-core-spring</artifactId>
  32. <version>${brave.version}</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>io.zipkin.reporter</groupId>
  36. <artifactId>zipkin-sender-okhttp3</artifactId>
  37. <version>${zipkin-reporter.version}</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>io.zipkin.reporter</groupId>
  41. <artifactId>zipkin-sender-libthrift</artifactId>
  42. <version>${zipkin-reporter.version}</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>io.zipkin.reporter</groupId>
  46. <artifactId>zipkin-sender-kafka08</artifactId>
  47. <version>${zipkin-reporter.version}</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>io.zipkin.brave</groupId>
  51. <artifactId>brave-spring-web-servlet-interceptor</artifactId>
  52. <version>${brave.version}</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>io.zipkin.brave</groupId>
  56. <artifactId>brave-spring-resttemplate-interceptors</artifactId>
  57. <version>${brave.version}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework</groupId>
  61. <artifactId>spring-core</artifactId>
  62. <version>${spring.version}</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework</groupId>
  66. <artifactId>spring-context</artifactId>
  67. <version>${spring.version}</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.springframework</groupId>
  71. <artifactId>spring-beans</artifactId>
  72. <version>${spring.version}</version>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.springframework</groupId>
  76. <artifactId>spring-web</artifactId>
  77. <version>${spring.version}</version>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.springframework</groupId>
  81. <artifactId>spring-webmvc</artifactId>
  82. <version>${spring.version}</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>org.eclipse.jetty</groupId>
  86. <artifactId>jetty-server</artifactId>
  87. <version>${jetty.version}</version>
  88. <scope>test</scope>
  89. </dependency>
  90. <dependency>
  91. <groupId>org.eclipse.jetty</groupId>
  92. <artifactId>jetty-webapp</artifactId>
  93. <version>${jetty.version}</version>
  94. <scope>test</scope>
  95. </dependency>
  96. <dependency>
  97. <groupId>log4j</groupId>
  98. <artifactId>log4j</artifactId>
  99. <version>1.2.</version>
  100. </dependency>
  101. <dependency>
  102. <groupId>org.slf4j</groupId>
  103. <artifactId>slf4j-log4j12</artifactId>
  104. <version>1.7.</version>
  105. </dependency>
  106. <dependency>
  107. <groupId>org.slf4j</groupId>
  108. <artifactId>jcl-over-slf4j</artifactId>
  109. <version>1.7.</version>
  110. </dependency>
  111. </dependencies>
  112. <build>
  113. <plugins>
  114. <plugin>
  115. <inherited>true</inherited>
  116. <groupId>org.apache.maven.plugins</groupId>
  117. <artifactId>maven-compiler-plugin</artifactId>
  118. <version>2.5.</version>
  119. <configuration>
  120. <source>1.6</source>
  121. <target>1.6</target>
  122. <optimize>true</optimize>
  123. <debug>true</debug>
  124. </configuration>
  125. </plugin>
  126. <plugin>
  127. <groupId>org.apache.maven.plugins</groupId>
  128. <artifactId>maven-failsafe-plugin</artifactId>
  129. <version>2.19.</version>
  130. <executions>
  131. <execution>
  132. <id>integration-test</id>
  133. <goals>
  134. <goal>integration-test</goal>
  135. </goals>
  136. </execution>
  137. <execution>
  138. <id>verify</id>
  139. <goals>
  140. <goal>verify</goal>
  141. </goals>
  142. </execution>
  143. </executions>
  144. </plugin>
  145. <plugin>
  146. <groupId>org.apache.maven.plugins</groupId>
  147. <artifactId>maven-war-plugin</artifactId>
  148. <version>3.0.</version>
  149. <configuration>
  150. <webResources>
  151. <resource>
  152. <directory>${basedir}/src/main/webapp</directory>
  153. <filtering>true</filtering>
  154. <includes>
  155. <include>**/index.html</include>
  156. </includes>
  157. </resource>
  158. <resource>
  159. <directory>${basedir}/src/main/webapp/WEB-INF</directory>
  160. <filtering>true</filtering>
  161. <targetPath>WEB-INF</targetPath>
  162. <includes>
  163. <include>**/web.xml</include>
  164. </includes>
  165. </resource>
  166. </webResources>
  167. <packagingExcludes>WEB-INF/lib/servlet-api-*.jar</packagingExcludes>
  168. </configuration>
  169. </plugin>
  170. </plugins>
  171. </build>
  172. </project>

2、配置埋点(servlet拦截器)

WebTracingConfiguration.java

  1. package com.example.demo.common;
  2. import com.github.kristofa.brave.Brave;
  3. import com.github.kristofa.brave.http.DefaultSpanNameProvider;
  4. import com.github.kristofa.brave.http.SpanNameProvider;
  5. import com.github.kristofa.brave.spring.BraveClientHttpRequestInterceptor;
  6. import com.github.kristofa.brave.spring.ServletHandlerInterceptor;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import javax.annotation.PostConstruct;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.context.annotation.Import;
  14. import org.springframework.http.client.ClientHttpRequestInterceptor;
  15. import org.springframework.web.client.RestTemplate;
  16. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  17. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  18. import zipkin.Span;
  19. import zipkin.reporter.AsyncReporter;
  20. import zipkin.reporter.Reporter;
  21. import zipkin.reporter.Sender;
  22. import zipkin.reporter.okhttp3.OkHttpSender;
  23.  
  24. /**
  25. * This adds tracing configuration to any web mvc controllers or rest template clients. This should
  26. * be configured last.
  27. */
  28. @Configuration
  29. // import as the interceptors are annotation with javax.inject and not automatically wired
  30. @Import({BraveClientHttpRequestInterceptor.class, ServletHandlerInterceptor.class})
  31. public class WebTracingConfiguration extends WebMvcConfigurerAdapter {
  32.  
  33. /** 发送器配置 */
  34. @Bean Sender sender() {
  35. return OkHttpSender.create("http://47.52.199.51:9411/api/v1/spans");
  36. //return LibthriftSender.create("127.0.0.1");
  37. // return KafkaSender.create("127.0.0.1:9092");
  38. }
  39.  
  40. /** 用什么方式显示span信息 */
  41. @Bean Reporter<Span> reporter() {
  42. //取消注释,日志打印span信息
  43. //return new LoggingReporter(); // 打印日志本地,通过日志收集到ES
  44. return AsyncReporter.builder(sender()).build();
  45. }
  46.  
  47. @Bean Brave brave() {
  48. return new Brave.Builder("brave-webmvc-zipkin").reporter(reporter()).build();
  49. }
  50. // span命名提供者,默认为http方法.
  51. @Bean SpanNameProvider spanNameProvider() {
  52. return new DefaultSpanNameProvider();
  53. }
  54.  
  55. @Autowired
  56. private ServletHandlerInterceptor serverInterceptor;
  57.  
  58. @Autowired
  59. private BraveClientHttpRequestInterceptor clientInterceptor;
  60.  
  61. @Autowired
  62. private RestTemplate restTemplate;
  63.  
  64. @Bean
  65. RestTemplate template() {
  66. return new RestTemplate();
  67. }
  68.  
  69. // 添加rest template拦截器
  70. @PostConstruct
  71. public void init() {
  72. List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(restTemplate.getInterceptors());
  73. interceptors.add(clientInterceptor);
  74. restTemplate.setInterceptors(interceptors);
  75. }
  76.  
  77. // 添加Severlet拦截器
  78. @Override
  79. public void addInterceptors(InterceptorRegistry registry) {
  80. registry.addInterceptor(serverInterceptor);
  81. }
  82. }

2、ZipkinController.java

  1. package com.example.demo.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import org.springframework.web.client.RestTemplate;
  6.  
  7. @RestController
  8. public class ZipkinController {
  9. @Autowired RestTemplate template;
  10.  
  11. @GetMapping("/zipkin")
  12. public String service() throws Exception {
  13. return template.getForObject("http://192.168.1.100:8080/service0", String.class);
  14. }
  15. }

4、运行

1、访问:http://192.168.1.100:8084/zipkin

service0~service2部分请看:调用链系列一、Zipkin架构介绍、搭建、Springboot集承、Zipkin UI详解

调用链系列二、Zipkin 和 Brave 实现(springmvc、RestTemplate)服务调用跟踪的更多相关文章

  1. 调用链系列(3):如何从零开始捕获body和header

    拓展阅读:调用链系列(1):解读UAVStack中的贪吃蛇 调用链系列(2):轻调用链实现 在Java中,HTTP协议的请求/响应模型是由Servlet规范+Servlet容器(如Tomcat)实现的 ...

  2. 调用链系列(1):解读UAVStack中的贪吃蛇

    一.背景 对于分布式在线服务,一个请求需要经过多个系统中多个模块,可能多达上百台机器的协作才能完成单次请求.这种场景下单靠人力无法掌握整个请求中各个阶段的性能开销,更无法快速的定位系统中性能瓶颈.当发 ...

  3. dubbo+zipkin调用链监控(二)

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. C#制作Windows service服务系列二:演示一个定期执行的windows服务及调试(windows service)

    系列一: 制作一个可安装.可启动.可停止.可卸载的Windows service(downmoon原创) 系列二:演示一个定期执行的windows服务及调试(windows service)(down ...

  5. C#调用C++系列二:传结构体

    这一篇记录下C#调用C++的结构体的方式来使用OpenCV的数据格式,这里会有两种方式,第一种是C#传一个结构体和图像的路径给C++,然后C++将图像加载进来,再把传进来的结构体填满即可,第二种是C# ...

  6. spring cloud 入门系列五:使用Feign 实现声明式服务调用

    一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...

  7. 调用链系列三、基于zipkin调用链封装starter实现springmvc、dubbo、restTemplate等实现全链路跟踪

    一.实现思路 1.过滤器实现思路 所有调用链数据都通过过滤器实现埋点并收集.同一条链共享一个traceId.每个节点有唯一的spanId. 2.共享传递方式 1.rpc调用:通过隐式传参.dubbo有 ...

  8. Istio调用链埋点原理剖析—是否真的“零修改”分享实录(下)

    调用链原理和场景 正如Service Mesh的诞生是为了解决大规模分布式服务访问的治理问题,调用链的出现也是为了对应于大规模的复杂的分布式系统运行中碰到的故障定位定界问题.大量的服务调用.跨进程.跨 ...

  9. 多语言(Java、.NET、Node.js)混合架构下开源调用链追踪APM项目初步选型

    1. 背景 我们的技术栈包括了Java..NET.Node.js等,并且采用了分布式的技术架构,系统性能管理.问题排查成本越来越高. 2. 基本诉求 针对我们的情况,这里列出了选型的主要条件,作为最终 ...

随机推荐

  1. FreeRTOS 中 systick 相关配置

    @2018-7-16 > systick 属性配置 在文件 <port.c> 中函数 void vPortSetupTimerInterrupt( void ) 中配置计数周期.时钟 ...

  2. 【转】JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解

    http://www.cnblogs.com/therunningfish/p/5524238.html JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsol ...

  3. 在Android中afinal框架下實現sqlite數據庫版本升級的辦法

    public abstract void onUpgrade(SQLiteDatabase db,int oldVersion,int new Version) 這個方法在實現時需要重寫.   pub ...

  4. 2018 省选 T1 一双木棋

    题目描述 菲菲和牛牛在一块n 行m 列的棋盘上下棋,菲菲执黑棋先手,牛牛执白棋后手. 棋局开始时,棋盘上没有任何棋子,两人轮流在格子上落子,直到填满棋盘时结束. 落子的规则是:一个格子可以落子当且仅当 ...

  5. merge函数:R语言,根据相同的列或ID合并不同的文件

    一般Excel就能实现根据相同的列或ID合并不同的文件,但对于大文件来说,比如几十个G的数据量,用Excel处理,不仅耗时,而且还会使电脑崩溃.R语言的优势就体现在这里了,处理大文件相当快. firs ...

  6. R语言画棒状图(bar chart)和误差棒(error bar)

    假设我们现在有CC,CG,GG三种基因型及三种基因型对应的表型,我们现在想要画出不同的基因型对应表型的棒状图及误差棒.整个命令最重要的就是最后一句了,用arrows函数画误差棒.用到的R语言如下: d ...

  7. eclipse中编辑properties文件无法看到中文

    如果在eclipse中编辑properties文件无法看到中文则参考“Eclipse开发环境配置-indigo.docx”添加propedit插件.

  8. 1021. Deepest Root (25)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  9. 学习windows编程 day3 之 设置当前的背景颜色

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  10. Keil stm32 printf到Debug窗口

    使用JlinkV8+Keil41.在main.c输入以下代码 #include <stdio.h> #define ITM_Port8(n) (*((volatile unsigned c ...