springCloud学习-服务链路追踪(Spring Cloud Sleuth)
1、简介
Spring Cloud Sleuth 是 Spring Cloud 的一个组件,它的主要功能是在分布式系统中提供服务链路追踪的解决方案。 常见的链路追踪组件有 Google 的 Dapper、 Twitter 的 Zipkin , 以及阿里的 Eagleeye(鹰眼)等,它们都是非常优秀的链路追踪开源组件。
这里主要讲述如何在 Spring Cloud Sleuth 中集成 Zipkin。
2、基本术语
- 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注解的过程图形化:
3、构建server-zipkin
在spring Cloud为F版本的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可,下载地址:
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
下载完成jar 包之后,可以将jar放入工程中,然后右键运行,也可以到jar包目录下,运行如下命令:
java -jar jar包名称
我这里是:
java -jar zipkin-server-2.9.4-exec.jar
运行成功之后,访问 http://localhost:9411,出现如下页面表示启动成功
4、构建server-hi工程
4.1、在pom.xml中引入zipkin的起步依赖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.study</groupId>
<artifactId>service-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>service-hi</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.lishun</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</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>
</dependencies>
</project>
4.2、在配置文件中加入如下配置:
server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi
spring.sleuth.sampler.probability=1.0
spring.sleuth.sampler.probability=1.0表示以 100% 的概率将链路的数据上传给 Zipkin Server , 在默认情况下 , 该值为0.1
4.3、在ServiceHiApplication中加入API接口,代码如下:
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; @RestController
@SpringBootApplication
public class ServiceHiApplication { public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
} private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName()); @Autowired
private RestTemplate restTemplate; @Bean
public 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"; } @Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}
5、构建service-miya工程
service-miya的pom.xml文件和配置文件如下:
5.1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId>
<artifactId>service-miya</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>service-miya</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.lishun</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</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>
</dependencies> </project>
5.2、application.properties代码如下:
server.port=8989
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-miya
spring.sleuth.sampler.probability=1.0
5.3、在ServiceMiyaApplication中加入API接口,代码如下:
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;
@RestController
@SpringBootApplication
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);
} @Autowired
private RestTemplate restTemplate; @Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
} @Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}
6、链路追踪实践
6.1、依次启动jar包,hi和miya工程,打开浏览器访问:http://localhost:9411/,会出现以下界面:
6.2、访问:http://localhost:8989/miya,浏览器出现:
i'm service-hi
6.3、再打开http://localhost:9411/的界面,此时就可以看到服务之间的调用关系,和服务之间的调用数据
springCloud学习-服务链路追踪(Spring Cloud Sleuth)的更多相关文章
- SpringCloud(7)服务链路追踪Spring Cloud Sleuth
1.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.本文主要讲述服务追踪组件zipki ...
- 史上最简单的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)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- 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 Cloud Sleuth)
一.简介 一个分布式系统由若干分布式服务构成,每一个请求会经过多个业务系统并留下足迹,但是这些分散的数据对于问题排查,或是流程优化都很有限. 要能做到追踪每个请求的完整链路调用,收集链路调用上每个 ...
- spring boot 2.0.3+spring cloud (Finchley)7、服务链路追踪Spring Cloud Sleuth
参考:Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版] Spring Cloud Sleuth 是Spring Cloud的一个组件,主要功能是 ...
随机推荐
- PL/SQL编程基础
1. PL/SQL块的基础结构 DECLARE /* * 定义部分——定义常量.变量.复杂数据类型.游标.用户自定义异常 */ BEGIN /* * 执行部分——PL/SQL语句和SQL语句 */ E ...
- 40. combo的displayField和valueField属性
转自:https://xsl2007.iteye.com/blog/773464 下拉框combo可以设置displayField和valueField属性,这两个值值相当于Java中的map,一个键 ...
- js和php中几种生成验证码的方式
之前做过取随机数和生成验证码的练习,都是通过取随机数作为数组下标,然后从数组中取值的方式(js): /*验证码*/ function sj_yzm(){ //存一个包括数字和字母的数组 var zon ...
- [Swift通天遁地]二、表格表单-(13)实时调整表单元素的显示和隐藏
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- selenium3 + python - action_chains源码分析
ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互.这对于做更复杂的动作非常有用,比如悬停和 ...
- GoLang 编译exe添加ICO图标
我们在做Go开发的时候在Window平台下编译出来的exe后大部分都是没有图标,看起来很难看.下面我们说下如何添加一个图标. 1.首先在根目录下,exe的同级目录下创建.rc文件, IDI_ICON1 ...
- Bitmap与String之间的转换
/** * 将bitmap转换成base64字符串 * * @param bitmap * @return base64 字符串 */ public String bitmaptoString(Bit ...
- Linux-fork()函数详解,附代码注释
// // main.c // Project_C // // Created by LiJinxu on 16/8/13. // Copyright © 2016年 LiJinxu-NEU. All ...
- 关于类似vue-cli 脚手架
#!/usr/bin/env node const download = require('download-git-repo') const program = require('commander ...
- 3星|《商业周刊中文版:2017商业人物(下)》:酒店才应该是出行住宿的最佳选择,Airbnb不是
商业周刊/中文版:2017商业人物(下) 对一些知名商业人物的访谈的合辑. 总体评价3星,有一些参考价值. 以下是本期一些内容的摘抄: 1:段永平是一位隐秘的亿万富豪,去年,他创立的智能手机姊妹品牌O ...