Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring Cloud Sleuth with Spring Boot Tutorial

Introduction

Spring Cloud Sleuth is a distributed tracing solution for Spring applications. It integrates seamlessly with Spring Boot to provide traceability across microservices. By automatically adding trace and span IDs to logs, it helps in tracking the flow of requests in a service-oriented architecture.

Prerequisites

Before we begin, ensure you have the following:

  • Java Development Kit (JDK) 8 or higher installed
  • Maven installed on your machine
  • An IDE such as IntelliJ IDEA or Eclipse
  • Basic knowledge of Spring Boot and Maven

Setting Up the Project

We'll create a simple Spring Boot application and then add Spring Cloud Sleuth to it.

1. Create a New Spring Boot Project

You can create a new Spring Boot project using Spring Initializr (https://start.spring.io/). Select the following dependencies:

  • Spring Web
  • Spring Cloud Sleuth

2. Add Dependencies

Add the following Maven dependencies to your pom.xml file:

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

Also, make sure to add the Spring Cloud dependency management in your pom.xml:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2020.0.3</version>  <!-- Check for the latest version -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
                

Creating a Simple REST Controller

Let's create a simple REST controller that will handle incoming requests.

1. Create a Controller Class

Create a new class named GreetingController in your project:

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

@RestController
public class GreetingController {

    @GetMapping("/greet")
    public String greet() {
        return "Hello from Spring Cloud Sleuth!";
    }
}
                

Running the Application

You can run your Spring Boot application using Maven. Use the following command:

mvn spring-boot:run

Once the application is running, access the endpoint http://localhost:8080/greet from your browser or Postman. You should see the message: Hello from Spring Cloud Sleuth!

Logging with Sleuth

Spring Cloud Sleuth automatically adds trace and span IDs to your logs. To see this in action, you can add a simple log statement inside the greet method:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
public class GreetingController {
    private static final Logger logger = LoggerFactory.getLogger(GreetingController.class);

    @GetMapping("/greet")
    public String greet() {
        logger.info("Greeting endpoint was called");
        return "Hello from Spring Cloud Sleuth!";
    }
}
                

When you call the endpoint again, you'll see logs showing the trace and span IDs:

2023-01-01 12:00:00 INFO GreetingController: Greeting endpoint was called

TraceId: 1234567890abcdef SpanId: 1234567890abcdef

Conclusion

In this tutorial, we covered the basics of integrating Spring Cloud Sleuth with a Spring Boot application. You learned how to set up a project, create a REST controller, and observe the logging of trace and span IDs. This powerful tool helps you trace requests across microservices and is essential for debugging and monitoring in microservices architectures.

For more advanced usage, consider exploring additional features of Sleuth, such as integration with Zipkin or using custom sampling strategies.