Distributed Logging and Tracing using Zipkin and Sleuth

Posted By : Amarnath

Dec 29, 2023

In app development on a microservices architecture, various services collaborate to fulfill client requests. A single client request may require multiple calls to different microservices to generate a response. In the event of exceptions or latency issues during these interactions, it becomes crucial to pinpoint the root cause of the problem. To address this, we leverage Spring Cloud Sleuth and Zipkin for distributed logging and tracing. Consequently, we will delve into the implementation of Distributed Logging Tracing using Sleuth Zipkin and explore related concepts.

 

 

Steps for implementing Zipkin in a microservices environment

 

To access Zipkin using a container locally, you can use Docker to run a Zipkin server as a container. Here are the steps:

 

Prerequisites:

 

  • Docker installed on your local machine.

 

Steps to run Zipkin as a Docker container:-

 

1. Pull the Zipkin Docker image:

 

Open your terminal or command prompt and run the following command to pull the Zipkin Docker image:

 

docker pull openzipkin/zipkin

 

2. Run Zipkin container:

 

Once the image is pulled, execute the following command to run Zipkin as a Docker container:

 

docker run -d -p 9411:9411 --name zipkin-container openzipkin/zipkin

 

This command does the following:

 

  • -d: Runs the container in detached mode.
  • -p 9411:9411: Maps port 9411 of the container to port 9411 on your local machine. This is the default port used by Zipkin for the web interface.
  • -name zipkin-container: Assigns a name (zipkin-container) to the running container.

 

3. Access Zipkin UI:


Open a web browser and go to http://localhost:9411 (assuming you haven't changed the port mapping). This will take you to the Zipkin UI, where you can view and analyze the traces collected from your Spring Boot application.
 

4. Integrate Spring Boot application with Zipkin:

 

Make sure your Spring Boot application is configured to send traces to the Zipkin server by setting the spring.zipkin.base-url property in your application properties or YAML file, as mentioned in the previous instructions.

 

With these steps completed, your Spring Boot application should start sending traces to the Zipkin server running in the Docker container. You can then use the Zipkin UI to visualize and analyze the distributed traces generated by your application.

 

To implement Zipkin distributed tracing in a Spring Boot application, you need to integrate Zipkin dependencies and configure your application to send traces to the Zipkin server. Here's a step-by-step guide:

 

Also, Explore | How To Create A Staking Smart Contract

 

Steps to integrate Zipkin in a Spring Boot application:

 

1. Create a New Spring Boot Project:

 

You can create a new Spring Boot project using Spring Initializr or your preferred IDE (like IntelliJ IDEA or Eclipse). Ensure that you include the necessary dependencies for Spring Cloud Sleuth and Zipkin.

 

2. Add Zipkin and Sleuth dependencies to your pom.xml:
 

Add the necessary dependencies for Spring Cloud Sleuth and Zipkin:  

 

For Maven :

 


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-sleuth</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-sleuth-zipkin</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	

 

For Gradle :-

 


dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
    implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

 

 

3. Configure Zipkin properties:

 

In each microservice's application.properties or application.yml, set the URL of your Zipkin server:

 

For application.properties:

 

spring.zipkin.base-url=http://localhost:9411

 

For application.yml:

 

spring:
  zipkin:
    base-url: http://localhost:9411

 

4. Run the application and check the logs.

 

For Maven project :

 

mvn spring-boot:run

 

For Gradle project

 

./gradlew bootRun

 

5. After hitting the api you can see the logs on Zipkin UI

 

While Request through the client.

 

 

6. Final Logs are collected on Zipkin server and for access to zipkin UI  in you browser :

 

http://localhost:9411/zipkin/

 

 

Also, check | An Incredibly Easy Method to Mine Cryptocurrency on Ubuntu

Conclusion

 

Implementing Zipkin in a Spring Boot project involves integrating distributed tracing to monitor and track requests across microservices.Resolve any conflicts or issues related to dependencies, versions, or runtime errors to ensure smooth functionality.

By following above steps, you can successfully integrate and utilize Zipkin for distributed tracing within your Spring Boot microservices architecture.

 

Reference

 

a. For creating project you can use spring initilizer service  

b. For zipkin and sleuth dependencies visit mavenrepository.com service  

c. For Docker tutorial  visit  docker official website https://docs.docker.com/get-docker/ or got to my githup page for docker container for image and run on local server.

d. For all the set up with real project you can see my code on Github.

 

Interested in web or mobile app development to bring your vision into reality, connect with our skilled app developers to get started. 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 21, 2024 at 11:54 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text