Spring Cloud Sleuth Tutorial
Overview
Spring Cloud Sleuth provides distributed tracing capabilities for Spring Cloud applications. It integrates with popular tracing systems like Zipkin and allows you to trace requests across microservices, providing insights into the flow and performance of requests.
Key Features of Spring Cloud Sleuth
Spring Cloud Sleuth offers several features that facilitate tracing in a microservices architecture:
- Distributed Tracing: Track the flow of requests across microservices.
- Correlation IDs: Automatically add trace and span IDs to logs for easy correlation.
- Integration with Zipkin: Send trace data to Zipkin for visualization and analysis.
- Context Propagation: Propagate tracing context across threads and services.
Setting Up Spring Cloud Sleuth
To set up Spring Cloud Sleuth, add the following dependencies to your project:
// build.gradle
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
implementation 'org.springframework.cloud:spring-cloud-starter-zipkin'
}
This adds the necessary dependencies for Spring Cloud Sleuth and Zipkin integration.
Configuration
Configure Spring Cloud Sleuth and Zipkin in the application.properties
file:
// application.properties
spring.zipkin.baseUrl=http://localhost:9411
spring.sleuth.sampler.probability=1.0
The spring.zipkin.baseUrl
property sets the URL of the Zipkin server, and the spring.sleuth.sampler.probability
property sets the sampling rate (1.0 means every request will be traced).
Tracing a Request
Spring Cloud Sleuth automatically adds trace and span IDs to requests and logs. Here's an example of a simple service:
// ExampleService.java
@Service
public class ExampleService {
private static final Logger logger = LoggerFactory.getLogger(ExampleService.class);
public String process() {
logger.info("Processing request in ExampleService");
return "Processed";
}
}
// ExampleController.java
@RestController
public class ExampleController {
private final ExampleService exampleService;
@Autowired
public ExampleController(ExampleService exampleService) {
this.exampleService = exampleService;
}
@GetMapping("/process")
public String process() {
return exampleService.process();
}
}
The trace and span IDs will be automatically added to the logs, allowing you to trace the request flow.
Viewing Traces in Zipkin
Start the Zipkin server by running the following command:
$ curl -sSL https://zipkin.io/quickstart.sh | bash -s
$ java -jar zipkin.jar
Access the Zipkin UI at http://localhost:9411 to view the traces.
Propagating Trace Context
Spring Cloud Sleuth propagates the tracing context across threads and services. Here's an example of an asynchronous service:
// AsyncService.java
@Service
public class AsyncService {
private static final Logger logger = LoggerFactory.getLogger(AsyncService.class);
@Async
public CompletableFuture asyncProcess() {
logger.info("Processing request asynchronously in AsyncService");
return CompletableFuture.completedFuture("Async Processed");
}
}
// AsyncController.java
@RestController
public class AsyncController {
private final AsyncService asyncService;
@Autowired
public AsyncController(AsyncService asyncService) {
this.asyncService = asyncService;
}
@GetMapping("/async-process")
public CompletableFuture asyncProcess() {
return asyncService.asyncProcess();
}
}
The tracing context will be propagated to the asynchronous method, ensuring trace continuity.
Key Points
- Spring Cloud Sleuth provides distributed tracing for Spring Cloud applications.
- Automatically adds trace and span IDs to logs for easy correlation.
- Integrates with Zipkin for trace visualization and analysis.
- Propagates tracing context across threads and services.
- Supports asynchronous tracing for complete trace continuity.
Conclusion
Spring Cloud Sleuth is a powerful tool for distributed tracing in a microservices architecture. By leveraging its features, developers can gain insights into the flow and performance of requests, making it easier to diagnose and resolve issues. Happy coding!