以下案例是基于《从0开始搭建 SpringCloudAlibaba项目》继续搭建的。
1、简介
Spring Cloud Sleuth为Spring Cloud实现了分布式跟踪解决方案。
2、术语
Spring Cloud Sleuth借鉴了Dapper的术语。
跨度:基本工作单位。例如,发送RPC是一个新的跨度,就像发送响应到RPC一样。跨度由跨度的唯一64位ID和跨度所属的跟踪的另一个64位ID标识。跨区还具有其他数据,例如描述,带有时间戳的事件,键值批注(标签),引起跨度的跨区ID和进程ID(通常为IP地址)。
跨度可以启动和停止,并且可以跟踪其时序信息。创建跨度后,您必须在将来的某个时间点将其停止。
迹线:一组形成树状结构的跨度。例如,如果您运行分布式大数据存储,则跟踪可能是由PUT
请求形成的。
注释:用于及时记录事件的存在。使用 Brave工具,我们不再需要为Zipkin设置特殊事件来了解客户端和服务器是谁,请求在哪里开始以及在哪里结束。但是,出于学习目的,我们标记这些事件以突出显示发生了哪种操作。
-
cs:客户端已发送。客户提出了要求。此注释指示跨度的开始。
-
sr:接收到服务器:服务器端收到请求并开始处理它。
cs
从该时间戳中减去该时间戳可揭示网络延迟。 -
ss:服务器已发送。在请求处理完成时进行注释(当响应被发送回客户端时)。
sr
从该时间戳中减去该时间戳将显示服务器端处理该请求所需的时间。 -
cr:收到客户。表示跨度结束。客户端已成功收到服务器端的响应。
cs
从该时间戳中减去该时间戳将显示客户端从服务器接收响应所需的整个时间。
3、 搭建案例
3.1 创建Zipkin-Server
在spring Cloud为F版本的时候,已经不需要自己构建Zipkin-Server了,只需要下载jar即可,下载地址:https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
本文用的是最新的jar,版本为 zipkin-server-2.12.9-exec.jar ,cmd 直接运行 java -jar zipkin-server-2.12.9-exec.jar
启动完成后访问地址:http://127.0.0.1:9411/
3.2 创建Sleuth-Service-A
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">
<parent>
<artifactId>cloudalibaba</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-sleuth-service-a</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
</project>
application.yml 配置文件
server:
port: 8201
spring:
application:
name: sleuth-a-server #服务名称
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos的服务注册中心地址
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1.0 #spring.sleuth.sampler.probability=1.0表示以 100% 的概率将链路的数据上传给 Zipkin Server , 在默认情况下 , 该值为0.1
3.3 创建Sleuth-Service-B
pom.xml 依赖如下,和A服务的一样
<?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">
<parent>
<artifactId>cloudalibaba</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-sleuth-service-b</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
application.yml 配置文件
server:
port: 8202
spring:
application:
name: sleuth-b-server #服务名称
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos的服务注册中心地址
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1.0 #spring.sleuth.sampler.probability=1.0表示以 100% 的概率将链路的数据上传给 Zipkin Server , 在默认情况下 , 该值为0.1
TestController.java
package com.example.cloudalibabasleuth.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zqing
* @description: TODO
* @date: 2020/11/18 14:01
*/
@RestController
public class TestController {
@RequestMapping("/getServiceB")
public String getServiceB(){
return "hello 调用B服务";
}
}
3.4 在Sleuth-Service-A上调用Sleuth-Service-B
Sleuth-Service-A上创建ServiceAFeign接口和FeignServiceFallBack
package com.example.cloudalibabasleuth.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author zqing
* @description: TODO
* @date: 2020/11/18 14:08
*/
@FeignClient(name = "sleuth-b-server",fallbackFactory = FeignServiceFallBack.class)
public interface ServiceAFeign {
@RequestMapping("/getServiceB")
String getServiceB();
}
package com.example.cloudalibabasleuth.feign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author zqing
* @description: 降级处理
* @date: 2020/11/18 14:10
*/
@Component
@Slf4j
public class FeignServiceFallBack implements FallbackFactory<ServiceAFeign> {
@Override
public ServiceAFeign create(Throwable throwable) {
return new ServiceAFeign() {
@Override
public String getServiceB() {
return "服务被降级";
}
};
}
}
Sleuth-Service-A上创建TestController
package com.example.cloudalibabasleuth.controller;
import com.example.cloudalibabasleuth.feign.ServiceAFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zqing
* @description: TODO
* @date: 2020/11/18 14:01
*/
@RestController
public class TestController {
@Autowired
private ServiceAFeign serviceAFeign;
@RequestMapping("/indexA")
public String getInfo(){
return "hello service-a";
}
@RequestMapping("/indexB")
public String getServiceB(){
return serviceAFeign.getServiceB();
}
}
4、运行Sleuth-Service-A和Sleuth-Service-B
访问地址:http://localhost:8201/indexA
访问地址:http://localhost:8201/indexB
5、访问zipkin-server服务(刚开始是没有任何服务的)
服务链路追踪到此完成。
源码已上传gitee:源码下载