SpringBoot集成Zipkin实现分布式全链路监控

Zipkin 简介

Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in service architectures. Features include both the collection and lookup of this data.

If you have a trace ID in a log file, you can jump directly to it. Otherwise, you can query based on attributes such as service, operation name, tags and duration. Some interesting data will be summarized for you, such as the percentage of time spent in a service, and whether or not operations failed.

Application's need to be “instrumented” to report trace data to Zipkin. This usually means configuration of a tracer or instrumentation library. The most popular ways to report data to Zipkin are via http or Kafka, though many other options exist, such as Apache ActiveMQ, gRPC and RabbitMQ. The data served to the UI is stored in-memory, or persistently with a supported backend such as Apache Cassandra or Elasticsearch.

Zipkin是一种分布式跟踪系统。 它有助于收集解决服务体系结构中的延迟问题所需的计时数据。 功能包括收集和查找此数据。

Zipkin是Twitter基于google的分布式监控系统Dapper(论文)的开发源实现,zipkin用于跟踪分布式服务之间的应用数据链路,分析处理延时,帮助我们改进系统的性能和定位故障。Dapper论文地址

如果日志文件中有跟踪ID,则可以直接跳转到该文件。 否则,你可以根据服务,操作名称,tag标签和持续时间等属性进行查询。 将为您总结一些有趣的数据,例如在服务中花费的时间占比,以及操作是否失败。

应用程序需要“检测”以向Zipkin报告跟踪数据。 这通常意味着配置跟踪器或检测库。 向Zipkin报告数据的最常用方法是通过http或Kafka,尽管存在许多其他选项,例如Apache ActiveMQ,gRPC和RabbitMQ。 提供给UI的数据存储在内存中,或者持久存储在受支持的后端(如Apache Cassandra或Elasticsearch)中。

本示例中是使用Zipkin中集成的http组件进行发送Span数据。

Springboot 集成 Zipkin

安装启动 zipkin

https://github.com/openzipkin/zipkin 中下载 zipkin.jar

java -jar zipkin.jar

版本说明

框架组件 Version
springboot 2.1.6.RELEASE
zipkin 3.9.0

项目结构

项目采用父工程集成多模块的方式构建而成,demo-zipkin 父工程聚合了zipkin-1zipkin-2zipkin-3zipkin-4zipkin-5 五个 Module。

demo-zipkin
 zipkin-1
 |-SpanCollectorConfig
 |-application.properties
 |-ZipkinController
 |-Application1
 zipkin-2
 |-SpanCollectorConfig
 |-application.properties
 |-ZipkinController2
 |-Application1
 zipkin-3
 |-SpanCollectorConfig
 |-application.properties
 |-ZipkinController3
 |-Application1
 zipkin-4
 |-SpanCollectorConfig
 |-application.properties
 |-ZipkinController4
 |-Application1
 zipkin-5
 |-SpanCollectorConfig
 |-application.properties
 |-ZipkinController5
 |-Application1

工程端口分配

每个 Module 使用不同的端口,分别启动自己的Application。

Module名称 端口 Application
zipkin-1 8081 Application1
zipkin-2 8082 Application2
zipkin-3 8083 Application3
zipkin-4 8084 Application4
zipkin-5 8085 Application5

引入 Maven 依赖

<properties>
 <zipkin.version>3.9.0</zipkin.version>
</properties>

<!-- Springboot 相关 -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
</dependency>

<!-- zipkin相关 -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
 <groupId>io.zipkin.brave</groupId>
 <artifactId>brave-core</artifactId>
 <version>${zipkin.version}</version>
</dependency>
<dependency>
 <groupId>io.zipkin.brave</groupId>
 <artifactId>brave-spancollector-http</artifactId>
 <version>${zipkin.version}</version>
</dependency>
<dependency>
 <groupId>io.zipkin.brave</groupId>
 <artifactId>brave-web-servlet-filter</artifactId>
 <version>${zipkin.version}</version>
</dependency>
<dependency>
 <groupId>io.zipkin.brave</groupId>
 <artifactId>brave-apache-http-interceptors</artifactId>
 <version>${zipkin.version}</version>
</dependency>
<dependency>
 <groupId>io.zipkin.brave</groupId>
 <artifactId>brave-okhttp</artifactId>
 <version>${zipkin.version}</version>
</dependency>

配置文件、收集器的设置

配置 application.properties,以 zipkin-1 为例,其他工程中配置时将 zipkin.serviceNameserver.port更改为 myzipkin-2 …8081...等即可

zipkin.serviceName=myzipkin-1
zipkin.url=http://localhost:9411
zipkin.connectTimeout=6000
zipkin.readTimeout=6000
zipkin.flushInterval=1
zipkin.compressionEnabled=true
zipkin.samplerRate=1

server.port=8081
server.servlet.context-path=/

配置Span收集器

设置收集器的详细参数,包含超时时间、上传span间隔、以及配置采集率等,进而对收集器进行初始化。

package com.anqi.zipkin.bean;

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.okhttp.BraveOkHttpRequestResponseInterceptor;
import com.github.kristofa.brave.servlet.BraveServletFilter;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpanCollectorConfig {

 @Value("${zipkin.url}")
 private String url;

 @Value("${zipkin.serviceName}")
 private String serviceName;

 /*
  连接超时时间
  */
 @Value("${zipkin.connectTimeout}")
 private int connecTimeout;

 /*
  是否启动压缩
  */
 @Value("${zipkin.compressionEnabled}")
 private boolean compressionEnabled;

 /*
  上传 span 的间隔时间
  */
 @Value("${zipkin.flushInterval}")
 private int flushInterval;

 /*
  读取超时时间
  */
 @Value("${zipkin.readTimeout}")
 private int readTimeout;

 @Value("${zipkin.samplerRate}")
 private float samplerRate;

 /**
  * 配置 span 收集器
  * @return
  */
 @Bean
 public SpanCollector spanCollector() {
  Config config = Config.builder()
    .connectTimeout(connecTimeout)
    .compressionEnabled(compressionEnabled)
    .flushInterval(flushInterval)
    .readTimeout(readTimeout)
    .build();

  return HttpSpanCollector.create(url, config, new EmptySpanCollectorMetricsHandler());
 }

 /**
  * 配置采集率
  * @param spanCollector
  * @return
  */
 @Bean
 public Brave brave(SpanCollector spanCollector) {
  Builder builder = new Builder(serviceName);
  builder.spanCollector(spanCollector)
    .traceSampler(Sampler.create(samplerRate))
    .build();
  return builder.build();
 }

 /**
  * @Description: 设置server的(服务端收到请求和服务端完成处理,并将结果发送给客户端)过滤器
  * @Param:
  * @return: 过滤器
  */
 @Bean
 public BraveServletFilter braveServletFilter(Brave brave) {
  BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),
    brave.serverResponseInterceptor(), new DefaultSpanNameProvider());
  return filter;
 }

 /**
  * @Description: 设置client的 rs和cs的拦截器
  * @Param:
  * @return: OkHttpClient 返回请求实例
  */
 @Bean
 public OkHttpClient okHttpClient(Brave brave) {
  OkHttpClient httpClient = new OkHttpClient.Builder()
    .addInterceptor(new BraveOkHttpRequestResponseInterceptor(
      brave.clientRequestInterceptor(),
      brave.clientResponseInterceptor(),
      new DefaultSpanNameProvider())).build();
  return httpClient;
 }
}

编写 Controller 发送请求进行测试

Zipkin-1中的Controller

package com.anqi.zipkin.controller;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("zipkin")
public class ZipkinController {

 public static final String url = "http://localhost:8082/zipkin/service2";

 @Autowired
 OkHttpClient client;

 @GetMapping("/service1")
 public String service() {
  Request request = new Request.Builder().url(url).build();
  Response response;
  try {
   response = client.newCall(request).execute();
   return response.body().string();

  } catch (Exception e) {
   e.printStackTrace();
  }
  return "null";
 }
}

Zipkin-2中的Controller

package com.anqi.zipkin.controller;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("zipkin")
public class ZipkinController2 {
 public static final String url = "http://localhost:8083/zipkin/service3";
 public static final String url2 = "http://localhost:8084/zipkin/service4";

 @Autowired
 OkHttpClient client;

 @GetMapping("/service2")
 public String service() throws Exception {
  System.out.println("loading-----");
  Request request1 = new Request.Builder().url(url).build();
  Request request2 = new Request.Builder().url(url2).build();

  Response response1 = client.newCall(request1).execute();
  Response response2 = client.newCall(request2).execute();
  return "con2 + "+ response1.body().string() + "-" + response2.body().string();
 }
}

Zipkin-3中的Controller

package com.anqi.zipkin.controller;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("zipkin")
public class ZipkinController3 {
 public static final String url = "http://localhost:8084/zipkin/service4";

 @Autowired
 OkHttpClient client;

 @GetMapping("/service3")
 public String service() throws Exception {
  Request request = new Request.Builder().url(url).build();
  Response response = client.newCall(request).execute();
  return "con3 + "+ response.body().string();
 }
}

Zipkin-4中的Controller

package com.anqi.zipkin.controller;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("zipkin")
public class ZipkinController4 {
 public static final String url = "http://localhost:8085/zipkin/service5";

 @Autowired
 OkHttpClient client;

 @GetMapping("/service4")
 public String service() throws Exception {
  Request request = new Request.Builder().url(url).build();
  Response response = client.newCall(request).execute();
  return "con4 + "+ response.body().string();
 }
}

Zipkin-5中的Controller

package com.anqi.zipkin.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("zipkin")
public class ZipkinController5 {

 @GetMapping("/service5")
 public String service() throws Exception {

  return "service5 -----";
 }
}

Springboot 启动类

package com.anqi.zipkin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application1 {
 public static void main(String[] args) {
  SpringApplication.run(Application1.class);
 }
}

运行分析

在地址栏请求urlhttp://localhost:8081/zipkin/service1,然后访问http://localhost:9411/zipkin/

查看服务调用耗时

查看服务依赖关系

核心概念

通过上图可以了解到共有7个span,分别为

{
 "zipkin-1":["Server Start", "Server Finish"],
 "myzipkin-1,myzipkin-2":
  ["Client Start", "Server Start", "Client Finish", "Server Finish"],
 "myzipkin-2,myzipkin-3":
   ["Client Start", "Server Start", "Client Finish", "Server Finish"],
 "myzipkin-3,myzipkin-4":
   ["Client Start", "Server Start", "Client Finish", "Server Finish"],
 "myzipkin-4,myzipkin-5":
   ["Client Start", "Server Start", "Client Finish", "Server Finish"],
 "myzipkin-2,myzipkin-4":
   ["Client Start", "Server Start", "Client Finish", "Server Finish"],
 "myzipkin-4,myzipkin-5":
   ["Client Start", "Server Start", "Client Finish", "Server Finish"],
}

在json文件中选取两个子集进行分析

基本数据:用于跟踪树中节点的关联和界面展示,包括traceId、spanId、parentId、name、timestamp和duration,其中parentId为null的Span将成为跟踪树的根节点来展示,当然它也是调用链的起点,为了节省一次spanId的创建开销,让顶级Span变得更明显,顶级Span中spanId将会和traceId相同。timestamp用于记录调用的起始时间,而duration表示此次调用的总耗时,所以timestamp+duration将表示成调用的结束时间,而duration在跟踪树中将表示成该Span的时间条的长度。需要注意的是,这里的name用于在跟踪树节点的时间条上展示。traceId:标记一次请求的跟踪,相关的Spans都有相同的traceId。kind :zipkin最新V2版本的API中,不再要求在annotations中上传cs,cr,sr,ss。而是通过kind标记是server-side span还是client-side span,两端记录自己的timestap来取代cs和sr,记录duration来取代cr和ss

{
 "traceId": "867a9e3867736b17",
 "parentId": "96f19423db38c90d",
 "id": "6c9fd521b6589b7f",
 "kind": "SERVER",
 "name": "get",
 "timestamp": 1568103422569000,
 "duration": 6000,
 "localEndpoint": {
  "serviceName": "myzipkin-4",
  "ipv4": "192.168.1.160"
 },
 "tags": {
  "http.status_code": "200",
  "http.url": "/zipkin/service4"
 }
 },
 {
 "traceId": "867a9e3867736b17",
 "parentId": "867a9e3867736b17",
 "id": "96f19423db38c90d",
 "kind": "CLIENT",
 "name": "get",
 "timestamp": 1568103422447000,
 "duration": 139000,
 "localEndpoint": {
  "serviceName": "myzipkin-1",
  "ipv4": "192.168.1.160"
 },
 "tags": {
  "http.url": "http://localhost:8082/zipkin/service2"
 }
 }

总结

以上所述是小编给大家介绍的SpringBoot集成Zipkin实现分布式全链路监控,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

时间: 2019-09-08

SpringBoot2.0 ZipKin示例代码

