Table of Contents
Distributed Tracing with Spring Cloud Sleuth in Microservices
What have we learned so far,
PART 1 – MICROSERVICES INTRODUCTIONย –ย In this tutorial, we discussedย what microservices architecture is and how its different from monolithic architecture.
ย PART 2 โ SCENARIO TO DEVELOPย –ย In this tutorial, we discussedย the scenario to develop.
PART 3 – FEIGN CLIENTย – In this tutorial, we discussed how feign client simplifies the rest client consumption.
PART 4 – RIBBON CLIENTย –ย In this tutorial, we discussed howย ribbon client handles the client side load balancing.
PART 5 – EUREKA NAMING SERVERย –ย In this tutorial, we discussed howย Eureka Naming Server is used to dynamically register the services at one place.
PART 6 – ZUUL GATEWAYย –ย In this tutorial, we discussed how Zuul gateway acts as a single entry point to our application.
What is Spring Cloud Sleuth?
Spring Cloud Sleuth implements a distributed tracing solution for Spring Cloud, borrowing heavily from Dapper, Zipkin and HTrace. For most users Sleuth should be invisible, and all your interactions with external systems should be instrumented automatically. You can capture data simply in logs, or by sending it to a remote collector service.
Lets first understand the terminologies used in sleuth and zipkin
https://cloud.spring.io/spring-cloud-static/spring-cloud-sleuth/2.0.0.M9/single/spring-cloud-sleuth.html#_only_sleuth_log_correlation
One request will travel from different applications to process and It becomes very difficult to trace the logs as they will be logged in diffrent log files with no common identifier to identify the request. Spring cloud sleuth helps us by adding an identifier to all the logs.
Letsย implement it in our example.
Make below changes in zuul-gateway-service, account-service &customer-service.
1. Add below maven dependency in pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
2. Modify application.properties files to store logs in a file
account-service -> application.properties
server.port=8100 spring.application.name=account-service #Part 4 #customer-service.ribbon.listOfServers=http://localhost:8200,http://localhost:8201 #Part 5 eureka.client.service-url.default-zone=http://localhost:8761/eureka logging.level.root=DEBUG logging.level.org.springframework.web=ERROR logging.file=C:\logs\account-service.log
customer_service -> application.properties
server.port=8200 spring.application.name=customer-service eureka.client.service-url.default-zone=http://localhost:8761/eureka logging.level.root=DEBUG logging.level.org.springframework.web=ERROR logging.file=C:\logs\customer_service.log
3. Add logs in all controllers and methods in account-service & customer-service
4. Start our application in below order
eureka-naming-service, zuul-gateway-service, account-service & customer-service.
5. Now hit the below request
http://localhost:8765/account-service/accounts/1
Please see below image wherein sleuth has added a unique id as b31c813535f3d16e for the request, we can use this id to check the logs in all the application.
Source Code
Download source code of microservice with spring cloud from below git repository :
Lets go to our next tutorial where we will discuss
Part 8 โ Distributed Tracing With Spring Cloud Sleuth & Zipkin
โย In this tutorial we will understand below topics
โย What is Zipkin?
โย How to log the distributed logs at one place in microservices?
โย Setup Zipkin Server
โย Setting up RabbitMQ
โย How to implement theย Zipkinย in microservices?
PART 8 โ MICROSERVICES WITH SPRING CLOUD : DISTRIBUTED TRACING WITH SPRING CLOUD SLEUTH & ZIPKIN


