Recent Tutorials and Articles
    Service Tracing with Spring Boot and ZipKin
    Published on: 6th October 2016
    Posted By: Amit Kumar

    This tutorial will demonstrate service tracing using ZipKin in services developed on Spring Boot.

    Abstract


    Microservices architecture is quite useful to build highly available and targeted scalable ployglot applications. It basically involves segregating an application into multiple lightweight distributed stateless services.

    However, there are many challenges with managing these distributed stateless services. One of those challenge is debugging latency issues. Since a request now may be served using a chain of multiple services, it is quite problematic to figure out the areas taking longer time in processing.

    This problem can be solved conservatively by logging the time taken by each service. This approach however has limitations such as too much logging and need to analyse logs from multiple services.

    What if there was a way to control the amount of logging and visualise service call trace in a nice intuitive web dashboard? This is where service tracing tools come into picture.

    One of popular service tracing tool is ZipKin. It is based on Google Dapper whitepaper and comes with a nice UI to visualise call trace and service dependencies.

    In this tutorial, we will be setting up ZipKin, develop two services and analysing their trace using ZipKin web dashboard.

     

    Pre-requisites


    Here are the required softwares and skills required to effectively follow instructions in this tutorial - 

    1. Java 8 or later
    2. Apache Maven (you can verify it with command: mvn -version)
    3. Basic knowledge of creating JAX-RS based REST services using Spring Boot. If you are new to Spring Boot, you are strongly recommended to read - Developing JAX-RS Services using Spring Boot

     

    Setting up ZipKin Tracing Server


    We will start off with setting up ZipKin server. ZipKin server itself is based on Google Dapper and built using Spring Boot. Being a Spring Boot application, setting it up is as simple as downloading and executing a JAR file.

    Windows:

    On windows, you can download ZipKin jar file from location - ZipKin Maven Jar. Once jar file is download, simply run it using below command - 

    java -jar zipkin.jar

     

    Linux:

    On Linux, it is even easier and you can download and run it using below commands -

    # Download Jar file
    wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
    
    # Start ZipKin server
    java -jar zipkin.jar

    Once you have stared ZipKin server, you can access its dashboard at http://localhost:9411.

     

    Building and Starting Up Author and Tutorial Services


    Next step is to develop two services to demonstrate service tracing. We will be using following two services -

    • author-services - Exposes an Author resource responsible for providing author details for provided author id.
    • tutorial-services - Exposes a Tutorial resource responsible for providing tutorial details for provided tutorial id. This service internally calls author service to get author details of a Tutorial.

    Since our main focus is Service tracing, we will not be discussing creation of these services in details. Instead, we will be download these from GitHub repository here - https://github.com/aksain/microservices/tree/master/spring-cloud/service-tracing/zipkin. If you are new to REST services, you can get started with Developing JAX-RS Services using Spring Boot.

     

    Once you have downloaded these services, let's first go to directory author-services  and execute following commands to build and run it - 

    # build the project
    mvn clean install -DskipTests
    
    # Start author services
    java -jar target/author-services-0.0.1-SNAPSHOT.jar

    You should see following logs in the end of console in case of successful starting of author services - 

    2016-10-21 09:43:56.304  INFO [author-services,,,] 28831 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
    2016-10-21 09:43:56.448  INFO [author-services,,,] 28831 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 7777 (http)
    2016-10-21 09:43:56.453  INFO [author-services,,,] 28831 --- [           main] c.a.m.author.AuthorServicesApplication   : Started AuthorServicesApplication in 11.977 seconds (JVM running for 12.631)

     

    Similarly, now move to directory tutorial-services  and execute following commands to build and run it - 

    # build the project
    mvn clean install -DskipTests
    
    # Start tutorial services
    java -jar target/tutorial-services-0.0.1-SNAPSHOT.jar

    You should see following logs in the end of console in case of successful starting of tutorial services -

    2016-10-21 09:41:23.992  INFO [tutorial-services,,,] 28752 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
    2016-10-21 09:41:24.119  INFO [tutorial-services,,,] 28752 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8888 (http)
    2016-10-21 09:41:24.124  INFO [tutorial-services,,,] 28752 --- [           main] c.a.m.t.TutorialServicesApplication      : Started TutorialServicesApplication in 13.279 seconds (JVM running for 14.026)

     

    Testing Services


    Since our services have started successfully, it's time to test these services. However, we will just be calling tutorial services as it calls author services internally.

    In order to test it, open your favourite browser or REST client and make a call to following URL.

    http://localhost:8888/tutorial/1

    You should get following response -

    {"id":1,"name":"Tutorial1","description":"Tutorial 1 Description","authorId":1,"authorName":"Amit Kumar"}

    Note: Since ZipKin only traces 1/10th of requests, you should make above request more than 10 times to be able to see it on ZipKin UI. You can change this ratio by using following property:

    spring.sleuth.sampler.percentage

     

    Visualizing Service Traces and Dependencies


    Finally, in order to visualise traces, open ZipKin UI dashboard by accesing URL - http://localhost:9411

    Choose service tutorial-services from services dropdown menu as shown below:

    ZipKin Service Selection

    Click on Find Traces button to search for traces and you will see traces as below:

    ZipKin Find Traces

    Click on trace to see the complete details as shown below - 

    ZipKin Trace Details View

    Above trace clearly states the time taken (77 microseconds) by author-services call as tutorial-services method calls it internally.

    Apart from this, you can click on Dependencies menu to see how your services are related. Below diagram shows that tutorial-services calls author-services internally.

    ZipKin Dependencies View

     

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Posted By: Amit Kumar
    Published on: 6th October 2016

    Comment Form is loading comments...