Spring Cloud Zipkin And Sleuth Using Spring Boot

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.

Check Out What It Takes To Build A Successful App Here

As it is from the spring cloud family, once added to the CLASSPATH, it automatically integrated to the common communication channels like –

  • requests made with the RestTemplate etc.
  • requests that pass through a Netflix Zuul micro proxy
  • HTTP headers received at Spring MVC controllers
  • requests over messaging technologies like Apache Kafka or RabbitMQ etc.

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 Spring Initializer Add few dependencies in it,

  • Web
  • Sleuth
  • Make sure to add spring cloud version, In my case, I am using Edgware.SR3

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.

 

Our BOT Model Helped A HR Tech Company To Build Their Technology

Now we have done with the Sleuth server, let’s look for the Zipkin server.

1. Create a new spring project by following the same step in previous project creation.

  • Add Zipkin autoconfigure UI and Zipkin server.
  • Add the same spring cloud version
           <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

4. 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);
 }
}

5. 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.

coma

Conclusion-

In this article we learn about spring cloud zipkin and sleuth and their integration to analysis. Hope you enjoyed this article. If you any questions or doubts, Please comment us

 

Get A Team Of Full Stack Developers Started Within 24 Hours

Deepak

Tech Expert

Deepak is a Full-stack developer with around 3 years of experience. He is an expert in building Java integrated web applications, Creating REST APIs with well designed, testable and efficient and optimized code. He is having experience in web technologies like AngularJS, Angular6, JavaScript, etc. He is a Lead developer at Codegrip. He loves to solve technical problems and helping others.

Get in touch for a detailed discussion.

Hear From Our 100+ Customers
coma

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

We had very close go live timeline and MindBowser team got us live a month before.

author
Shaz Khan
CEO, BuyNow WorldWide
coma

They were a very responsive team! Extremely easy to communicate and work with!

author
Kristen M.
Founder & CEO, TotTech
coma

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

author
Chacko Thomas
Co-Founder, TEAM8s
coma

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

author
Dave Dubier
Founder & CEO, MangoMirror
coma

Mindbowser was very helpful with explaining the development process and started quickly on the project.

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

author
Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness
coma

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

They're very tech-savvy, yet humble.

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.

author
Katie Taylor
Founder, Child Life On Call
coma

As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.

author
Radhika Kotwal
Founder of Courtyardly
coma

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

author
David Cain
CEO, thirty2give
coma

We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!

author
Bart Mendel
Founder, Mindworks