Backend Development 14 min read

Integrating Spring Cloud Sleuth and Zipkin for Distributed Tracing in Microservices

This tutorial explains the principles of distributed tracing, why it is needed for microservice architectures, and provides step‑by‑step instructions for adding Spring Cloud Sleuth and Zipkin—including Maven dependencies, configuration, Docker deployment, and Elasticsearch persistence—to a Spring Cloud project.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Integrating Spring Cloud Sleuth and Zipkin for Distributed Tracing in Microservices

Introduction

The author writes while caring for a hospitalized child and uses a mobile hotspot, but the technical content focuses on combining theory and practice to add the tracing components Sleuth and Zipkin to a Spring Cloud project.

Why Use Distributed Tracing?

Microservice architectures split business logic into many services, often thousands, making error localization difficult. Tracing provides a Trace ID and Span ID that record the order and timing of service calls, enabling rapid fault isolation.

Core Concepts of Tracing

Span

Basic unit of work representing a single remote call.

64‑bit unique identifier.

Start and end timestamps allow latency measurement.

Trace

Aggregates multiple spans into a tree structure.

Represents the entire request flow across services.

Annotation

cs (Client Sent) – marks the start of a client request.

sr (Server Received) – records when the server receives the request.

ss (Server Sent) – records when the server sends the response.

cr (Client Received) – marks the end of the client request.

Integrating Spring Cloud Sleuth

1. Add Spring Cloud dependencies

<dependencyManagement>
    <dependencies>
        <!-- Spring Cloud dependencies -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2. Add Sleuth starter

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

3. Observe traces via logs

logging.level.org.springframework.cloud.openfeign=debug
logging.level.org.springframework.cloud.sleuth=debug

4. Start microservices

Run gateway, question, and admin services; logs will show trace and span IDs when Sleuth is enabled.

Zipkin – Visual Tracing

Zipkin provides a UI to view traces collected by Sleuth. It consists of Collection, Storage, API, and UI components.

Docker Deployment

docker run -d -p 9411:9411 openzipkin/zipkin

Access the UI at http:// :9411/zipkin .

Zipkin Maven Dependency

<!-- Zipkin tracing component -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

Configuration

# Zipkin server address
spring.zipkin.base-url=http://192.168.56.10:9411/
# Disable service discovery for Zipkin URL
spring.zipkin.discovery-client-enabled=false
# Use HTTP sender
spring.zipkin.sender.type=web
# Sample 100% of requests
spring.sleuth.sampler.probability=1

Testing

Invoke an API (e.g., /studytime/list/test/{id} ) and view the trace in Zipkin UI, which shows request latency, server processing time, and total duration.

Persisting Zipkin Data

By default Zipkin stores data in memory, which is lost on restart. For production, configure a persistent store such as Elasticsearch.

Elasticsearch via Docker

docker run --env STORAGE_TYPE=elasticsearch --env ES_HOSTS=192.168.56.10:9200 openzipkin/zipkin-dependencies

Elasticsearch provides scalable storage and query capabilities for trace data.

Conclusion

The author reflects on personal challenges while writing the tutorial and encourages readers to keep learning.

dockerMicroservicesElasticsearchDistributed TracingSpring CloudZipkinSleuth
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.