【SpringCloud】 第九篇: 服务链路追踪(Spring Cloud Sleuth)
前言:
必需学会SpringBoot基础知识
简介:
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。
工具:
JDK8
apache-maven-3.5.2
IntelliJ IDEA 2018.1 x64
一、简介
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注解的过程图形化:
四、构建工程
基本知识讲解完毕,下面我们来实战,本文的案例主要有三个工程组成:一个eureka-server-zipkin,它的主要作用使用ZipkinServer 的功能,收集调用数据,并展示;一个eureka-server-zipkin-client,对外暴露hi接口;一个eureka-server-zipkin-eddie,对外暴露eddie接口;这两个service可以相互调用;并且只有调用了,eureka-server-zipkin才会收集数据的,这就是为什么叫服务追踪了。
理论差不多, 就不继续探讨, 都是那句老话: 师傅带入门, 本领看自己
4.1 创建 eureka-server-zipkin
pom引入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <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> <dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency> <dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在其程序入口类, 加上注解@EnableZipkinServer,开启ZipkinServer的功能:
package com.lwc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.EnableZipkinServer; /**
* @author Eddie
*/
@EnableZipkinServer
@SpringBootApplication
public class EurekaServerZipkinApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerZipkinApplication.class, args);
}
}
在配置文件application.yml指定服务端口为:
server:
port: 9411
4.2 创建 eureka-server-zipkin-client
在其pom引入起步依赖spring-cloud-starter-zipkin,代码如下:
<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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在其配置文件application.yml指定zipkin server的地址,头通过配置“spring.zipkin.base-url”指定:
server:
port: 8988
spring:
zipkin:
base-url: http://localhost:9411
application:
name: eureka-server-zipkin-client
通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就可以了。
对外暴露接口:
package com.lwc.controller; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @author eddie.lee
* @Package com.lwc.controller
* @ClassName ZipkinController
* @description
* @date created in 2018-04-09 13:55
* @modified by
*/
@Slf4j
@RestController
public class ZipkinController { @Autowired
private RestTemplate restTemplate; @GetMapping("/hi")
public String callHome() {
log.info(" callHome() ==> calling trace eureka-server-zipkin-client");
return restTemplate.getForObject("http://localhost:8989/eddie", String.class);
} @GetMapping("/info2")
public String info2() {
log.info(" info2() ==> calling trace eureka-server-zipkin-client");
return "this is eureka-server-zipkin-client"; } }
4.3 创建 eureka-server-zipkin-eddie
创建过程 eureka-server-zipkin-client,引入相同的依赖,配置下spring.zipkin.base-url。
对外暴露接口:
package com.lwc.controller; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @author eddie.lee
* @Package com.lwc.controller
* @ClassName EddieController
* @description
* @date created in 2018-04-09 15:56
* @modified by
*/
@Slf4j
@RestController
public class EddieController { @Autowired
private RestTemplate restTemplate; @GetMapping("/hi")
public String home(){
log.info("==> hi is being called");
return "hi i'm eddie.lee!";
} @GetMapping("/eddie")
public String info(){
log.info("==> info is being called");
return restTemplate.getForObject("http://localhost:8988/info2",String.class);
}
}
4.4 启动工程,演示追踪
依次启动上面的三个工程,打开浏览器访问:http://localhost:9411/,会出现以下界面:
访问:http://localhost:8989/eddie,浏览器出现:
this is eureka-server-zipkin-client
再打开http://localhost:9411/的界面,点击 依赖分析 ,可以发现服务的依赖关系:
点击 查找 可以看到具体服务相互调用的数据:
PS: 在网上很多出现依赖找不到, 或者是WEB UI查看不到依赖分析等情况, 请按本文章对应代码参考, 欢迎评论;
源码下载:
标签 9-1
https://github.com/eddie-code/SpringCloudDemo
【SpringCloud】 第九篇: 服务链路追踪(Spring Cloud Sleuth)的更多相关文章
- 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)
转载请标明出处: 原文首发于:>https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f9-sleuth/ 本文出自方志朋的博客 这篇文章主 ...
- 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 注意情况: 该案例使用的spring-boot版本1.5.x,没使用2.0.x, 另外本文图3 ...
- SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- 第八篇: 服务链路追踪(Spring Cloud Sleuth)
一.简介 一个分布式系统由若干分布式服务构成,每一个请求会经过多个业务系统并留下足迹,但是这些分散的数据对于问题排查,或是流程优化都很有限. 要能做到追踪每个请求的完整链路调用,收集链路调用上每个 ...
- SpringCloud(7)服务链路追踪Spring Cloud Sleuth
1.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.本文主要讲述服务追踪组件zipki ...
- springCloud学习-服务链路追踪(Spring Cloud Sleuth)
1.简介 Spring Cloud Sleuth 是 Spring Cloud 的一个组件,它的主要功能是在分布式系统中提供服务链路追踪的解决方案. 常见的链路追踪组件有 Google 的 Dappe ...
- SpringCloud 教程 (二) 服务链路追踪(Spring Cloud Sleuth)
一.简介 Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle exampl ...
- 服务链路追踪(Spring Cloud Sleuth)
sleuth:英 [slu:θ] 美 [sluθ] n.足迹,警犬,侦探vi.做侦探 微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元.由于服务单元数量众多,业务的 ...
- spring boot 2.0.3+spring cloud (Finchley)7、服务链路追踪Spring Cloud Sleuth
参考:Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版] Spring Cloud Sleuth 是Spring Cloud的一个组件,主要功能是 ...
随机推荐
- 讲一个关于RSA加密算法的故事
有甲乙两个人,甲有两把钥匙,一把叫做甲的公钥,另一把叫做甲的私钥.乙同样有两把钥匙,一把叫做乙的公钥,另一把叫做乙的私钥. 某一天,甲乙成为了好朋友,甲想向乙发送一份保密数据,这份保密数据要求只有甲乙 ...
- 锐捷交换机RG-3760-24 的简单配置与VLAN搭建
要做的事 将交换机和主机连通. 建立vlan,并将主机配置到vlan当中. 连接主机和交换机 安装配置软件 选用SecureCRT 8.0来配置交换机,可在网上下载. 插入配置线 把配置线插入cons ...
- UIScrollView的常用属性
UIScrollView的常用属性
- Python嵌套列表去重
raw_list = [ [ 'CS_SUPP_INFO', 'A', '1'], [ 'CS_SUPP_INFO', '1', 'A'], [ 'CS_SUPP_INFO', '1', 'A'], ...
- Entity Framework中DbContext结合TransactionScope提交事务的正确方式
问: I would like know what is the best possible way to implement transactions with DBContext. In part ...
- 关于JS的clone()函数编写的一些问题
问题讲述:用js 实现一个clone()克隆函数,该函数会把输入进去的不同类型值Number,String,Undefined,Boolean,Function,Null,Object,Array,R ...
- 深入浅出爬虫之道: Python、Golang与GraphQuery的对比
深入浅出爬虫之道: Python.Golang与GraphQuery的对比 本文将分别使用 Python ,Golang 以及 GraphQuery 来解析某网站的 素材详情页面 ,这个页面的特色是具 ...
- 算法是什么(二)手写个链表(java)
算法是什么(二)手写个链表(java) liuyuhang原创,未经允许禁止转载 目录 算法是什么(〇) 很多语言的API中都提供了链表实现,或者扩展库中实现了链表. 但是更多的情况下,Map(或 ...
- PX4地面站QGroundControl在ubuntu下的安装
1.引言 相信很多玩开源无人机的朋友手上都有一架无人机,而不是仅仅停留在理论的学习和程序的学习.放飞自己组装的无人机才是乐趣所在,那么这本文就介绍玩无人机必不可少的地面站软件qgroundcontro ...
- lead over 和 lag over
今天在熟悉项目的某个功能模块时,查看mybatis的映射文件内发现这样的一串sql: (T.NET_VALUE - LEAD(T.NET_VALUE)OVER(ORDER BY T.ESTIMATE_ ...