Spring Cloud Sleuth and Zipkin

This tutorial will teach you how to use Spring Cloud Sleuth with Zipkin to trace HTTP requests across your Microservices. Using Spring Cloud Sleuth and Zipkin, you can aggregate in one place the information about HTTP requests that your Microservices sent and the time it took for the HTTP Request to complete. This will help you identify slow-responding Microservices, so you can work on them to improve performance.

Please check this Spring Boot Microservices and Spring Cloud video course for step-by-step video lessons showing how to set up and use Spring Cloud Sleuth.

Spring Cloud Sleuth

Spring Cloud Sleuth will add additional details to logging information when it is logged. These are:

  • Span ID – The basic unit of work. For example, sending an HTTP Request will generate a new span id.  A new span id will be generated if another Microservice is called within the same flow. All of these spans will be grouped under the same trace id.
  • Trace ID – Is used to group several spans. The initial HTTP Request will generate a new trace id. If within the same flow, a Microservice sends an HTTP request to another Microservice, then the Trace Id for both HTTP Requests will be the same. HTTP Requests sent by two different users will generate two different trace IDs.

Enable Spring Cloud Sleuth

Add Sleuth Dependency

To enable Spring Cloud Sleuth in your Spring Boot Microservice, add the following dependency to POM.XML file.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

This single dependency will add support for Sleuth, which can send information to the Zipkin server.

Update application.properties file

Now when we have added spring-cloud-sleuth-zipkin dependency to pom.xml file we need to update application.properties file by adding new configuration properties:

spring.zipkin.service.name=zipkin
spring.zipkin.baseUrl=http://localhost:9411
spring.zipkin.sender.type=web
spring.sleuth.sampler.probability=1.0

where:

  • spring.zipkin.service.name – Is used to provide a name under which the Zipkin server is known. (Optional).
  • spring.zipkin.baseUrl – Is used to provide the domain and port number on which the Zipkin server is running. The default Zipkin server port number is 9411.
  • spring.zipkin.sender.type – Is used to provide how the information to Zipkin will be sent. A value of web will specify that information will be sent over HTTP. Other possible values, for example, are rabbit or Kafka.
  • spring.sleuth.sampler.probability – Is used to specify how much information needs to be sent to Zipkin. By default, Spring Cloud Sleuth sets all spans to non-exportable. That means that traces appear in logs but not in any remote store. A Value of 1.0 would mean 100% of all time. And a value of 0.1 would mean only 10%. If you want to ensure all spans are sent to Zipkin, provide a value of 1.0 here.

Note: If you get the following error when your spring boot application starts with Spring Cloud Sleuth enabled:

Error creating bean with name 'rabbitListenerContainerFactory'

then add the two following properties to application.properties file.

spring.rabbitmq.listener.direct.retry.enabled=true
spring.rabbitmq.listener.simple.retry.enabled=true

Sleuth Logging

After enabling Sleuth in your Spring Boot application, follow the steps above. You can log something to see how Trace and Span Ids are added to your logging information. To enable logging, you can use LoggerFactory, for example:

Logger logger = LoggerFactory.getLogger(this.getClass());

and then log something before sending an HTTP request and after sending the HTTP request. For example:

logger.info("Before sending GET Albums HTTP Request...");
List<AlbumResponseModel> albumsList = albumsServiceClient.getAlbums(userId) 
logger.info("Albums web service endpoint was called and recieved " + albumsList.size() + " items");

When you run your application, you should see similar information added to your logging.

[users-ws,88430cb09b5e45ff,48cc20a35c22f1ab,true]

where:

  • users-ws – Name of current Microservice,
  • 88430cb09b5e45ff – Trace Id,
  • 48cc20a35c22f1ab – Span id,
  • true – If the information is sent to Zipkin.

But first, we need to start up the Zipkin server.

Starting up Zipkin Server

To download the Zipkin server run the following command in your terminal window.

curl -sSL https://zipkin.apache.org/quickstart.sh | bash -s

Run Zipkin with:

java -jar zipkin.jar

Open Zipkin UI 

When you run Zipkin with the above java -jar command, by default, it will start on port 9411. To see Zipkin Dashboard, open the following URL in your browser window:

http://localhost:9411/zipkin/

And you should see something like this:

I hope this tutorial was helpful to you.

Spring Cloud Video Course

To learn more about building and running RESTful Microservices with Spring Boot and Spring Cloud, check out my online video courseSpring Boot Microservices and Spring Cloud.


Spring Cloud video course

Happy learning!

Leave a Reply

Your email address will not be published. Required fields are marked *