Complete microservices architecture with real examples — Step 7 — Distribute log tracing
Hello guys! Welcome again to the Agnasarp! Now we are in the last step of the implementation of microservices architecture. Here we are going to implement the distributed log tracing and it will be handled by Sleuth and Zipkin. There are two unique ids that are called Trace ID and Span ID to implement the log tracing and with them, we can find where the request was started, where the request ended, where the request was successful, and where the request got failed, and so on.
We have to download the Zipkin server and implement the Zipkin client and Sleuth in all our microservices.
Download Zipkin Server
- Go to https://zipkin.io/ in the browser.
- Click Quickstart
- Click on the latest release on the Java version. You can run it as a dockerized version but for the sake of simplicity, we have chosen the Java version here.
The file name of Zipkin is zipkin-server-2.23.2-exec.jar so we can use the below jar command to start the server.
java -jar zipkin-server-2.23.2-exec.jar
It will run on http://127.0.0.1:9411/ and when we call it using a browser, we get
Now we have to add Zipkin client and Sleuth in our microservices. Our main microservices are Department Service and User Service so, we have to and these dependencies only to those services.
And need to add Zipkin base URL in both the services as highlighted below.
After we run Department Service and User Service with the Zipkin server, it enables the below information in every log that we have already defined.
[Application name, Trace ID, Span ID]
Trace ID is unique across all the microservices for a particular request. Span ID will be changed accordingly in each and every microservices.
If we call Get User API it will call Get Department API as well inside the application. Therefore in User Service, we got
2021–05–28 01:25:47.963 INFO [USER-SERVICE,be5879f5e04b09ea,be5879f5e04b09ea] 6680 — — [nio-8380-exec-6] c.a.user.controller.UserController : Inside getUserById method of UserController
2021–05–28 01:25:47.963 INFO [USER-SERVICE,be5879f5e04b09ea,be5879f5e04b09ea] 6680 — — [nio-8380-exec-6] com.agnasarp.user.service.UserService : Inside getUserWithDepartment method of UserService
And in Department Service, we got
2021–05–28 01:25:48.019 INFO [DEPARTMENT-SERVICE,be5879f5e04b09ea,db2285f8a3a30819] 11236 — — [nio-8280-exec-8] c.a.d.controller.DepartmentController : Inside getDepartmentById method of DepartmentController
2021–05–28 01:25:48.019 INFO [DEPARTMENT-SERVICE,be5879f5e04b09ea,db2285f8a3a30819] 11236 — — [nio-8280-exec-8] c.a.d.service.DepartmentService : Inside getDepartmentById method of DepartmentService
As you can see, the highlighted Trace ID is equal in both the services because this is the same request which went through Get User -> Get Department. The next one which is the Span ID that is unique for the service call within itself.
We will select the user-service as the serviceName.
We can see the request when we hit the RUN QUERY button.
We can go through all the information provided by Zipkin as below. Service names, trace ids, span ids, durations, HTTP method, resource path, controller class, controller method, client address, etc. can be found as per the below image.
On the dependency page, we can get an idea about dependencies over microservices we created. As per the below image, User Service dependents on Department Service.
Now we have come to the end of our microservices implementation. In summary, we have implemented the Department Service, the User Service, the Service Registry, the API Gateway, the Circuit Breaker Pattern with Hystrix, the Config Server, and at last, the Distributed Log Tracing with Zipkin and Sleuth. These are the basic elements in the microservices architecture and we can implement a lot from these base models. You can get all the source code of all components from GitHub with the below links and reach our past posts through the links below. Hope you guys enjoy our posts on microservices architecture and we wish you all the best! Bye bye!
Download source from github:
Go to Step 1 — Department Microservice
Go to Step 2 — User Microservice
Go to Step 3 — Service Registry
Go to Step 4 — API Gateway
Go to Step 5 — Circuit Breaker
Go to Step 6— Config Server
Originally published at https://www.agnasarp.com on May 27, 2021.