Spring Cloud (ten): Zipkin service tracking

I. Overview

Why do we need service tracking? There are many services calling each other in a distributed system, and the calling relationship is intricate. If there is a problem at this time, when we troubleshoot or optimize the architecture, the workload will be relatively large. Big, at this time, we need to be able to accurately track each network request and understand the entire operation process, such as which microservices the network request passes through, whether there is delay in each microservice, etc., so that we can troubleshoot problems conveniently. At this time we can use Spring Cloud Zipkin to achieve.

Spring Cloud Zipkin is a component that can collect and track request data in a distributed system, allowing developers to more intuitively monitor the time spent on requests in various microservices. Zipkin includes Zipkin Server and Zipkin Client. The Zipkin Server server is used to collect tracking data between microservices, and the data generation and display are completed through the Zipkin Client client. Next, let's enter the actual combat exercise!

Two, actual combat! Quickly build Zipkin Server and Zipkin client

  1. Create subproject zipkinServer, pom.xml configuration is as follows:
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
    	<groupId>io.zipkin.java</groupId>
    	<artifactId>zipkin-server</artifactId>
    	<version>2.9.4</version>
    </dependency>
    
    <dependency>
    	<groupId>io.zipkin.java</groupId>
    	<artifactId>zipkin-autoconfigure-ui</artifactId>
    	<version>2.9.4</version>
    </dependency>
  1. Create a configuration file, the code is as follows:
server:
  port: 9090
  1. Create a startup class with the following code:
package com.zing;

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

import zipkin.server.internal.EnableZipkinServer;

@SpringBootApplication
@EnableZipkinServer
public class ZipkinApplication {
    
    
	public static void main(String[] args) throws Exception {
    
    
		SpringApplication.run(ZipkinApplication.class, args);
	}
}
注解说明
  • @EnableZipkinServer: Declare to start Zipkin Server
  1. Create a zipkin client. Create a sub-project, and configure the pom.xml file as follows:
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-zipkin</artifactId>
    	<version>2.0.2.RELEASE</version>
    </dependency>
  1. Create a configuration file application.yml, the configuration is as follows:
server:
  port: 8090
spring:
  application:
    name: zipkinclient
  sleuth:
    web:
      client:
        enabled: true
    sampler:
      probability: 1.0
  zipkin:
    base-url: http://localhost:9090/
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
属性说明
  • spring.sleuth.web.client.enabled: set to enable request tracking

  • spring.sleuth.sampler.probability: Set the sampling ratio, the default is 1.0

  • spring.zipkin.base-url: Zipkin Server address

  1. Create a startup class with the following code:
package com.zing;

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

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

}
  1. Create a controller for testing, the code is as follows:
package com.zing.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/zipkin")
public class ZipkinHandler {
    
    
	
	@Value("${server.port}")
	private String port;
	
	@GetMapping("/index")
	public String index() {
    
    
		return this.port;
	}
	
}
  1. Start the registration center, zipkinserver, and zipkinclient three services in turn, and visit http://localhost:9090/zipkin/ to display the following visual interface:
    insert image description here
  2. Let's visit the interface http://localhost:8090/zipkin/index to be tracked and the following interface is displayed:
    insert image description here
  3. Visit the zipkin service tracking visualization interface address http://localhost:9090/zipkin/ again, click the Find Traces button to enter the following page
    insert image description here
  4. Click the data in the list above to enter the specific display list, as shown in the figure below:
    insert image description here
  5. Click the data in the list to view the detailed information of the tracked service, as shown in the figure below:
    insert image description here

3. Summary

In this article, we use zipkin to monitor the requests between our microservices. You can see some detailed information, such as the time spent, and the path method of the specific request. The above is the use of Zipkin.

One development engineer is also in the continuous learning stage, and the usual small experiences are shared from time to time. I hope that those who read the text I wrote can avoid detours and wish you success in work and study.
Bloggers have limited experience, if there are any shortcomings, welcome to communicate and improve together~ I hope to make progress together with you who are also in CSDN.

Author | Sweet Little Sweet Potato
Produced | Little Sweet Potato

Guess you like

Origin blog.csdn.net/qq_36836370/article/details/130901465