3

Spring Cloud Sleuth 介绍

 1 year ago
source link: https://blog.51cto.com/u_15326439/5906977
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Spring Cloud Sleuth 介绍_应用程序

如果您正在开始使用Spring Cloud Sleuth或Spring,请首先阅读本节。 它回答了基本的“什么?”,“如何?”和“为什么?”的问题。 它包括对Spring Cloud Sleuth的介绍,以及安装说明。 然后,我们将引导您构建第一个Spring Cloud Sleuth应用程序,并在进行过程中讨论一些核心原则。

1. 春云侦探简介

Spring Cloud Sleuth为Spring Cloud提供分布式跟踪解决方案的API。 它与OpenZipkin Brave集成

Spring Cloud Sdetectath能够跟踪您的请求和消息,以便您可以将该通信与相应的日志条目相关联。 您还可以将跟踪信息导出到外部系统以可视化延迟。 Spring Cloud Sleuth直接支持OpenZipkin兼容系统。

1.1. 术语

《春云侦探》借用了达佩尔的术语。

跨度:基本工作单元。 例如,发送 RPC 是一个新的跨度,向 RPC 发送响应也是如此。 跨度还具有其他数据,例如描述、带时间戳的事件、键值注释(标签)、导致它们的跨度的 ID 和进程 ID(通常是 IP 地址)。

跨度可以启动和停止,并且它们会跟踪其计时信息。 创建范围后,必须在将来的某个时间点停止它。

跟踪:一组跨度形成树状结构。 例如,如果运行分布式大数据存储,则可能会通过请求形成跟踪。​​PUT​

注释/事件:用于及时记录事件的存在。

从概念上讲,在典型的 RPC 方案中,我们标记这些事件以突出显示发生的操作类型(这并不意味着物理上此类事件将在跨度上设置)。

  • cs:客户端已发送。 客户端已提出请求。 此注释指示范围的开始。
  • sr:服务器已收到:服务器端收到请求并开始处理它。 从此时间戳中减去时间戳可显示网络延迟。cs
  • ss:服务器已发送。 在请求处理完成后(当响应发送回客户端时)进行注释。 从此时间戳中减去时间戳可显示服务器端处理请求所需的时间。sr
  • CR:客户已收到。 表示范围的结束。 客户端已成功收到来自服务器端的响应。 从此时间戳中减去时间戳将显示客户端从服务器接收响应所需的全部时间。cs

下图显示了“跨度”和“跟踪”在系统中的外观。

Spring Cloud Sleuth 介绍_应用程序_02

音符的每种颜色都表示一个跨度(有七个跨度 - 从AG)。 请考虑以下注意事项:

Trace Id = X
Span Id = D
Client Sent

此注释指示当前跨度的跟踪ID 设置为X范围 ID设置为D。 此外,从RPC的角度来看,该事件发生了。​​Client Sent​

让我们考虑更多注意事项:

Trace Id = X
Span Id = A
(no custom span)

Trace Id = X
Span Id = C
(custom span)

您可以继续创建的范围(带指示的示例),也可以手动创建子范围(带指示的示例)。​​no custom span​​​​custom span​

下图显示了跨度的父子关系的外观:

Spring Cloud Sleuth 介绍_Cloud_03

2. 开发您的第一个基于 Spring Cloud 侦探的应用程序

本节介绍如何开发一个小型的“Hello World!”Web应用程序,该应用程序突出了Spring Cloud Sleuth的一些主要功能。 我们使用 Maven 来构建这个项目,因为大多数 IDE 都支持它。 作为示踪剂实现,我们将使用OpenZipkin Brave。

您可以通过转到start.spring.io​并从依赖项搜索器中选择“Web”和“Spring Cloud Sleuth”启动器来缩短以下步骤。 这样做会生成一个新的项目结构,以便您可以立即开始编码。

2.1. 创建 POM

我们需要从创建一个Mavenfile开始。 这是用于构建项目的配方。 打开您喜欢的文本编辑器并添加以下内容:​​pom.xml​​​​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>

<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- Use the latest compatible Spring Boot version. You can check https://spring.io/projects/spring-cloud for more information -->
<version>$2.6.13</version>
</parent>

<!-- Spring Cloud Sleuth requires a Spring Cloud BOM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!-- Provide the latest stable Spring Cloud release train version (e.g. 2020.0.0) -->
<version>${release.train.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<!-- (you don't need this if you are using a GA version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>

前面的列表应为您提供一个工作版本。 您可以通过运行来测试它(现在,您可以忽略“jar 将为空 - 没有内容被标记为包含!”警告)。​​mvn package​

此时,您可以将项目导入到 IDE 中(大多数现代 Java IDE 都包含对 Maven 的内置支持)。 为简单起见,我们继续在本例中使用纯文本编辑器。

2.2. 添加类路径依赖

要添加必要的依赖项,请编辑您的依赖项并添加紧邻该部分下方的依赖项:​​pom.xml​​​​spring-boot-starter-web​​​​parent​

<dependencies>
<!-- Boot's Web support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Sleuth with Brave tracer implementation -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
</dependencies>

2.3. 编写代码

为了完成我们的应用程序,我们需要创建一个 Java 文件。 默认情况下,Maven 编译源,因此您需要创建该目录结构,然后添加一个名为 to 包含以下代码的文件:​​src/main/java​​​​src/main/java/Example.java​

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

private static final Logger log = LoggerFactory.getLogger(Example.class);

@RequestMapping("/")
String home() {
log.info("Hello world!");
return "Hello World!";
}

public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}

}

虽然这里没有太多代码,但很多事情正在发生。 我们将在接下来的几节中逐步介绍重要部分。

@RestController和@RequestMapping注释

Spring Boot 设置了 Rest 控制器,并使我们的应用程序绑定到 Tomcat 端口。 带有 Brave 跟踪器的 Spring Cloud Sleuth 将提供传入请求的检测。

2.4. 运行示例

此时,应用程序应该可以工作。 由于您使用了 POM,因此您有一个可用于启动应用程序的有用目标。 从根项目目录中键入以启动应用程序。 应会看到类似于以下内容的输出:​​spring-boot-starter-parent​​​​run​​​​SPRING_APPLICATION_NAME=backend mvn spring-boot:run​

$ mvn spring-boot:run

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
...
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)

如果打开 Web 浏览器,应会看到以下输出:​​localhost:8080​

Hello World!

如果您检查日志,您应该会看到类似的输出

2020-10-21 12:01:16.285 INFO [backend,0b6aaf642574edd3,0b6aaf642574edd3] 289589 --- [nio-9000-exec-1] Example : Hello world!

您可以注意到日志记录格式已使用以下信息进行了更新。 此条目对应于。 从环境变量中读取了应用程序名称。​​[backend,0b6aaf642574edd3,0b6aaf642574edd3​​​​[application name,trace id, span id]​​​​SPRING_APPLICATION_NAME​

您可以设置,而不是在处理程序中显式记录请求。​​logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG​

要正常退出应用程序,请按。​​ctrl-c​

3. 后续步骤

希望本节提供了一些Spring Cloud Sleuth基础知识,并帮助您编写自己的应用程序。 如果你是一个面向任务的开发人员,你可能想跳到spring.io并查看一些入门指南,这些指南解决了特定的“我如何使用Spring做到这一点?”问题。 我们还有特定于Spring Cloud Sleuth的“操作方法”参考文档。

否则,下一个合乎逻辑的步骤是阅读使用春云侦探。 如果你真的不耐烦,你也可以跳到前面阅读春云侦探功能。

可以在示例中找到默认项目示例。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK