Spring Cloud Sleuth adds trace and span IDs to the application logs, allowing developers to correlate requests across multiple services. A trace represents the entire flow of a user’s request through different microservices, while a span is a single unit of work within the trace. Each span has a unique identifier and can have parent - child relationships, indicating the flow of work.
Zipkin is a distributed tracing system. It collects and aggregates trace data sent by Spring Cloud Sleuth from different microservices. Zipkin provides a web UI where developers can visualize the traces, view the duration of each span, and analyze the flow of requests.
The design philosophy behind using Spring Cloud Sleuth and Zipkin is to decouple the monitoring functionality from the business logic of the microservices. Spring Cloud Sleuth injects tracing information into the requests and responses transparently, without requiring significant changes to the existing codebase.
Both Spring Cloud Sleuth and Zipkin follow open - standard tracing models, such as the OpenTracing and OpenTelemetry standards. This allows for interoperability with other tracing and monitoring tools in the ecosystem.
In your pom.xml
(if using Maven), add the following dependencies:
<dependencies>
<!-- Spring Cloud Sleuth -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!-- Zipkin Sender -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
In your application.properties
or application.yml
, add the following configuration:
spring.zipkin.base-url=http://localhost:9411
Since tracing can generate a large amount of data, especially in high - traffic applications, sampling is crucial. Spring Cloud Sleuth supports different sampling strategies, such as probabilistic sampling. You can configure the sampling rate in your application:
spring.sleuth.sampler.probability=0.1
This configuration means that only 10% of the requests will be traced.
Sending trace data to the Zipkin server adds network overhead. To reduce this, you can consider using a local buffer or a more efficient transport protocol.
Use the trace and span IDs provided by Spring Cloud Sleuth in your application logs. This allows you to correlate log entries with specific requests.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
private static final Logger logger = LoggerFactory.getLogger(SampleController.class);
@Autowired
private Tracer tracer;
@GetMapping("/hello")
public String hello() {
String traceId = tracer.currentSpan().context().traceId();
String spanId = tracer.currentSpan().context().spanId();
logger.info("Received request. Trace ID: {}, Span ID: {}", traceId, spanId);
return "Hello, World!";
}
}
When making external calls (e.g., HTTP requests to other microservices), make sure to propagate the trace and span IDs. Spring Cloud Sleuth does this automatically for many common libraries, such as RestTemplate
and WebClient
.
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
@RestController
public class TracingExampleApplication {
public static void main(String[] args) {
SpringApplication.run(TracingExampleApplication.class, args);
}
@GetMapping("/test")
public String test() {
// This method will be automatically traced by Spring Cloud Sleuth
return "Test response";
}
}
Use a centralized configuration management system (e.g., Spring Cloud Config) to manage the tracing and sampling configuration across all microservices.
Handle errors gracefully in your microservices and log the trace and span IDs along with error messages. This helps in debugging issues.
An e - commerce application consists of multiple microservices, such as product catalog, shopping cart, and payment gateway. By using Spring Cloud Sleuth and Zipkin, the development team was able to identify a performance bottleneck in the payment gateway microservice. The tracing data showed that a particular database query was taking a long time, and the team was able to optimize it.
A social media platform used Spring Cloud Sleuth and Zipkin to monitor the flow of requests between the user profile, news feed, and notification microservices. This helped in reducing the response time of the application by identifying and optimizing the slowest parts of the system.
Spring Cloud Sleuth and Zipkin are powerful tools for monitoring microservices in Java applications. By understanding the core principles, design philosophies, and following best practices, developers can effectively trace requests across multiple services, identify performance bottlenecks, and debug issues. However, it is important to consider performance implications and avoid common pitfalls when using these tools.