全链路spring cloud sleuth+zipkin

http://blog.csdn.net/qq_15138455/article/details/72956232

版权声明:@入江之鲸

一、About ZipKin

please google

二、 Demo Scene




三、 Result Display


四、Prepare

1、soft version

kafka:2.10-0.10.2.0
zokeeper:3.4.10
elasticsearch:5.2.2
jdk:1.8
spring boot:1.5.3.RELEASE
sprign cloud:Dalston.RELEASE
rabbit mq:3.6.9

2、install

kafka+zookeeper
elasticsearch
rabbit mq
mysql

3、create four spring cloud project

web-api、user-api、order-api、zipkin
ps:

why i will create zipkin project use spring boot by myself not use  zipkin.jar from http://zipkin.io/,actually,zipkin.jar is a spring boot project,check it‘s dependency lib you will find it din‘t use spring-cloud-sleuth-stream,but i will send trace info to kafka for zipkin server collector ,so i must use spring-cloud-sleuth-stream in my service

and the message send to kafka is a  sleuth.span object and use kafka default serialized,but zipkin.jar only receive zipkin.span and json or thrift encode,so it‘s not matching,That‘s the reason i create zipkin server

but if you use rabbit mq,that‘s no problem.

4、configuration

4.1、the service web-api、user-api、order-api config part like:
pom.xml

[html] view plain copy

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-stream-kafka</artifactId>
  8. </dependency>

application.properties

[html] view plain copy

  1. spring.sleuth.sampler.percentage=1.0
  2. spring.cloud.stream.kafka.binder.brokers=10.20.1.11:9092,10.20.1.12:9092
  3. spring.cloud.stream.kafka.binder.zkNodes=10.20.1.11:2181,10.20.1.12:2181

4.2、the zipkinconfig part like:

pom.xml

[html] view plain copy

  1. <!-- the  first one dependency below,In principle, there is no need,beause sleuth-zipkin-stream 1.5.3 will  Introduce zipkin version1.19 Automaticly,but 1.19 only support  elasticsearch version 2.X -->
  2. <dependency>
  3. <groupId>io.zipkin.java</groupId>
  4. <artifactId>zipkin</artifactId>
  5. <version>1.24.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.cloud</groupId>
  13. <artifactId>spring-cloud-starter-stream-kafka</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>io.zipkin.java</groupId>
  17. <artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
  18. <version>1.24.0</version>
  19. <optional>true</optional>
  20. </dependency>

application.properties

[html] view plain copy

  1. #kafka config
  2. spring.sleuth.enabled=false
  3. spring.sleuth.sampler.percentage=1.0
  4. spring.cloud.stream.kafka.binder.brokers=10.20.1.11:9092,10.20.1.12:9092
  5. spring.cloud.stream.kafka.binder.zkNodes=10.20.1.11:2181,10.20.1.12:2181
  6. #elasticsearch config
  7. zipkin.storage.type=elasticsearch
  8. zipkin.storage.elasticsearch.hosts=10.20.1.11:9200,10.20.1.12:9200
  9. zipkin.storage.elasticsearch.cluster=elasticsearch
  10. zipkin.storage.elasticsearch.index=zipkin
  11. zipkin.storage.elasticsearch.index-shards=5
  12. zipkin.storage.elasticsearch.index-replicas=1

ZipKin Server Startup class configuration

[java] view plain copy

  1. <span style="font-size:14px;">@SpringBootApplication
  2. //@EnableZipkinServer //this is used by interface receive trace info
  3. @EnableZipkinStreamServer //can be used kafka,rabbit
  4. public class ZkingApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ZkingApplication.class, args);
  7. }
  8. }</span>

五、Demo DownLoad

click me

by the way,spring cloud is a pretty boy,i like its combination of terseness and elegance

六、补充

如果kafka没有启动,spring boot会启动失败,这个异常处理设计的真是缺德

/**
 * 1、修改背景 
 * 因kafka节点没有启动 在spring boot启动时初始化outputBindingLifecycle、inputBindingLifecycle
 * 两个bean时候连接kafka失败,向外抛出了异常直到EmbeddedWebApplicationContext类
 * 捕获处理,处理方式为:stopAndReleaseEmbeddedServletContainer()导致整个应用停止启动 
 * 2、修改方案
 * 干预上面两个bean的初始化,在连接kafka异常时,将异常处理掉,不向上层抛出
 * 3、修改步骤
 * 3.1、使用自定义MyBindingLifecycle的bean将BindingServiceConfiguration中的两个bean初始化替换掉
 * 3.2、在自定bean中启动线程MyBindingThread来控制两个bean的初始化
 * 4、解决启动问题之后,实际上kafka还是没有连接的,此时向kafka发送span时会失败,默认的处理方案是捕获到异常之后使用
 *   handleError处理,再次发送新的span,这就导致循环发送
 *   参见:ErrorHandlingTaskExecutor中的execute方法
 *   catch (Throwable t) 
 *   {
 *        ErrorHandlingTaskExecutor.this.errorHandler.handleError(t);
 *     }
 * 5、解决方案
 * 重写ErrorHandler的handleError方法
 * 6、跟踪代码发现
 * 跟踪发现ErrorHandler对线对象是在SourcePollingChannelAdapterFactoryBean初始化时候设置的
 * spca.setErrorHandler(this.pollerMetadata.getErrorHandler());
 * 进一步发现是在pollerMetadata对象中,所以需要在pollerMetadata对象初始化时候做修改
 * 7、修改步骤
 * 自定义MyPollerMetadata且需要@Configuration,重写handleError方法如下
 * @author zhangdingxin、yangxi
 */

