Part 7 – Distributed Tracing with Spring Cloud Sleuth in Microservices

slueth2Blogo

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.

Part 61 - Capture

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

Microservices Tutorial

Microservices Tutorial

Related Posts

Microservices Interview Questions