一、spring cloud sleuth
1.Spring-Cloud-Sleuth:是Spring Cloud的组成部分之一,为SpringCloud应用实现了一种分布式追踪解决方案,其兼容了Zipkin, HTrace和log-based追踪
2.Span:基本工作单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span通过一个64位ID唯一标识,trace以另一个64位ID表示,span还有其他数据信息,比如摘要、时间戳事件、关键值注释(tags)、span的ID、以及进度ID(通常是IP地址)
span在不断的启动和停止,同时记录了时间信息,当你创建了一个span,你必须在未来的某个时刻停止它。
3.Trace:一系列spans组成的一个树状结构,例如,如果你正在跑一个分布式
大数据工程,你可能需要创建一个trace。
4.Annotation:用来及时记录一个事件的存在,一些核心annotations用来定义一个请求的开始和结束
1)cs(Client Sent) :客户端发起一个请求,这个annotion描述了这个span的开始
2)sr(Server Received): 服务端获得请求并准备开始处理它,如果将其sr减去cs时间戳便可得到网络延迟
3)ss(Server Sent):注解表明请求处理的完成(当请求返回客户端),如果ss减去sr时间戳便可得到服务端需要的处理请求时间
4)cr(Client Received):表明span的结束,客户端成功接收到服务端的回复,如果cr减去cs时间戳便可得到客户端从服务端获取回复的所有所需时间
二、实例:http通信方式、Mq异步通信+Mysql方式、Mq异步通信+
elasticsearch方式
建三个项目:server-monitor监控(负责数据收集以及信息展示功能)、consumer-beehive服务消费者、provider-user服务提供者
1.http方式:将数据保存到内存中,但是生产环境还是需要将数据持久化中
1)server-monitor:
a.pom.xml依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
b.**Application.java:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import zipkin.server.EnableZipkinServer; @SpringBootApplication @EnableZipkinServer //将http改为mq时,用EnableZipkinStreamServer代替 public class ServerMonitorApplication { public static void main(String[] args){ SpringApplication.run(ServerMonitorApplication.class,args); } }
c.application.yml: