In this article, you will learn about Zipkin and Sleuth.
Let’s consider one scenario, you are working on multiple microservices and you want to trace those service logs and performance. Now it becomes very hard to look for those services and find the exact log or issue.
Here we can use Zipkin to overcome these kinds of issues.
Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in service architectures. Features include both the collection and lookup of this data.
This is very useful during debugging when lots of microservices are implemented and the application becomes slow in any particular situation. In such a case, we first need to identify to see which underlying service is actually slow. Once the slow service is identified, we can work to fix that issue. Distributed tracing helps in identifying that slow component among in the ecosystem.
Sleuth is the tool provided by Spring Cloud. It is used to generate trace id, span id, and this information to service calls in the headers and Mapping Diagnostic Context. So that it can be used by tools like Zipkin, ELK, etc to store indexes and process logs file.
As it is from the spring cloud family, once added to the CLASSPATH, it automatically integrated to the common communication channels like –
Using Sleuth is very easy. We just need to add it’s started pom in the spring boot project. It will add the Sleuth to the project and so in its runtime.
In this, we are going to implement one microservice in that we will configure Sleuth and Zipkin. We will create two microservice one is for Sleuth and one is for the Zipkin server.
1. Create one spring boot project now. There are several ways to create a spring boot project, We are going to use https://start.spring.io/ Add few dependencies in it,
2. Once the project is created now import in your IDE and run it once to make sure everything is working fine.
3. The next step would be to create a RestController class. I am going to use the main method and annotate it with @RestController.
@SpringBootApplication @RestController public class RestServiceApplication { private static final Logger LOG = LoggerFactory.getLogger(RestServiceApplication.class); @Autowired private RestTemplate restTemplate; @GetMapping("/check") public String check() { LOG.info("check service called...."); return "The api executed successfully"; } public static void main(String[] args) { SpringApplication.run(RestServiceApplication.class, args); } }
4. Now the run the application. Once you run the application, hit the API which we have created.
5. Once the API gets called, you’ll notice that on IDE console you’ll find some output with some kind of keys. That keys represent the trace id and span id.
– These are the ids that are going to be used by the Zipkin server.
Now we have done with the Sleuth server, let’s look for the Zipkin server.
<dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency>
2. Add one annotation in the main application
@SpringBootApplication @EnableZipkinServer public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); } }
3. Add server port,
server.port=9411
Now again jump back to the sleuth server and edit the controller so that we can call another server using RestTemplate. The code will look like this,
package com.example.sleuth.api; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @SpringBootApplication @RestController public class RestServiceApplication { private static final Logger LOG = LoggerFactory.getLogger(RestServiceApplication.class); @Autowired private RestTemplate restTemplate; @GetMapping("/check") public String check() { LOG.info("check service called...."); return "The api executed successfully"; } @GetMapping("/check-another") public String checkAnother() { LOG.info("Check another service called...."); return restTemplate.getForObject("http://localhost:8080/check", String.class); } public static void main(String[] args) { SpringApplication.run(RestServiceApplication.class, args); } }
Create one another class called CloudConfig where all the configuration related RestTemplate and AlwaysSampler will be present. And that is going to create magic in the application,
import org.springframework.cloud.sleuth.sampler.AlwaysSampler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class CloudConfig { @Bean public RestTemplate template() { return new RestTemplate(); } @Bean public AlwaysSampler defaultSampler() { return new AlwaysSampler(); } }
We are done with coding now, let’s run both applications. Now hit API a few times to generate some amount of logs. Once done, open the localhost:9411 URL in the browser. After that, you will see the Zipkin UI in that you can find the whole details of logs, the API call duration, etc.
You can select a specific service from the Service Name list.
That’s it, We are done, I hope this blog will help you in the future.