转载请标明出处: 
http://blog.csdn.net/forezp/article/details/81041078 
本文出自方志朋的博客

这篇文章主要讲述服务追踪组件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 requests.

—— 摘自官网

Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可。

二、服务追踪分析

微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。

随着服务的越来越多,对调用链的分析会越来越复杂。它们之间的调用关系也许如下:

三、术语

  • Span:基本工作单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span通过一个64位ID唯一标识,trace以另一个64位ID表示,span还有其他数据信息,比如摘要、时间戳事件、关键值注释(tags)、span的ID、以及进度ID(通常是IP地址) 
    span在不断的启动和停止,同时记录了时间信息,当你创建了一个span,你必须在未来的某个时刻停止它。
  • Trace:一系列spans组成的一个树状结构,例如,如果你正在跑一个分布式大数据工程,你可能需要创建一个trace。
  • Annotation:用来及时记录一个事件的存在,一些核心annotations用来定义一个请求的开始和结束 
    • cs - Client Sent -客户端发起一个请求,这个annotion描述了这个span的开始
    • sr - Server Received -服务端获得请求并准备开始处理它,如果将其sr减去cs时间戳便可得到网络延迟
    • ss - Server Sent -注解表明请求处理的完成(当请求返回客户端),如果ss减去sr时间戳便可得到服务端需要的处理请求时间
    • cr - Client Received -表明span的结束,客户端成功接收到服务端的回复,如果cr减去cs时间戳便可得到客户端从服务端获取回复的所有所需时间 
      将Span和Trace在一个系统中使用Zipkin注解的过程图形化:

将Span和Trace在一个系统中使用Zipkin注解的过程图形化:

四、构建工程

基本知识讲解完毕,下面我们来实战,本文的案例主要有三个工程组成:一个server-zipkin,它的主要作用使用ZipkinServer 的功能,收集调用数据,并展示;一个service-hi,对外暴露hi接口;一个service-miya,对外暴露miya接口;这两个service可以相互调用;并且只有调用了,server-zipkin才会收集数据的,这就是为什么叫服务追踪了。

4.1 构建server-zipkin

在spring Cloud为F版本的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可,下载地址:

https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

也可以在这里下载:

链接: https://pan.baidu.com/s/1w614Z8gJXHtqLUB6dKWOpQ 密码: 26pf

下载完成jar 包之后,需要运行jar,如下:

java -jar zipkin-server-2.10.1-exec.jar

访问浏览器localhost:9494

4.2 创建service-hi

在其pom引入起步依赖spring-cloud-starter-zipkin,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.forezp</groupId><artifactId>service-zipkin</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-hi</name><description>Demo project for Spring Boot</description><parent><groupId>com.forezp</groupId><artifactId>sc-f-chapter9</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

在其配置文件application.yml指定zipkin server的地址,头通过配置“spring.zipkin.base-url”指定:

server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就可以了。

对外暴露接口:

package com.forezp;import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.logging.Level;
import java.util.logging.Logger;@SpringBootApplication
@RestController
public class ServiceHiApplication {public static void main(String[] args) {SpringApplication.run(ServiceHiApplication.class, args);}private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName());@Autowiredprivate RestTemplate restTemplate;@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}@RequestMapping("/hi")public String callHome(){LOG.log(Level.INFO, "calling trace service-hi  ");return restTemplate.getForObject("http://localhost:8989/miya", String.class);}@RequestMapping("/info")public String info(){LOG.log(Level.INFO, "calling trace service-hi ");return "i'm service-hi";}@Beanpublic Sampler defaultSampler() {return Sampler.ALWAYS_SAMPLE;}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

4.3 创建service-miya

创建过程痛service-hi,引入相同的依赖,配置下spring.zipkin.base-url。

对外暴露接口:


package com.forezp;import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.logging.Level;
import java.util.logging.Logger;@SpringBootApplication
@RestController
public class ServiceMiyaApplication {public static void main(String[] args) {SpringApplication.run(ServiceMiyaApplication.class, args);}private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName());@RequestMapping("/hi")public String home(){LOG.log(Level.INFO, "hi is being called");return "hi i'm miya!";}@RequestMapping("/miya")public String info(){LOG.log(Level.INFO, "info is being called");return restTemplate.getForObject("http://localhost:8988/info",String.class);}@Autowiredprivate RestTemplate restTemplate;@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}@Beanpublic Sampler defaultSampler() {return Sampler.ALWAYS_SAMPLE;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

4.4 启动工程,演示追踪

依次启动上面的工程,打开浏览器访问:http://localhost:9411/,会出现以下界面:

访问:http://localhost:8989/miya,浏览器出现:

i’m service-hi

再打开http://localhost:9411/的界面,点击Dependencies,可以发现服务的依赖关系:

点击find traces,可以看到具体服务相互调用的数据:

本文源码下载:

https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter9

五、参考资料

spring-cloud-sleuth

http://blog.csdn.net/forezp/article/details/70162074

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接 ,博主地址:http://blog.csdn.net/forezp。 https://blog.csdn.net/forezp/article/details/81041078

SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)相关推荐

  1. 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

    转载请标明出处: http://blog.csdn.net/forezp/article/details/81041078 本文出自方志朋的博客 个人博客纯净版:https://www.fangzhi ...

  2. 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)

    转:https://blog.csdn.net/forezp/article/details/70162074 这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了z ...

  3. 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案, ...

  4. 《深入理解 Spring Cloud 与微服务构建》第十四章 服务链路追踪 Spring Cloud Sleuth

    <深入理解 Spring Cloud 与微服务构建>第十四章 服务链路追踪 Spring Cloud Sleuth 文章目录 <深入理解 Spring Cloud 与微服务构建> ...

  5. SpringCloud教程- 服务链路追踪(Spring Cloud Sleuth)(SpringCloud版本Greenwich.SR4)

    文章目录 一.Sleuth简介 二.为何使用Sleuth 三.构建工程 server-zipkin zipkin-serivce-hi zipkin-server-hello 四. 启动工程演示 代码 ...

  6. SpringCloud 教程 (二) 服务链路追踪(Spring Cloud Sleuth)

    一.简介 Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle exampl ...

  7. SpringCloud: 服务链路追踪(Spring Cloud Sleuth)

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且 ...

  8. 企业分布式微服务云SpringCloud SpringBoot mybatis (九)服务链路追踪(Spring Cloud Sleuth)...

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Add sleuth to the classpath of a Spring Bo ...

  9. springcloud上传文件_Spring Cloud实战:服务链路追踪Spring Cloud Sleuth

    推荐阅读: Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC 一个SpringBoot问题就干趴下了?我却凭着这份PDF文档吊打面试官. 前 ...

最新文章

  1. 大数据分布式集群搭建(8)
  2. 【拾贝】版本控制-git 建立Team项目
  3. PHP知识点 自己做个记录
  4. 人体特征点检测解决方案
  5. 仿iOS Segmented Control样式
  6. 第九届中国开源黑客松活动将于2019年4月18日-4月20日,在深圳举办
  7. 视频格式基础知识:让你了解MKV、MP4、H.265、码率、色深等等
  8. mybatis mysql begin end_mybatis批量操作
  9. 苹果27寸一体机拆机图解_21.5/27寸新iMac完全拆解:维修要你命
  10. datacom-HCIP之题库继续解析
  11. 送书 | 令附生信专用简明 Python 文字和视频教程
  12. IRT和DINA模型学习总结
  13. 阿里云服务器的使用以及虚拟机创建用户
  14. Pygame中文文档
  15. Vue 项目打包部署实战完整流程总结!
  16. 浅谈游戏中台-我眼中的supercell为何成功?
  17. Integrating Caching Techniques in CDNs using a Classification Approach
  18. 8个快速提高用户忠诚度的套路,运营人必备
  19. 2022-2028年中国无刷直流电机制造行业市场研究分析及投资策略研究报告
  20. 按生辰八字给宝宝取名的方法步骤

热门文章

  1. 阅读使人进步《我的书单》
  2. 聊一聊浏览器缓存机制
  3. 声纹识别X-Vector
  4. U盘被写保护的处理办法
  5. matlab水印嵌入算法,一个基于dwt和hvs的数字水印算法嵌入源代码 水印嵌入不了!!!...
  6. 【LSTM预测】基于matlab鲸鱼算法优化双向长短时记忆BiLSTM航空发动机寿命预测 (多输入单输出)【含Matlab源码 2288期】
  7. 《巴菲特的护城河》书中的精髓:如何识别有投资价值的公司,让你投资的钱不再打水漂?
  8. enet分割_关于人体分割的训练Enet
  9. 东南大学计算机学院通知,2020下半年江苏东南大学计算机等级考试报名通知
  10. mess组网 中继_MESH无线自组网对比无线中继区别图