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 Spring Initializer 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.
1. Create a new spring project by following the same step in previous project creation.
<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.
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
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowThe Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
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.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
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.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork