Distributed Tracing with Spring Cloud Sleuth and Zipkin

In microservices, requests often span across multiple services. When something goes wrong or performance degrades, it becomes challenging to trace the root cause. This is where distributed tracing shines.

This tutorial explains how to set up Spring Cloud Sleuth with Zipkin for full visibility into your service calls.

๐Ÿ“– Table of Contents

  1. What is Distributed Tracing?
  2. What is Spring Cloud Sleuth?
  3. What is Zipkin?
  4. Setup Project with Sleuth and Zipkin
  5. Configure Zipkin Server
  6. Sample Microservices Setup
  7. Trace IDs and Logs
  8. Visualize Traces in Zipkin
  9. Conclusion
Distributed Tracing with Spring Cloud Sleuth and Zipkin

๐Ÿ” 1. What is Distributed Tracing?

Distributed Tracing helps you track a request as it flows through multiple services. Each trace consists of:

  • Trace ID: Shared across the entire request.
  • Span ID: Represents a single operation within the trace.

It helps:

  • Identify latency bottlenecks
  • Debug failures across services
  • Provide end-to-end request visibility

๐ŸŒฉ 2. What is Spring Cloud Sleuth?

Spring Cloud Sleuth automatically instruments your Spring Boot applications with:

  • Unique traceId for each request
  • Span IDs for each method call
  • Integration with Zipkin for trace collection

๐Ÿ“ฆ 3. What is Zipkin?

Zipkin is a distributed tracing system that collects and visualizes traces.

  • Collects spans and trace metadata
  • Provides a UI to search and view traces
  • Supports multiple storage backends (in-memory, MySQL, Elasticsearch, etc.)

โš™๏ธ 4. Setup Spring Boot with Sleuth and Zipkin

โœ… Dependencies (pom.xml)

Also, include Spring Boot Starter Web and Spring Cloud BOM:

๐Ÿ›  5. Configure Zipkin

โœ… application.yml

๐Ÿงฑ 6. Sample Service Setup

โœ… ProductService.java

โœ… ProductController.java

๐Ÿ“ฅ 7. Run Zipkin Server Locally

You can run Zipkin using Docker:

Then access the Zipkin UI at:
๐Ÿ‘‰ http://localhost:9411

๐Ÿ“„ 8. Trace IDs and Logs

Spring Sleuth adds these to each log line:

Where:

  • First is the service name
  • Second is traceId
  • Third is spanId
  • Fourth indicates whether it’s sampled

You can trace a request through:

  • Logs
  • Zipkin UI

๐Ÿงช 9. Test Tracing Across Services

If you have two services (e.g., ProductService and InventoryService), calls between them will share the same traceId automatically when using RestTemplate, WebClient, or FeignClient.

Spring Cloud Sleuth auto-instruments the HTTP clients and passes the trace headers.

Example using RestTemplate:

๐Ÿ“Š 10. View in Zipkin

Visit http://localhost:9411 and click “Find Traces”.

Youโ€™ll see the entire call path from:

  • API Gateway โ†’ Product Service โ†’ Inventory Service

Each span shows:

  • Duration
  • Endpoint
  • Service Name
  • Timestamp

๐Ÿ“Œ Key Takeaways

  • Spring Cloud Sleuth makes it easy to add tracing to Spring Boot.
  • Zipkin helps visualize and analyze these traces.
  • Trace IDs flow automatically across services.
  • Logs and trace data are correlated for easier debugging.

โœ… Conclusion

Spring Cloud Sleuth and Zipkin provide a powerful combination for distributed tracing in microservices. By visualizing the journey of a request, you can reduce MTTR, detect slow calls, and improve system observability.

Start tracing today to build more resilient and observable systems!