Whether you deploy to the cloud or on-prem, one of the biggest challenges of a microservice or distributed architecture is monitoring.
Zipkin is a distributed tracing system that can help to solve some of these problems. Specifically, it helps gather data needed to troubleshoot latency issues.
In this article, we look at spring clouds support for Zipkin by setting up some spring boot REST services.
The example code is written in kotlin but can easily be ported over to java.
Source code for this example can be found on github:
You'll need some things to follow along with the example:
- Docker (including docker-compose)
- The source code cloned and imported into your IDE
Setting the scene
To demonstrate Zipkin's tracing abilities, we are going to bring up some fictitious services that our service can make requests to.
Our service will return the tax of an employee based on their name:
$ curl http://localhost:8080/tax\?name\=michael $ 10000.0
To do this, it needs some help for other services:
- An employee service which will return employee information by name
- A payroll service which will return the employees tax code by employee id.
Finally, our service will calculate and return the tax to be paid.
Additionally, we will bring up a Zipkin server which our service will push trace data to.
The employee and payroll service will be mocked out using mockserver. We will not be covering mockserver in this post.
There are two json files under the dist directory that tell the mock services how to respond to requests, including some delays which will slow down there response times.
To manage all this, we will be using a docker compose yml file:
Let's start everything up now by running:
docker-compose -f docker-compose.yml up
N.B you can optionally background the process by adding -d to the end of this command
Creating a Basic Rest Service
Creating the service is very simple, and if you're happy with spring boots defaults then this only involves adding a jar to your maven pom!
Default properties such as zipkin urls can be overidden under src/main/resources/application.properties
A full list of properties are listed in the spring cloud documentation
Hitting the endpoint
Start the service in your IDE and navigate to it in your browser:
|Mock Employee Service||http://localhost:8082/employeeService|
|Mock Payroll Service||http://localhost:8083/payrollService|
Now navigate to the Zipkin UI. Click on Find Traces and you will see the trace you created by opening the service url.
Click on a span to expand it:
Additionally you could use a tool like apache benchmark to put the service under some load. This will create more traces that can be viewed in the zipkin ui:
ab -n 500 -c 20 "http://localhost:8080/tax?name=michael"
We've seen just how easy it is to add tracing to your spring cloud application.