Cloud Native 6 min read

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.

macrozheng
macrozheng
macrozheng
Master Distributed Tracing with Spring Cloud Sleuth and Zipkin: A Step‑by‑Step Guide

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-service and ribbon-service. The ribbon-service calls user-service via 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.yml to 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 rate

Integrating 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.jar Access 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.bat to 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:9200

Restart user-service and 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 testing

Project Source Code

https://github.com/macrozheng/springcloud-learning

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ElasticsearchDistributed TracingzipkinSleuth
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

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.