转载请标明出处:
原文首发于:>https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f9-sleuth/
本文出自方志朋的博客

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

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

server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi

通过引入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;}}

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;}
}

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


扫码关注有惊喜

(转载本站文章请注明作者和出处 方志朋的博客)

转载于:https://www.cnblogs.com/forezp/p/9852079.html

史上最简单的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. SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

    转载请标明出处:  http://blog.csdn.net/forezp/article/details/81041078  本文出自方志朋的博客 这篇文章主要讲述服务追踪组件zipkin,Spri ...

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

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

  5. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/ 本文出自方志朋的博客 在上一篇文章,讲了 ...

  6. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

    转载请标明出处: https://www.fangzhipeng.com/springcloud/2017/07/12/sc02-rest-ribbon/ 本文出自方志朋的博客 最新Finchley版 ...

  7. 最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  8. 史上最简单的 SpringCloud 教程

    关注公众号"风色年代"订阅更多精彩文章,本博大部分文章为转载并已标明原文出处,如有再转敬请保留,请自觉尊重原创作者的劳动成果! https://blog.csdn.net/fore ...

  9. 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)

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

  10. 史上最简单的git教程|第九篇:分支管理工具

    在上一篇我们讲到了如何创建合并分支.如何解决冲突以及如何删除分支,那么接下来我们将讲到一些常用的分支管理工具. 获取所有分支列表: $ git branch * b1master 可以看书一共有两个分 ...

最新文章

  1. 2019第四周作业(基础作业+挑战作业)
  2. 这款开源测试神器,圆了我玩游戏不用动手的梦想
  3. Python环境出现模块找不到
  4. 招聘 Java 和 前端工程师
  5. CVPR 2021 | 自适应激活函数ACON:统一ReLU和Swish的新范式
  6. java泛型解析(转)
  7. Java探索之旅(6)——对象和类
  8. mysql中有没有单行函数_MySQL之函数(单行函数、分组函数)
  9. bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘
  10. Linux+QT界面开发(含数据库)小结
  11. l380废墨收集垫已到使用寿命_湖北土工网垫
  12. OSI参考模型(应用层、表示层、会话层、传输层、网络层、数据链路层、物理层)...
  13. PCB板设计后期处理流程及工作步骤有哪些?2021-07-29
  14. Mac 打开大小写切换很慢
  15. 教育学外文文献是哪里找的?
  16. 战争调度(树形DP+BFS)
  17. MySQL技巧-EXPLAIN输出格式
  18. 编程语言评价标准:冯诺伊曼体系
  19. 各行业领域的最全最好的网址导航大全,持续收集中,真是太好了
  20. 【模型开发】评分卡应用

热门文章

  1. matlab分析具体问题论文,matlab论文12010245327马文建.doc
  2. mysql查询4-6_MySQL学习(四)查询
  3. java date只保留年月日_入门之JAVA爬虫
  4. 让mysql timeStamp类型支持默认值0000-00-00 00:00:00
  5. coxbox变换 python_怎样用Python实现对Xbox手柄的控制?
  6. 【渝粤教育】国家开放大学2019年春季 2766养羊技术 参考试题
  7. 【渝粤教育】国家开放大学2018年春季 0063-21T中国现当代文学专题 参考试题
  8. DataSet本地化数据的二表链接操作
  9. 23种设计模式(十三)接口隔离之门面模式
  10. 系统类配置(三)【ubuntu14.04或者ubuntu16.04 配置caffe】