Spring Cloud Integration with Sleuth for Service Traceability: Hands‑On Guide
This tutorial walks through integrating Spring Cloud Sleuth and Zipkin to achieve distributed service tracing in a microservice architecture, covering the underlying concepts, step‑by‑step implementation, log analysis, and optional asynchronous log collection via RabbitMQ.
Background
Microservice systems grow, request flows span many services, and diagnosing failures becomes difficult without a tracing tool.
Sleuth Overview
Spring Cloud Sleuth generates a Trace ID for an entire request chain and a Span ID for each service invocation. The core component Tracer creates these IDs and propagates them downstream.
Trace ID: unique identifier for the whole request path.
Span ID: identifies a single processing unit (e.g., an HTTP call) with start/end timestamps and metadata.
Implementation Steps
3.1 Add Dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>3.2 Configure Application
spring.application.name=eureka-consumer-sleuth-1
server.port=9013
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/3.3 Create Startup Class
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}3.4 Service Test
Two consumer projects eureka-consumer-sleuth-1 and eureka-consumer-sleuth-2 expose /trace-1 and /trace-2 endpoints.
@RestController
public class HelloController {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);
@Autowired private RpcService rpcService;
@GetMapping("/trace-1")
public String trace() {
LOGGER.info("trace-1, preparing request to trace-2...");
String result = rpcService.hello();
LOGGER.info("trace-1, received result: " + result);
return "trace-1, response: " + result;
}
}Calling http://localhost:9013/trace-1 produces logs such as:
2024-10-15 18:02:15.850 INFO [eureka-consumer-sleuth-1,58a2cabc2d29bcf6,58a2cabc2d29bcf6,false] ... : trace-1, preparing request to trace-2...
2024-10-15 18:02:15.864 INFO [eureka-consumer-sleuth-1,58a2cabc2d29bcf6,58a2cabc2d29bcf6,false] ... : trace-1, received result: hello, I am trace-2!
2024-10-15 18:02:15.860 INFO [eureka-consumer-sleuth-2,58a2cabc2d29bcf6,dd77986d1c5cf706,false] ... : trace-2, received requestThe log prefix [service-name,traceId,spanId,exportable] shows the service name, shared Trace ID, individual Span ID, and whether the data will be sent to an external system.
Integrating Zipkin
Zipkin is an open‑source distributed tracing system that collects trace data, provides a UI for searching traces, and supports multiple storage backends (In‑Memory, MySQL, Cassandra, Elasticsearch).
4.1 Build Zipkin Server
4.1.1 Create Project
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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>4.1.2 Application Properties
spring.application.name=zipkin-server
server.port=90204.1.3 Startup Class
@EnableZipkinServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}4.2 Add Zipkin Support to Clients
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency> # Zipkin server URL
spring.zipkin.base-url=http://localhost:9020
# Sample 100% of requests
spring.sleuth.sampler.percentage=1.0Default sampler is 0.1 (10%).
4.3 Service Test
Start zipkin-server, the two consumer services, and Eureka. Access http://localhost:9020/ to view the Zipkin UI, click “Find Traces”, and observe the traced request chain with service names, method names, and timings.
4.4 Asynchronous Log Collection (Optional)
When log volume is high, Sleuth can stream trace data to RabbitMQ and let Zipkin consume it.
4.4.1 Client Adjustments
<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> spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=adminRemove spring.zipkin.base-url from the client configuration.
4.4.2 Server Adjustments
<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> spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=adminIn this mode Sleuth publishes trace spans to RabbitMQ; Zipkin consumes them for analysis.
Conclusion
Combining Spring Cloud Sleuth with Zipkin provides a lightweight yet powerful solution for pinpointing performance bottlenecks and errors in complex microservice environments, enabling rapid diagnosis of problematic requests.
References
http://www.ityouknow.com/springcloud/2018/02/02/spring-cloud-sleuth-zipkin.html
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.
Pan Zhi's Tech Notes
Sharing frontline internet R&D technology, dedicated to premium original content.
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.
