How to Integrate Zipkin with Spring Cloud Sleuth for Distributed Tracing
This guide walks through setting up a Zipkin server, configuring Spring Cloud Sleuth to send trace data via HTTP or RabbitMQ, and using the Zipkin UI to monitor request latency and service dependencies in a Spring Boot microservices environment.
Building on a previous article about using ELK for log collection, this tutorial explains why traditional log analysis falls short for latency monitoring and introduces Zipkin, an open‑source distributed tracing system based on Google Dapper.
Zipkin Overview
Zipkin collects trace spans from services, stores them, and provides a REST API and a Web UI for querying trace timelines and service dependency graphs.
Step 1 – Build a Zipkin Server
Create a Spring Boot project named zipkin-server and add the following dependencies to pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Add a main class annotated with @EnableZipkinServer and @SpringBootApplication:
@EnableZipkinServer
@SpringBootApplication
public class ZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinApplication.class, args);
}
}Configure the server port (default 9411) in application.properties:
spring.application.name=zipkin-server
server.port=9411Run the application and open http://localhost:9411/ to see the Zipkin UI.
Step 2 – Configure Spring Cloud Sleuth to Send Traces to Zipkin
In each microservice (e.g., trace-1 and trace-2) add the Zipkin starter dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>Set the Zipkin server address in application.properties (the HTTP URL can be omitted if the server runs on the default port 9411):
spring.zipkin.base-url=http://localhost:9411Collecting Traces via Message Middleware
To send traces asynchronously through RabbitMQ, add the stream extensions:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>Remove the HTTP‑only spring.zipkin.base-url property and add RabbitMQ settings:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=springcloud
spring.rabbitmq.password=123456On the Zipkin server side, add the corresponding collector dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>After rebuilding and restarting both the client services and the Zipkin server, a RabbitMQ exchange named sleuth will receive trace messages.
Testing and Analysis
Start eureka-server, trace-1, trace-2, and the Zipkin server. Send a few HTTP requests to http://localhost:9101/trace-1. When the trace is sampled (e.g., using AlwaysSampler), the trace appears in the Zipkin UI, and the corresponding RabbitMQ message can be seen in the sleuth exchange.
In the Zipkin UI you can explore individual trace details, view the request latency breakdown, and inspect the automatically generated service dependency graph:
Complete Example
The full source code is available in two repositories:
GitHub: https://github.com/dyc87112/SpringCloud-Learning/
Gitee: https://gitee.com/didispace/SpringCloud-Learning/
These projects contain the trace-1, trace-2, and zipkin-server modules demonstrated above.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
