Cloud Native 25 min read

Integrating Zipkin with Spring Cloud Sleuth for Distributed Tracing in Microservices

The article explains how to implement distributed tracing in a microservice architecture using Spring Cloud Sleuth and Zipkin, covering the concepts of spans and traces, configuration steps, code examples, deployment methods, sampling considerations, and persistence options such as MySQL and Elasticsearch.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Integrating Zipkin with Spring Cloud Sleuth for Distributed Tracing in Microservices

Microservice architecture consists of many service units, making error localization difficult; distributed tracing is required to track request flow across services.

Zipkin, based on Google’s Dapper, provides a tracing system that collects span data and visualizes service dependencies.

1. Introduction

Spring Cloud Sleuth adds tracing to Spring Boot applications and integrates with Zipkin by adding the appropriate dependencies.

2. Service tracing analysis

Requests may pass through front‑end A, middleware B/C, and back‑end services D/E; tracing records each hop.

3. Terminology

Span – basic work unit identified by a 64‑bit ID.

Trace – tree of spans representing a request.

Annotation – event marker (cs, sr, ss, cr).

4. Sleuth and Zipkin relationship

spring‑cloud‑sleuth‑zipkin (or spring‑cloud‑starter‑zipkin) provides automatic instrumentation; the starter adds @EnableZipkinServer or @EnableZipkinStreamServer to launch the server.

5. Zipkin overview

Zipkin collects spans via HTTP, Kafka or Scribe, stores them, and provides a UI for querying traces.

Zipkin architecture

Tracer → Reporter → Collector → Storage → API → UI.

Trace and Span JSON example

{
  "traceId": "bd7a977555f6b982",
  "name": "get-traces",
  "id": "ebf33e1a81dc6f71",
  "parentId": "bd7a977555f6b982",
  "timestamp": 1458702548478000,
  "duration": 354374,
  "annotations": []
}

Transport

Spans can be sent via HTTP, Kafka or Scribe.

6. Zipkin deployment

Three ways: Docker container, executable JAR, or building from source.

docker run -d -p 9411:9411 openzipkin/zipkin
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec' && java -jar zipkin.jar

7. Sample application

Configure Maven dependencies, enable @EnableZipkinServer , set server.port=9411 and spring.zipkin.base-url=http://localhost:9411 . Adjust sampling rate with spring.sleuth.sampler.percentage=1 .

8. Improving reliability

Replace HTTP transport with Spring Cloud Stream (RabbitMQ) to avoid data loss and reduce latency; use @EnableZipkinStreamServer and add spring-cloud-sleuth-zipkin-stream dependency.

9. Persistence

Zipkin can store data in MySQL (add JDBC driver, configure datasource) or Elasticsearch (add zipkin-autoconfigure-storage-elasticsearch-http and set zipkin.storage.type=elasticsearch ).

After configuring the chosen storage, start Zipkin and invoke services; the UI shows trace timelines, service dependencies and latency details.

JavamicroservicesSpring BootDistributed TracingZipkinSpring Cloud Sleuth
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.