Master Distributed Tracing with Spring Cloud Sleuth and Zipkin: A Step‑by‑Step Guide
Learn how to integrate Spring Cloud Sleuth for request tracing, configure Zipkin to collect and visualize logs, and persist trace data to Elasticsearch, with detailed code snippets and step‑by‑step instructions for setting up services, running Zipkin, and analyzing trace information.
Spring Cloud Sleuth Overview
As systems grow, service‑to‑service calls become complex, making it hard to pinpoint delays or errors. Spring Cloud Sleuth provides distributed tracing to visualize the call chain of a request across multiple services.
Adding Request Tracing to Services
We demonstrate tracing between
user-serviceand
ribbon-service. The
ribbon-servicecalls
user-servicevia
RestTemplate.
Add tracing support to both services.
Include the required dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>Configure
application.ymlto point to the Zipkin server and set the sampling probability:
spring:
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 0.1 # set Sleuth sampling rateIntegrating Zipkin for Log Collection and Analysis
Zipkin, an open‑source project from Twitter, collects and visualizes tracing data generated by Spring Cloud Sleuth.
For Spring Boot 2.0+, you can run the Zipkin server without building it yourself:
java -jar zipkin-server-2.12.9-exec.jarAccess the Zipkin UI at
http://localhost:9411.
Start
eureka-server,
ribbon-service, and
user-service, then repeatedly call
http://localhost:8301/user/1. Zipkin will display the traced requests, showing each service’s latency.
Persisting Trace Information
By default Zipkin stores traces in memory, which are lost on restart. To persist data, you can configure Zipkin to use Elasticsearch.
Install Elasticsearch
Download Elasticsearch 6.2.2 zip package and unzip it.
Run
bin/elasticsearch.batto start Elasticsearch.
Configure Zipkin to Store Traces in Elasticsearch
Run Zipkin with storage parameters:
# STORAGE_TYPE: storage type, ES_HOSTS: Elasticsearch address
java -jar zipkin-server-2.12.9-exec.jar --STORAGE_TYPE=elasticsearch --ES_HOSTS=localhost:9200Restart
user-serviceand
ribbon-service, then invoke the endpoint again. If Kibana is installed, you can view the stored traces.
Additional Startup Parameters
Refer to the official Zipkin documentation for more options: https://github.com/openzipkin/zipkin/tree/master/zipkin-server#elasticsearch-storage
Modules Used in the Project
springcloud-learning
├── eureka-server # Eureka registry
├── user-service # Provides CRUD API for User objects
└── ribbon-service # Service used for Ribbon call testingProject Source Code
https://github.com/macrozheng/springcloud-learning
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.