EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Cloud Sleuth
Secondary Sidebar
Spring Tutorial
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Cloud Sleuth

Spring Cloud Sleuth

Introduction to Spring Cloud Sleuth

Spring cloud sleuth provides auto-configuration of spring boot for distributed tracing. It will configure everything which we require to get started. Sleuth includes where the trace data is reporting, how traces we require if we are required to send remote field, and which libraries need to be traced. It is used to add the traces into slf4j mdc.

What is spring cloud sleuth?

  • It is used to span ids and add traces into the slf4j mdc, so we can extract the logs from a given span or trace using a log aggregator.
  • It is also used to push the sampling decision below for tracer implementation. There are multiple ways we can change the sampling decision at runtime.
  • To implement the project, we need to add starter sleuth dependency in the pom.xml file.

Spring cloud sleuth is adding two types of IDs in our logging.

1) Span ID

2) Trace ID

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • Span ID represents the unit of work like sending a request of HTTP. Trace ID in sleuth contains a number of span IDs, creating a tree structure.

When calling one microservice to other traces, the ID is the same; it will not change throughout the procedure.

Use of Spring Cloud Sleuth and Zipkin

  • In spring, cloud sleuth traces connect it from service to service using header propagation. It will use the default format for traces as B3.
  • We can also change the sampling decision by skipping the client span reporting. We can achieve this by setting the spring sleuth web client skip pattern (spring.sleuth.web.client.skip-pattern) with the path pattern as skipped.
  • Zipkin is the distributed tracing system; it was used to gather the timing data needed to troubleshoot the latency problems. Zipkin feature includes the lookups and collection of the data.
  • Zipkin is very useful at the time of debugging when we have implemented multiple microservices and our application is performing slowly at a certain period.
  • Sleuth is nothing but the tool which was provided by spring cloud, used to generate service calls, the diagnostic context of mapping and headers, trace ID, and span ID.
  • Spring cloud sleuth is used in tools like ELK and Zipkin. Sleuth is used in Zipkin for processing logs and storing the indexes.
  • Sleuth is from the spring cloud family; after adding the same into the classpath, it will automatically be integrated between communication channels.

Spring Cloud Build project

The below example shows build projects.

1. Create a project template of spring cloud build by using spring initializer.

  • In the below step, we have provided project group name as com. example, artifact name as springcloudsleuth, project name as springcloudsleuth, and selected java version as 8.
  • In the below project, we have selected spring web and sleuth dependency to implement the spring cloud build project.

Group – com. example Artifact name – springcloudsleuth
Name – springcloudsleuth Spring boot – 2.6.0
Project – Maven Project Description – Project for springcloudsleuth
Java – 8 Dependencies – sleuth, spring web
Package name – com. example.springcloudsleuth
Spring Cloud Sleuth - template

2. After generating the project extract files and open this project by using the spring tool suite.

After generating the project using the spring initializer in this step, we extract the jar file and open the project using the spring tool suite.

Spring Cloud Sleuth - Extract Files

3. After opening the project using spring tool suite check the project and its files.

In this step, we are checking all the project template files. We also need to check maven dependencies and system libraries.

Spring Cloud Sleuth - Project files

4. Add dependency packages.

In this step, we are adding the contract verifier dependency in the build project.

Code:

<dependency> -- Start of dependency tag.
<groupId>org.springframework.cloud</groupId> -- Start and end of groupId tag.
<artifactId>spring-cloud-starter-sleuth</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.

Spring Cloud Sleuth - contract verifier

5. Build the project.

Spring Cloud Sleuth - project building

Example of Spring cloud sleuth

We are using the sleuth application to configure the first microservice. The below step shows example of sprig cloud sleuth as follows.

1. Create controller for the first microservice –

Microservice 1

Code:

@SpringBootApplication
public class micro1 {
public static void main(String[] args) {
SpringApplication.run(micro1.class, args);
}
}
@RestController
class ZipkinController{
@Autowired
RestTemplate rTemp;
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@Bean
public springcloudsleuth alwaysSampler() {
return new springcloudsleuth();
}
@GetMapping(value="/sleuth-Zipkin")
public String zipkinService1()
{
LOG.info("spring cloud sleuth");
String response = (String) rTemp.exchange ("http://localhost:8080/zipkin",
HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}).getBody ();
return "springcloudsleuth...";
}
}

Image 6

2. Configure application.properties file –

Code:

server.port = 8080
spring.application.name = springcloudsleuth-server1

Image 7

3. Create another three microservices and configure the application.properties file, use the port 8081, 8082, and 8083 –

Microservice 2

Image 8

Microservice 3

Image 9

Microservice 4

Image 10

4. After creating and configuring all microservices start all microservices –

Start the first microservice:

Image 11

Start the second microservice:

Image 12

Start the third microservice:

Image 13

Start the fourth microservice:

Image 14

5. Open zipkin dashboard and check the services –

Image 15

Web Request with Service Access

  • The extremely customizable web requests view allows us to examine more popular web requests from all of our services and search for requests that originate from directed to specific URLs.
  • The view of service access is easily customizable and serves as a good starting point for a more in-depth examination of our services.
  • Using sleuth it is very easy to add the required dependency to implement a project.
  • We can configure an alternate header format by specifying similar data formats.

Conclusion

Spring cloud sleuth is used to span ids and add traces into the slf4j mdc, so we can extract the logs from a given span or trace by using log aggregator. It will configure everything which we require to get started.

Recommended Articles

This is a guide to Spring Cloud Sleuth. Here we discuss the definition, what is spring cloud sleuth, examples, code, and applications. You may also have a look at the following articles to learn more –

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)
  1. Spring Cloud Dependencies
  2. Spring Cloud Version
  3. Spring Cloud Components
  4. Spring cloud stream
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more