时间: 2024-12-09 02:23:47

全链路spring cloud sleuth+zipkin的相关文章

Spring Cloud Sleuth + zipkin 实现服务追踪

服务追踪 Spring Cloud Sleuth实现了一种分布式的服务链路跟踪解决方案,通过使用Sleuth可以让我们快速定位某个服务的问题. 官方文档地址如下: http://cloud.spring.io/spring-cloud-static/spring-cloud-sleuth/2.0.1.RELEASE/single/spring-cloud-sleuth.html 一些概念: Span,Span是基本的工作单元.Span包括一个64位的唯一ID,一个64位trace码,描述信息,时

springcloud --- spring cloud sleuth和zipkin日志管理(spring boot 2.18)

前言 在spring cloud分布式架构中,系统被拆分成了许多个服务单元,业务复杂性提高.如果出现了异常情况,很难定位到错误位置,所以需要实现分布式链路追踪,跟进一个请求有哪些服务参与,参与的顺序如何,从而去明确一个问题. spring cloud sleuth 通常来说,一个分布式服务跟踪系统主要由三部分:数据收集.数据存储和数据展示. 对于大规模的分布式系统来说,数据存储可分为实时数据和全量数据两部分.实时数据用来排查故障,全量数据用于系统优化:数据展示涉及数据挖掘和分析. 名词解释 服务

跟我学SpringCloud | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪

SpringCloud系列教程 | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如无特殊说明,本系列教程全采用以上版本 在分布式服务架构中,需要对分布式服务进行治理--在分布式服务协同向用户提供服务时,每个请求都被哪些服务处理?在遇到问题时,在调用哪个服务上发生了问题?在分析性能时,调用各个服务都花了多长时间?哪些调用可以并行执行?-- 为此,分布式

spring cloud 入门系列八:使用spring cloud sleuth整合zipkin进行服务链路追踪

好久没有写博客了,主要是最近有些忙,今天忙里偷闲来一篇. =======我是华丽的分割线========== 微服务架构是一种分布式架构,微服务系统按照业务划分服务单元,一个微服务往往会有很多个服务单元,一个请求往往会有很多个单元参与,一旦请求出现异常,想要去定位问题点真心不容易,因此需要有个东西去跟踪请求链路,记录一个请求都调用了哪些服务单元,调用顺序是怎么样的以及在各个服务单元处理的时间长短.常见的服务链路追踪组件有google的dapper.twitter的zipkin.阿里的鹰眼等,它们

业余草 SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle examples), and you will see the correlation data being collected in logs, as long as you are logging re

Spring Cloud Sleuth 之Greenwich版本全攻略

微服务架构是一个分布式架构,微服务系统按业务划分服务单元,一个微服务系统往往有很多个服务单元.由于服务单元数量众多,业务的复杂性较高,如果出现了错误和异常,很难去定位.主要体现在一个请求可能需要调用很多个服务,而内部服务的调用复杂性决定了问题难以定位.所以在微服务架构中,必须实现分布式链路追踪,去跟进一个请求到底有哪些服务参与,参与的顺序又是怎样的,从而达到每个请求的步骤清晰可见,出了问题能够快速定位的目的. 在微服务系统中,一个来自用户的请求先到达前端A(如前端界面),然后通过远程调用,到达系

Distributed traceability with Spring Cloud: Sleuth and Zipkin

I. Sleuth 0. Concept Trace A set of spans that form a call tree structure, forms the trace of the request. Span It is the basic unit of work, for example a call to a service. They are identified with a span ID and a trace ID to which span is owned. T

【Spring Cloud】Spring Cloud之Spring Cloud Sleuth,分布式服务跟踪(1)

一.Spring Cloud Sleuth组件的作用 为微服务架构增加分布式服务跟踪的能力,对于每个请求,进行全链路调用的跟踪,可以帮助我们快速发现错误根源以及监控分析每条请求链路上的性能瓶颈等. 二.项目中如何引入Spring Cloud Sleuth组件1)增加spring-cloud-starter-sleuth依赖 <!-- sleuth--> <dependency> <groupId>org.springframework.cloud</groupId

浅尝Spring Cloud Sleuth

Spring Cloud Sleuth提供了分布式追踪(distributed tracing)的一个解决方案.其基本思路是在服务调用的请求和响应中加入ID,标明上下游请求的关系.利用这些信息,可以方便地分析服务调用链路和服务间的依赖关系. Only Sleuth 在Spring Tool Suite的文件菜单中,点击新建Spring Starter Project. 在请求处理方法内加上一行日志代码. import org.slf4j.Logger; import org.slf4j.Logg