🔍 登录 注册

Spring Cloud Sleuth

大 大苹果 2024-07-30 algorithm 👁 25 ⏱ 8min

Spring Cloud Sleuth 是 Spring Cloud 项目中的一个分布式追踪解决方案,旨在帮助开发者对微服务架构中的调用链路进行追踪和分析。它可以自动地为请求分配唯一的标识符,并在不同的服务之间传递这些标识符,以便在调用链中记录请求的详细信息。Sleuth 可以与多种分布式追踪系统集成,如 Zipkin 和 Jaeger,用于可视化和分析请求的流向。

Spring Cloud Sleuth 的核心概念

  1. Trace(追踪):一次请求在分布式系统中的调用链路。每个 Trace 都有一个唯一的 Trace ID。
  2. Span(跨度):一次请求在某个服务中的处理过程。一个 Trace 可以包含多个 Span。每个 Span 都有一个唯一的 Span ID。
  3. Propagation(传播):在服务之间传递 Trace ID 和 Span ID。
  4. Baggage(行李):用于在调用链中传递的自定义数据。

使用 Spring Cloud Sleuth 进行分布式追踪

我们将创建一个简单的 Spring Boot 微服务应用,包括两个服务:service-a 和 service-b,并使用 Spring Cloud Sleuth 进行追踪。

项目结构

  • service-a:调用 service-b 并记录调用链信息。
  • service-b:提供一个简单的 REST API,被 service-a 调用。

1. 创建 service-a

添加依赖

在 pom.xml 中添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

创建应用主类

package com.example.servicea;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class ServiceAApplication {

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

创建 Feign 客户端

package com.example.servicea;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "service-b", url = "http://localhost:8081")
public interface ServiceBClient {

    @GetMapping("/api/service-b")
    String callServiceB();
}

创建控制器

package com.example.servicea;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceAController {

    @Autowired
    private ServiceBClient serviceBClient;

    @GetMapping("/api/service-a")
    public String callServiceB() {
        return "Response from Service B: " + serviceBClient.callServiceB();
    }
}

配置文件

在 src/main/resources/application.yml 中添加以下配置:

server:
  port: 8080

spring:
  application:
    name: service-a

2. 创建 service-b

添加依赖

在 pom.xml 中添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

创建应用主类

package com.example.serviceb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServiceBApplication {

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

创建控制器

package com.example.serviceb;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceBController {

    @GetMapping("/api/service-b")
    public String serviceBEndpoint() {
        return "Hello from Service B!";
    }
}

配置文件

在 src/main/resources/application.yml 中添加以下配置:

server:
  port: 8081

spring:
  application:
    name: service-b

3. 启动项目

分别启动 service-a 和 service-b 项目:

  1. 启动 ServiceAApplication。
  2. 启动 ServiceBApplication。

4. 测试追踪功能

使用 Curl 或 Postman 测试服务调用:

curl http://localhost:8080/api/service-a

该请求将通过 service-a 调用 service-b,并在控制台输出追踪信息,类似于:

service-a 日志输出:

2024-07-30 12:00:00.000  INFO [service-a,traceId=1234567890abcdef,spanId=abcdef1234567890] 1 --- [nio-8080-exec-1] c.example.servicea.ServiceAController    : Calling Service B
2024-07-30 12:00:00.100  INFO [service-a,traceId=1234567890abcdef,spanId=abcdef1234567890] 1 --- [nio-8080-exec-1] c.example.servicea.ServiceAController    : Response from Service B: Hello from Service B!

service-b 日志输出:

2024-07-30 12:00:00.050  INFO [service-b,traceId=1234567890abcdef,spanId=1234567890abcdef] 1 --- [nio-8081-exec-1] c.example.serviceb.ServiceBController    : Received request for Service B

在日志中,我们可以看到 traceId 和 spanId 用于标识请求的追踪信息。

5. 集成 Zipkin 进行可视化追踪

我们可以将 Spring Cloud Sleuth 与 Zipkin 集成,以便可视化追踪信息。

添加 Zipkin 依赖

在 service-a 和 service-b 的 pom.xml 中添加以下依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

配置 Zipkin

在 src/main/resources/application.yml 中添加以下配置:

spring:
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      probability: 1.0
  • base-url:配置 Zipkin 服务的 URL。
  • sampler.probability:设置采样率为 100%,即所有请求都会被追踪。

启动 Zipkin

可以通过 Docker 启动一个 Zipkin 实例:

docker run -d -p 9411:9411 openzipkin/zipkin

访问 Zipkin UI

启动所有服务后,访问 http://localhost:9411 查看追踪信息。在 Zipkin UI 中,可以查看请求的调用链、请求耗时等详细信息。

小结

Spring Cloud Sleuth 为微服务架构提供了强大的分布式追踪功能,通过与 Zipkin 等工具的集成,可以实现对请求调用链的可视化和分析。在实际应用中,可以根据需求调整 Sleuth 的配置,以满足不同的追踪需求。

如果您有任何问题或需要进一步的帮助,请随时告诉我!

读完了?给作者一点支持

本文采用 CC BY 4.0 协议转载 · © 大苹果

评论(0)

登录 后参与评论。

  • 还没有评论,来抢沙发。

🔗 相关文章

动态规划从入门到进阶:状态设计与空间优化的完整方法论

从暴力递归到记忆化与递推,覆盖背包、区间、状态压缩等模型,给出动态规划状态设计与空间优化的完整方法论。

21 天前👍 0

Go 排序算法实战:从冒泡到 TimSort 的复杂度与工程取舍

用 Go 实现九种经典排序,剖析复杂度、稳定性与适用场景,并拆解标准库 sort 背后的 introsort 工程取舍。

21 天前👍 0

RabbitMQ 标准队列

RabbitMQ 标准队列(Classic Queue)是消息传递系统中最基本的队列类型,消息按先进先出(FIFO)的顺序存储和传递。以下是标准队列的常见应用场景和详细说明: ### 1. **任务队列(Task Queue)** - **场景描述**:在后台处理任务时,如图像处理、视频转码、数据分析等,需要将任务分配给多个工作节点进行处理,避免阻塞主应用。 - **应用说明**:在任务队列中,每个任务都会被推送到 RabbitMQ 的标准队列中,多个消费者从队列中获取任务并处理,任务处理完毕后删除队列中的消息。 - **举例**: - **应用**:电商网站后台处理订单数据,如生成发货单、发送确认邮件等。这些任务可以异步处理,以提高系统的响应速度。 - **代码示例(Node.js)**: ```javascript // 生产者:将任务添加到队列 const amqp = require('amqplib/callback_api'); amqp.connect('amqp://localhost', func

2024/8/11👍 0