Spring Cloud and Spring Boot. Part 2: using Zipkin Server for distributed tracing


From the last article (Habré translation, original in English) you learned how to use Eureka Server to discover and register services. In this article, we will introduce you to Distributed Tracing.

What is distributed tracing

Distributed tracing is used to find and fix errors in the operation of microservices. It helps track the progress of a request through a number of microservices. And besides that, it can be used for performance analysis.

With its help, it is easy to determine which microservice has crashed or where performance problems have arisen.

In this article, we will take a look at using Zipkin Server for distributed tracing. To do this, we need the following 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>

Launching Zipkin Server

Create a maven application and give it some name (like zipkin-server). To create a Spring project, you can use https://start.spring.io

Your pom.xml should look like this:

<?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>

Now open ZipkinServerApplication.java and add annotation @EnableZipkinServeras shown below.

package com.example.zikin.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin2.server.internal.EnableZipkinServer;

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

To file application.propertieslocated in src/main/resources, add the following parameters.

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

Run the project as Java application and go to http: // localhost: 9411 / zipkin

There are no traces here yet, as the client applications have not been registered yet.

Registering a client application with Zipkin Server

Registering Eureka Server with Zipkin Server

In the previous article (Habré translation, original in English.) we have seen how to start Eureka Server. It can now be registered with Zipkin Server by adding one property to the file application.properties

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

Parameter spring.zipkin.base-url defines the address where the Zipkin Server is located.

You also need to add another dependency to pom.xml Eureka Server applications.

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

After Eureka Server starts up, you will see traces in Zipkin Server, and eureka-server will appear in the Service Name field in Zipkin Server UI.

Registering a Spring Boot client application with Zipkin Server

To register any Spring Boot based client application, you need to add one property to the file application.properties

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

And one dependency in the pom.xml of your client application.

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

After launching the client application, you should be able to see its trace in Zipkin.

View trace details in Zipkin Server

You can view the trace details by clicking on any of the entries shown in the image above.

Now you know how to use Zipkin Server for distributed tracing.


Translation of the article was prepared on the eve of the start of the course “Spring Framework Developer”… We invite everyone to register for a free demo lesson on the topic Introducing the Cloud, Creating a Cluster in Mongo DB Atlas

During the lesson, participants, together with an expert, will talk about the types of clouds and set up a free Mongo DB cluster for their projects.

Similar Posts

Leave a Reply

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