使用Spring Boot集成Sleuth

大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!

在分布式系统中,跟踪请求的流转路径是非常重要的。Spring Cloud Sleuth是一个分布式追踪解决方案,它可以帮助我们实现这种请求链路追踪。本文将详细介绍如何在Spring Boot项目中集成Sleuth,并通过代码示例展示其具体用法。

一、引入依赖

首先,在你的Spring Boot项目中引入Sleuth的依赖。在pom.xml文件中添加以下依赖:

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

这些依赖将Sleuth和Zipkin集成到你的项目中,Zipkin是一个分布式追踪系统,用于收集和查看跟踪数据。

二、配置Sleuth

application.propertiesapplication.yml中配置Sleuth。以下是一个基本的配置示例:

spring.application.name=juwatech-sleuth-demo
spring.sleuth.sampler.probability=1.0
spring.zipkin.base-url=http://localhost:9411

这些配置指定了应用名称、采样率和Zipkin服务器地址。

三、创建示例应用

下面,我们将创建一个简单的Spring Boot应用,包含两个服务,通过REST API进行调用,并使用Sleuth进行追踪。

服务A:

package cn.juwatech.sleuth.serviceA;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class ServiceAApplication {

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

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@RestController
class ServiceAController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/serviceA")
    public String callServiceB() {
        String response = restTemplate.getForObject("http://localhost:8081/serviceB", String.class);
        return "Response from Service B: " + response;
    }
}

服务B:

package cn.juwatech.sleuth.serviceB;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class ServiceBApplication {

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

@RestController
class ServiceBController {

    @GetMapping("/serviceB")
    public String serviceB() {
        return "Hello from Service B!";
    }
}

四、运行应用

  1. 分别运行服务A和服务B。
  2. 访问http://localhost:8080/serviceA,你将看到服务A调用了服务B,并返回了服务B的响应。

五、查看跟踪数据

Sleuth会自动将跟踪数据发送到Zipkin。确保Zipkin服务器正在运行,并访问http://localhost:9411查看跟踪数据。

六、自定义跟踪信息

Sleuth允许我们自定义跟踪信息,例如添加自定义标签。以下是如何在服务A中添加自定义标签的示例:

package cn.juwatech.sleuth.serviceA;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
class ServiceAController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Tracer tracer;

    @GetMapping("/serviceA")
    public String callServiceB() {
        Span newSpan = tracer.nextSpan().name("custom-span");
        try (Tracer.SpanInScope ws = tracer.withSpan(newSpan.start())) {
            newSpan.tag("custom-tag", "custom-value");
            String response = restTemplate.getForObject("http://localhost:8081/serviceB", String.class);
            return "Response from Service B: " + response;
        } finally {
            newSpan.end();
        }
    }
}

在这个示例中,我们创建了一个新的Span,并添加了一个自定义标签。然后,我们将这个Span附加到当前的跟踪上下文中。

七、总结

通过Spring Boot集成Sleuth,我们可以轻松实现分布式追踪,跟踪请求在多个服务中的流转路径,并使用Zipkin来收集和查看这些跟踪数据。Sleuth的强大功能和简单易用的API使得它成为分布式系统中不可或缺的工具。