Spring Cloud Sleuth is a distributed tracing framework that allows you to trace the flow of requests through a microservice architecture. Zipkin is a popular open-source tool for storing and visualizing these traces.
To add Spring Cloud Sleuth to your Spring Boot application, add the following dependency to your pom.xml
file:
1 2 3 4 |
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-sleuth</artifactid> </dependency> |
To add the Zipkin server, you need to add the following dependency:
1 2 3 4 5 6 |
<dependency> <groupid>io.zipkin.java</groupid> <artifactid>zipkin-server</artifactid> <version>2.21.1</version> <scope>test</scope> </dependency> |
In your main class, add the @EnableZipkinServer
annotation to enable the Zipkin server:
1 2 3 4 5 6 7 8 |
@SpringBootApplication @EnableZipkinServer public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); } } |
Now, start the Zipkin server by running the main class. You can access the Zipkin UI by navigating to http://localhost:9411
in your browser.
Next, you need to add Sleuth to your car microservice. In the pom.xml
file, add the following dependency:
1 2 3 4 |
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-sleuth</artifactid> </dependency> |
In your main class, add the @EnableSleuth
annotation to enable Sleuth:
1 2 3 4 5 6 7 8 |
@SpringBootApplication @EnableSleuth public class CarServiceApplication { public static void main(String[] args) { SpringApplication.run(CarServiceApplication.class, args); } } |
Now, you need to configure the microservice to send trace data to the Zipkin server. You can do this by adding the following properties to the application.properties
file:
1 2 3 |
spring.zipkin.base-url=http://localhost:9411 spring.sleuth.sampler.percentage=1.0 |
The spring.zipkin.base-url
property specifies the URL of the Zipkin server, and the spring.sleuth.sampler.percentage
property sets the sample rate to 100%. This means that all requests will be traced.
To test your Sleuth and Zipkin setup, make a request to the car microservice. You should be able to see the trace data in the Zipkin UI.
Here is an example of a simple car REST API using Spring Cloud Sleuth and Zipkin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
@RestController @RequestMapping("/api/cars") public class CarController { @GetMapping public List<Car> getAllCars() { // Implementation goes here } @GetMapping("/{id}") public Car getCarById(@PathVariable long id) { // Implementation goes </code> } @PostMapping public Car createCar(@RequestBody Car car) { // Implementation goes here } @PutMapping("/{id}") public Car updateCar(@PathVariable long id, @RequestBody Car car) { // Implementation goes here } @DeleteMapping("/{id}") public void deleteCar(@PathVariable long id) { // Implementation goes here } } |
1 2 3 |
That's it! Your car microservice is now set up with Spring Cloud Sleuth and Zipkin for tracing requests. |