Configuring tracing with Zipkin , Spring Boot, Sleuth and Gradle
When there are a series of calls happening where Service A call Service B and Service B calls Service C it would help to profile how much of time is being spent in each of these services.
This analysis helps to identify bottlenecks in a microservice application.
Spring has made it easy to achieve this with minimal configuration. This involves 2 parts
- Installing the zipkin server which collects and displays traces and spans across microservices in a nice GUI
- Adding dependencies into your Spring application so that specific tracing implementations are available.
Trust me , No Code changes required. It is that simple
Lets see each step individually
Step 1 : Download and Run Zipkin sever
Download the zipkin server with the below command on your terminal. This would download the zipkin.jar into the folder from where the command was run.
curl -sSL https://zipkin.io/quickstart.sh | bash -s io.zipkin:zipkin-server:LATEST:slim zipkin.jar
Once downloaded you can run the server using the below command from the folder where you downloaded the zipkin.jar. It should not take more than a minute to start.
java -jar zipkin.jar
You can verify that zipkin sever is running by visiting the following site
Step 2: Configuring your Spring application with Spring Cloud Sleuth
There is plenty of content available on the internet on what “Sleuth” is hence I do not wish to cover it here. But for those who choose to remain on this site
Spring Cloud Sleuth allows you to aggregate and track log entries as requests move through a distributed software system. It is used to generate and attach the trace id, span id to the logs so that these can then be used by tools like Zipkin and ELK
Add the below dependencies into your build.gradle. Save and build.
runtimeOnly(group: 'org.springframework.cloud', name: 'spring-cloud-dependencies', version: '2020.0.4', ext: 'pom')
implementation(group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '3.0.2')
implementation(group: 'org.springframework.cloud', name: 'spring-cloud-sleuth-zipkin', version: '3.0.2')
That is all required to start tracing.
Step 3: Send out a HTTP request to your service
Using Postman or similar tools send out a HTTP request to your service. Now open the zipkin sever running on your machine and click on “Run Query”
You should now be seeing traces of the last HTTP request that you sent.