Problem:- How to trace/debug the log for particular request when an issue comes in microservice. When there are so many microservices user request will span many of them and it will difficult to trace the logs for a particular request when an issue occurs.
Solutions:- Spring Cloud Sleuth combined with ZipKin, allow you to perform distributed tracing for Spring Boot Application, ZipKin trace the data between and within services. It is distributed tracking system originally developed by Twitter(X) and now available as open source.
EX:- Let’s suppose user send request and this request will go to multiple service and send the response to the user. So how to trace the request log of mulitiple services.
How to implement Distributed Tracing with Sleuth and ZipKin in spring Boot Services.
Step 1: Add the following Micrometer-related Maven dependencies on pom.xml file to your all services/microServices.
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-micrometer</artifactId>
</dependency>
NOTE:-
- micrometer-observation dependency will allow us to collect and report metrics from our application to Zipkin.
- micrometer-tracing-bridge-brave dependency will allow us to trace our spring boot application.
- zipkin-reporter-brave dependency allows us to send traces that we collect to Zipkin.
- feign-micrometer dependency was added because I am using feign in my microservices to call other APIs. This dependency will configure the micrometer to work with feign.
Step 2: Add given properties in application.properties file. These all property you can add all of the Service/MicroServices.
spring.zipkin.base-url=http://127.0.0.1:9411/
management.tracing.sampling.probability=1.0
logging.pattern.level='%5p [${spring.application.name}, %X{traceId}:-}, %X{spanId:-}]'
logging.level.org.springframework.web=DEBUG
Step 3: Download zipkin jar (download) and run this jar using given command.
java -jar zipkin-server-2.23.19-exec.jar
Step 4: Go to your bowser and run http://127.0.0.1:9411/zipkin
Download complete Code and run given GET api and test.
http://localhost:9091/customer-service/api/v1/employeeList
That’s all.