Zipkin是一种分布式跟踪系统.它有助于收集解决微服务架构中延迟问题所需的时序数据.它管理这些数据的收集和查找.Zipkin的设计基于 Google Dapper论文. 应用程序用于向Zipkin报告时间数据.Zipkin用户界面还提供了一个依赖关系图,显示每个应用程序有多少跟踪请求.如果您正在解决延迟问题或错误问题,则可以根据应用程序,跟踪长度,注释或时间戳过滤或排序所有跟踪.选择跟踪后,您可以看到每个跨度所需的总跟踪时间百分比,从而可以识别问题应用程序. 这是翻译过来的原意,自己在这里想如

SpringBoot2.3集成ELK7.1.0的示例代码

最近想用ELK做日志分析,所以先写了Demo来实验一下! 1.安装ELK(Elasticsearch+Logstash+Kibana),具体安装教程百度 2.查看是否安装成功,输入localhost:9200,localhost:5601,如下页面则安装成功 3.pom包依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM

SpringBoot2.0整合WebSocket代码实例

这篇文章主要介绍了SpringBoot2.0整合WebSocket代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 之前公司的某个系统为了实现推送技术,所用的技术都是Ajax轮询,这种方式浏览器需要不断的向服务器发出请求,显然这样会浪费很多的带宽等资源,所以研究了下WebSocket,本文将详细介绍下. 一.什么是WebSocket? WebSocket是HTML5开始提供的一种在单个TCP连接上进行全双工通讯的协议,能更好的节省服务器资

springboot2.0整合dubbo的示例代码

写在前面: 使用springboot作为web框架,方便开发许多,做分布式开发,dubbo又不可少,那么怎么整合在一起呢, 跟我学一遍,至少会用 注意,springboot2.0和springboot1.x与dubbo整合不一样, 1.环境 1.新建一个空的maven项目,作为父工程,新建moudle,,service(接口层,及实现层,没有具体分,),web(web层,springboot项目) 项目结构如下 父pom如下 <properties> <project.build.sou

springboot2.0集成rabbitmq的示例代码

安装rabbitmq 简介: rabbitmq即一个消息队列,主要用来实现应用程序的异步和解耦,消息缓冲,消息分发的作用. 由于rabbitmq依赖于erlang语言,所以先安装erlang: 添加erlang solutions源 $ wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm $ sudo rpm -Uvh erlang-solutions-1.0-1.noarch.rpm $ su

springboot2 生产部署注意事项及示例代码

注意事项 1. 去除不需要的 jar 开发工具 jar :springs-boot-devtools 2. 监控一定要做好权限制或者去除 控制 jar :spring-boot-starter-actuator 监控 druid 的监控 swagger的接口 3 打包的时候要跳过测试 maven : clean package -Dmaven.test.skip=true 打包脚本示例: #!/bin/sh ## chang here SERVICE_DIR=/roncoo/spring-boo

Android 7.0 监听网络变化的示例代码

Android7.0前,Android系统前网络切换时,会发广播,业务只要监听广播即可. public class NetChangeReceiver extends BroadcastReceiver { private static final String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; @Override public void onReceive(Context conte

在Linux中安装ASPNET.Core3.0运行时的示例代码

# 以下示例适用于x64位runtime v3.0.0 mkdir /runtimes cd /runtimes wget https://download.visualstudio.microsoft.com/download/pr/b0c44e05-b7a1-4221-94ec-a0c0d3a11eed/afc61567dd6db8f097e244871889458c/aspnetcore-runtime-3.0.0-linux-x64.tar.gz mkdir aspnetcore-run

vue2.0自定义指令示例代码详解

1.什么是指令? 指令通常以"v-"作为前缀, 以方便Vue知道你在使用一种特殊的标记. 除了 Vue 核心携带着的一些默认指令(v-model 和 v-show)之外, Vue 还允许你注册自己的自定义指令.某些情况下,还是需要对普通元素进行一些底层 DOM 访问, 这也是自定义指令仍然有其使用场景之处. 2.全局指令: 当页面加载时,元素将获取焦点,事实上,在访问页面时,如果你还没有点击任何地方,上面的输入框现在应该处于获取焦点的状态.现在让我们构建指令以完成此效果: <te

vue2.0使用swiper组件实现轮播的示例代码

1.安装swiper npm install swiper@3.4.1 --save-dev 2.引用组件 import Swiper from 'swiper'; import 'swiper/dist/css/swiper.min.css'; 3.html页面代码 <div class="swiper-container" id="swiper"> <div class="swiper-wrapper"> <di