Distributed Logging and Tracing using Zipkin and Sleuth

Calender

1st January 2024

Clock

16 min read

Author
Amarnath Kumar

Senior Associate Consultant - Development

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 :

 


		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.cloud
			spring-cloud-starter-sleuth
		
		
			org.springframework.cloud
			spring-cloud-sleuth-zipkin
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

 

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. 

Author Amarnath Kumar

Amarnath Kumar is a highly skilled Backend Developer with over 2+ years of experience in the industry. He is well-versed in the latest technologies and has hands-on experience in Core Java, Spring-Boot, Spring-Security, Hibernate, Apache Kafka messaging queue, and Blockchain application development based on Ethereum, Tron, and Relational databases like MySQL and PostgreSQL. He is proficient in API implementations, Web Services, Socket programming, and code enhancements. He has contributed significantly to the company's success through his work on various client projects, including Wethio-Exchange, Hedgex Exchange, and many more. He enjoys reading and exploring new technologies to enhance his knowledge and expertise.

Senior Associate Consultant - Development

bg bg

What's Trending in Tech

bg

Our Offices

India

INDIA

DG-18-009, Tower B,
Emaar Digital Greens, Sector 61,
Gurugram, Haryana
122011.
Unit- 117-120, First Floor,
Welldone Tech Park,
Sector 48, Sohna road,
Gurugram, Haryana
122018.
USA

USA

30N, Gloud St STR E, Sheridan, Wyoming (USA) - 82801
Singapore

SINGAPORE

10 Anson Road, #13-09, International Plaza Singapore 079903.

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.