一、为什么需要Spring Cloud Sleuth

微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元。由于服务单元数量众多,业务的复杂性,如果出现了错误和异常,很难去定位。主要体现在,一个请求可能需要调用很多个服务,而内部服务的调用复杂性,决定了问题难以定位。所以微服务架构中,必须实现分布式链路追踪,去跟进一个请求到底有哪些服务参与,参与的顺序又是怎样的,从而达到每个请求的步骤清晰可见,出了问题,很快定位。

举个例子,在微服务系统中,一个来自用户的请求,请求先达到前端A(如前端界面),然后通过远程调用,达到系统的中间件B、C(如负载均衡、网关等),最后达到后端服务D、E,后端经过一系列的业务逻辑计算最后将数据返回给用户。对于这样一个请求,经历了这么多个服务,怎么样将它的请求过程的数据记录下来呢?这就需要用到服务链路追踪。

Google开源的 Dapper链路追踪组件,并在2010年发表了论文《Dapper, a Large-Scale Distributed Systems Tracing Infrastructure》,这篇文章是业内实现链路追踪的标杆和理论基础,具有非常大的参考价值。
目前,链路追踪组件有Google的Dapper,Twitter 的Zipkin,以及阿里的Eagleeye (鹰眼)等,它们都是非常优秀的链路追踪开源组件。

本文主要讲述如何在Spring Cloud Sleuth中集成Zipkin。在Spring Cloud Sleuth中集成Zipkin非常的简单,只需要引入相应的依赖和做相关的配置即可。

二、基本术语

Spring Cloud Sleuth采用的是Google的开源项目Dapper的专业术语。

  • Span:基本工作单元,发送一个远程调度任务 就会产生一个Span,Span是一个64位ID唯一标识的,Trace是用另一个64位ID唯一标识的,Span还有其他数据信息,比如摘要、时间戳事件、Span的ID、以及进度ID。
  • Trace:一系列Span组成的一个树状结构。请求一个微服务系统的API接口,这个API接口,需要调用多个微服务,调用每个微服务都会产生一个新的Span,所有由这个请求产生的Span组成了这个Trace。
  • Annotation:用来及时记录一个事件的,一些核心注解用来定义一个请求的开始和结束 。这些注解包括以下:
    • cs - Client Sent -客户端发送一个请求,这个注解描述了这个Span的开始
    • sr - Server Received -服务端获得请求并准备开始处理它,如果将其sr减去cs时间戳便可得到网络传输的时间。
    • ss - Server Sent (服务端发送响应)–该注解表明请求处理的完成(当请求返回客户端),如果ss的时间戳减去sr时间戳,就可以得到服务器请求的时间。
    • cr - Client Received (客户端接收响应)-此时Span的结束,如果cr的时间戳减去cs时间戳便可以得到整个请求所消耗的时间。

三、代码实战

(1)下载zipkin-server服务器

使用Spring Cloud为Finchley版的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可,
下载地址: https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server

zipkin-server服务器的启动方式:java -jar zipkin-server-2.10.2-exec.jar

(2)新建两个模块service-consumer-sleuth-8767和service-consumer-sleuth-8768

本文的案例主要有三个工程组成:一个server-zipkin它的主要作用使用ZipkinServer的功能,收集调用数据;新建一个服务
service-sleuth-8768对外暴露hi接口;新建另一个服务service-sleuth-8767对外暴露hi接口,这两个service可以相互调用;

【模块service-consumer-sleuth-8767】

pom.xml

application.yml

server:
port: 8767
spring:
zipkin:
base-url: http://localhost:9411
application:
name: service-sleuth-8767

主启动类

ServiceSleuth8767Application.java

package com.xu.serviceconsumer;

import brave.sampler.Sampler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
public class ServiceSleuth8767Application { public static void main(String[] args) { SpringApplication.run(ServiceSleuth8767Application.class, args);
} @Bean
@LoadBalanced
RestTemplate restTemplate() { return new RestTemplate();
} @Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}

建立一个测试的Controller

SleuthController.java

package com.xu.serviceconsumer.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class SleuthController { private static final Logger LOG = LoggerFactory.getLogger(SleuthController.class); @Autowired
private RestTemplate restTemplate; @RequestMapping("/hi")
public String callHome() {
LOG.info("calling trace service-hi ");
return restTemplate.getForObject("http://localhost:8768/info", String.class);
} @RequestMapping("/info")
public String info() {
LOG.info("calling trace service-sleuth-8767 ");
return "i'm ervice-sleuth-8767"; } }

【模块service-consumer-sleuth-8768】

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xu</groupId>
<artifactId>service-consumer-sleuth-8767</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-consumer-sleuth-8767</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.M3</spring-cloud.version>
</properties> <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> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.48</version>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories> </project>

application.yml

server:
port: 8768
spring:
zipkin:
base-url: http://localhost:9411
application:
name: service-sleuth-8768

主启动类ServiceSleuth8768Application.java

package com.xu.serviceconsumer;

import brave.sampler.Sampler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
public class ServiceSleuth8768Application { public static void main(String[] args) { SpringApplication.run(ServiceSleuth8768Application.class, args);
} @Bean
@LoadBalanced
RestTemplate restTemplate() { return new RestTemplate();
} @Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}

SleuthController.java

package com.xu.serviceconsumer.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class SleuthController { private static final Logger LOG = LoggerFactory.getLogger(SleuthController.class); @Autowired
private RestTemplate restTemplate; @RequestMapping("/hi")
public String callHome() {
LOG.info("calling trace service-hi ");
return restTemplate.getForObject("http://localhost:8767/info", String.class);
} @RequestMapping("/info")
public String info() {
LOG.info("calling trace service-sleuth-8768 ");
return "i'm ervice-sleuth-8768"; } }

在启动ZipkinServer后再启动两个服务

现在我们试着访问一下这个请求http://localhost:8767/hi,可以看到成功访问了另一个服务

再一次打开的zipkin-server服务的管理界面 http://localhost:9411 点击Dependencies可以发现service-sleuth-8767服务调用了service-sleuth-8768服务的依赖关系,还有调用的时间

点击截图上两个方框,即可弹出两个详细依赖的信息弹窗

点击【Zipkin】--->【查找】可以看到刚才调用的追踪信息

这里是其中一个点击进入后的信息内容

读者可以再请求一下http://localhost:8768/hi试试,然后刷新ZinpkinServer管理页面看看里面又发生了什么变化;到了这里,SpringCloudSleuth入门就到这里结束了,谢谢大家的围观,最后附上一张调用http://localhost:8768/hi后的ZipkinServrer管理页面依赖的截图

===============================================================================

如果您觉得此文有帮助,可以打赏点钱给我支付宝或扫描二维码

SpringCloud学习之Sleuth服务链路跟踪(十二)的更多相关文章

  1. ⑦SpringCloud 实战:引入Sleuth组件,完善服务链路跟踪

    这是SpringCloud实战系列中第7篇文章,了解前面第两篇文章更有助于更好理解本文内容: ①SpringCloud 实战:引入Eureka组件,完善服务治理 ②SpringCloud 实战:引入F ...

  2. Zipkin和微服务链路跟踪

    https://cloud.tencent.com/developer/article/1082821 Zipkin和微服务链路跟踪 本期分享的内容是有关zipkin和分布式跟踪的内容. 首先,我们还 ...

  3. 服务链路跟踪 && 服务监控

    服务链路跟踪 背景 微服务以微出名,在实际的开发过程中,涉及到成百上千个服务,网络请求引起服务之间的调用极其复杂. 当请求不可用或者变慢时,需要及时排查出故障服务点成为了微服务维护的一大难关. 服务链 ...

  4. SpringCloud学习笔记:服务支撑组件

    SpringCloud学习笔记:服务支撑组件 服务支撑组件 在微服务的演进过程中,为了最大化利用微服务的优势,保障系统的高可用性,需要通过一些服务支撑组件来协助服务间有效的协作.各个服务支撑组件的原理 ...

  5. SpringCloud学习之sleuth&zipkin【二】

    这篇文章我们解决上篇链路跟踪的遗留问题 一.将追踪数据存放到MySQL数据库中 默认情况下zipkin将收集到的数据存放在内存中(In-Memeroy),但是不可避免带来了几个问题: 在服务重新启动后 ...

  6. SpringCloud学习成长之 九 服务链路跟踪

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

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

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

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

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

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

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 注意情况: 该案例使用的spring-boot版本1.5.x,没使用2.0.x, 另外本文图3 ...

随机推荐

  1. java创建线程方式

    1.继承Thread类 public class ThreadCreator extends Thread{ public static void main(String[] args) { //第一 ...

  2. java web开发缓存方案,ehcache和redis哪个更好

    Ehcache在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS中取出来的高花费.高延迟采取的一种缓存方案.正因为Ehcache具有健壮性(基于java开发).被认证(具有apac ...

  3. Lamda简单使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. SciPy 图像处理

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  5. SciPy 教程

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  6. 2018--Linux面试题

    1.企业场景面试题:buffer与Cache的区别. 2.企业场景面试题:redhat与CentOS的区别. 3.企业场景面试题:  描述RAID 0 1 5 10的特点. 4.企业场景面试题:32位 ...

  7. LoadRunner接口测试

    [转自http://www.51testing.com/html/87/300987-805230.html] Action(){ //首先调用web_reg_find()这个注册函数,我们接口的正常 ...

  8. flutter如何使用配置文件pubspec.yaml(位于项目根目录)来管理第三方依赖包

    官方文档 在软件开发中,很多时候有一些公共的库或SDK可能会被很多项目用到,因此,将这些代码单独抽到一个独立模块,然后哪个项目需要使用时再直接集成这个模块,便可大大提高开发效率.很多编程语言或开发工具 ...

  9. Readiness 探测【转】

    除了 Liveness 探测,Kubernetes Health Check 机制还包括 Readiness 探测. 用户通过 Liveness 探测可以告诉 Kubernetes 什么时候通过重启容 ...

  10. 开发自己的 chart【转】

    Kubernetes 给我们提供了大量官方 chart,不过要部署微服务应用,还是需要开发自己的 chart,下面就来实践这个主题. 创建 chart 执行 helm create mychart 的 ...