Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Cloud and Spring Boot, Part 2: Implementing Zipkin Server For Distributed Tracing

DZone 's Guide to

Spring Cloud and Spring Boot, Part 2: Implementing Zipkin Server For Distributed Tracing

This second tutorial takes a look at how Zipkin server works with Eureka for distributed tracing.

· Cloud Zone ·
Free Resource

In my last article you have learn how to implement Eureka Server for service discovery and registration. In this article, you will learn one more important feature of microservices, Distributed Tracing.

What is Distributed Tracing?

Distributed Tracing is crucial for troubleshooting and understanding microservices. It is very useful when we need to track the request passing through multiple microservices. Distributed Tracing can be used to measure the performance of the microservices. 

It is easy to identify which microservice is failed or having performance issues whenever there are multiple service calls within a single request.

In this article, you will see how to implement Zipkin Server for Distributed Tracing.

To achieve this, you need to create Zipkin Server application and add these two dependencies in POM.xml.

<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-server</artifactId>
    <version>2.11.7</version>
</dependency>
<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-autoconfigure-ui</artifactId>
    <version>2.11.7</version>
</dependency>

Implementing Zipkin Server 

Create Spring Boot Application

You need to create a Maven application and name the application (i.e. zipkin-server) or you can use https://start.spring.io to create a Spring project.

POM.xml

You need to add this list of dependencies in the POM.xml of your project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.zikin.server</groupId>
<artifactId>zipkin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>zipkin-server</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-server</artifactId>
    <version>2.11.7</version>
</dependency>

<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-autoconfigure-ui</artifactId>
    <version>2.11.7</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

Spring Boot Starter Application

Now you need to open the Spring Boot application ZipkinServerApplication.java and add the annotation  @EnableZipkinServer  on the top of the class as shown below.

package com.example.zikin.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import zipkin.server.EnableZipkinServer;

@SpringBootApplication
@EnableZipkinServer
public class ZipkinServerApplication {

public static void main(String[] args) {
SpringApplication.run(ZipkinServerApplication.class, args);
}
}

Application Properties file

You need to add the below list of properties in application.properties located at src/main/resources of your application.

spring.application.name=zipkin-server
server.port=9411

Running Zipkin Server

Run the Zipkin server as a Java application and navigate to the url: http://localhost:9411/zipkin.

Image title

Currently, you will not see any traces as there is no client application registered with Zipkin Server.

Registering Client Application With Zipkin Server

Registering Eureka Server With Zipkin Server

In the last article, you have seen how to implement the Eureka Server. You can register Eureka Server to Zipkin server by adding one property in application.properties file located at src/main/resources.

spring.zipkin.base-url=http://localhost:9411/

spring.zipkin.base-url determines the address where Zipkin Server is running.

You need to add one dependency in POM.xml of Eureka Server application.

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

Once Eureka Server is up and running, you can find traces in Zipkin Server and even you can see eureka-server in Service Name of Zipkin Server UI.

Image title

Registering Spring Boot Client Application With Zipkin Server

For registering any Spring Boot-based client application, you simply need to add one property in application.properties file of your application located at src/main/resources.

spring.zipkin.base-url=http://localhost:9411/

You need to add one dependency in POM.xml of your client application.

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

Once your client application is up and running, you can see the traces in the Zipkin server.

Image title

Viewing Traces Details in Zipkin Server

You can view the traces details by clicking any of entry shown in above image.

Image title

Now you have learned how to implement Zipkin Server for Distributed Tracing.

Topics:
spring boot ,spring cloud ,zipkin ,distributed tracing ,cloud ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}