SpringCloud Alibaba learning (13)-Sleuth and ZipKin realize link tracking

The background of distributed link tracking

  • Single springboot project The
    previous business requirement was a single logic.
  • SpringCloud project composed of multiple microservices
    A business involves many microservices .

When doing distributed development in Springcloud, high-availability processing is usually done in a microservice with a large amount of processing.
But when an error message appears, locating the point of error can be a very troublesome problem.

A certain microservice is deployed in a cluster, and one of them has a problem, which requires a lot of labor and time to troubleshoot the root cause of the problem.

Insert picture description here
This is just one of the common problems. The problems of large-scale microservice architecture systems given on the Internet are as follows:

  • How to connect the call chain to quickly locate the problem.
  • How to figure out the dependencies between microservices.
  • How to perform performance analysis of various service interfaces.
  • How to track the processing sequence of business flows.

Sleuth process overview

Check the official Sleuth document, and you will see the following flowchart: The
Insert picture description here
first time I saw this flowchart, it was very daunting. There are a lot of parameter information above, but what do they mean?

Trance Id (request for unique identification):

1. The process in the figure always shows Trance id = x.
2. It means that every request will have a unique identifier.
3. Request processing flow:
Client-->service 1 -->service 2 -->service 3 -->service 4 --> service 2 --> service 1 -->client.
4.Trance id remains unchanged when a request is sent to each microservice, and even the final response.
5. Trance Id is the unique id of an entire link.

Span Id (one span):
Insert picture description here

Span is the unique id of a span.
Span Id = A is the span id of Service 1;
Span Id = B is the span id of Service 2;

Nomenclature of flow chart

the term scientific name description
span Basic work unit. (span) The span is uniquely identified by a 64-bit id. In addition to ID, span also contains other data, such as description, timestamp event, key-value pair annotations (tags), spanID, span parent ID, etc. When the span is started and stopped, time information is recorded. The initial span is called "rootspan", and the id of the span is equal to the ID of the trace.
trace track The 64-bit ID uniquely identifies, each request thread number is different, and each included span shares this trace id
CS Client sent The client initiates a request, and the annotation describes the beginning of the span.
SR server Received The server gets the request and prepares to process it. If you subtract the CS timestamp from the SR, you can get the network delay.
SS server sent The annotation indicates that the request processing is complete (when the response is sent back to the client). If you subtract the SR timestamp from the SS, you can get the time required for the server to process the request.
CR Client Received The identifier of the end of the span. The client successfully received the response from the server. If CR is subtracted from CS timestamp, we can get the time from the request sent by the client to the response from the server.
annotation Label Annotation is used to record the existence of an event, and the core annotation is used to define the beginning and end of the request. (CS, SR, SS, CR are collectively referred to as annotation)

Use Zipkin to try to display sleuth data

nacos-product-sleuth sub-service creation

In the dependency, you need to use the information of the project process, so you need to introduce org.springframework.cloudthe spring-cloud-starter-sleuthdependency, and if you need to use Zipkin for view display, you also need to introduce additional spring-cloud-starter-zipkindependency information.

The detailed pom.xml is as follows:

<!-- SpringCloud ailibaba nacos -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--每个服务都需要引用该依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!-- 引入 Sleuth依赖(引入了spring-cloud-starter-zipkin,这个依赖可以不需要引入) -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

Increase the configuration file, mainly used for 注册至nacos注册中心, sleuth信息推送至ui显示etc.

## 该服务的端口信息
server:
  port: 11000

## 服务别名和nacos服务注册发现地址配置
spring:
  application:
    name: nacos-product-sleuth
  zipkin:
    base-url: http://localhost:9411/  # 指向地址
    discovery-client-enabled: false # 让cloud将此连接当作是url,而不是服务名
  sleuth:
    sampler:
      probability: 1.0  #指定需要采样的请求的百分比,默认为 0.1f,即 10%
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

## 监控相关
management.endpoints.web.exposure.include: '*'

Start the preparation of the class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

Create external access interface

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

@RestController
public class TestController {
    
    
    @Value("${server.port}")
    private String port;

    @RequestMapping("/product/getProduct/{id}")
    public String getOrder(@PathVariable("id") String id) {
    
    
//        try {
    
    
//            Thread.sleep(800);
//        } catch (InterruptedException e) {
    
    
//            e.printStackTrace();
//        }
        return "this is product services,port:"+port+"\t id="+id;
    }
}

nacos-zipkin-client consumer sub-service

The dependency import is the same as the configuration file. If you want to display more specific sleuth information, the following information can be configured in the configuration file

# 打印更多的日志信息
logging:
  level:
    org.springframework.cloud.sleuth: debug

Create an interface as nacos-product-sleuththe interface information provided in the internal call . Since it is used here openFeign, openfeign needs to be added to the dependencies:

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

Write a control class and create an external exposure interface:

import cn.linkpower.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    
    

    @Autowired
    private ProductFeignService productFeignService;

    @RequestMapping("/test")
    public  String test(){
    
    
        return "this is zipkin client test  "+productFeignService.getOrder("1");
    }
}

Write feign service interface call

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "nacos-product-sleuth")
public interface ProductFeignService {
    
    
    @RequestMapping("/product/getProduct/{id}")
    public String getOrder(@PathVariable("id") String id);
}

Of course, fallback needs to be done here to construct the degraded return data information.

Secondly, there is the most important point, and then you need to add a @EnableFeignClientscomment on the startup class and turn on feign.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient //注册至注册中心中,调入了nacos依赖,可以无需此依赖信息
@EnableFeignClients //启用激活feign
public class ZipkinClient10000 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ZipkinClient10000.class,args);
    }
}

Download and start Zipkin

In the Spring Cloudmiddle, the initial building of Zipkin ui display, developers need to create a Zipkin-serversub-service, doing push receiver sleuth sub-service data. But in spring cloud alibabaChinese, no additional configuration is required, just download and then java -jar xxxstart, just like sentinel.

The download link is as follows:

Zipkin version download address

After downloading, use java -jar .\zipkin-server-2.12.9-exec.jarstart to use this version 2.12.9.

Start the test

Respectively start nacos, zipkin-server, nacos-product-sleuth, nacos-zipkin-client.

Access zipkin-serverservice interface link

http://localhost:9411/zipkin/

Insert picture description here
Request sub-service link:

http://localhost:10000/test

Insert picture description here
Return to the ui interface of zipkin and refresh the view.
Insert picture description here

If a service is down (closed nacos-product-sleuth), the link information at this time is:
Insert picture description here
Insert picture description here
click here to view
Insert picture description here

Reference article

SpringCloud alibaba new version tutorial [sleuth-zipkin link]-13

Spring Cloud Alibaba study notes (23)-Call chain monitoring tool Spring Cloud Sleuth + Zipkin

Code download

sleuth-cloud-admin-10000
sleuth-cloud-admin-product

github code download address

Guess you like

Origin blog.csdn.net/qq_38322527/article/details/114405498