SpringCloud Sleuth distributed request link tracking

SpringCloud Sleuth concept

1. Why use SpringCloud Sleuth? What are the current problems?

In the microservice framework, the request initiated by a client will be called by multiple different service nodes in the back-end system to produce the final request result. Each front-end request will form a complex distributed service call link. Any high delay or error in any link will cause the final failure of the whole request. Spring Cloud Sleuth implements a distributed tracking solution for Spring Cloud, which is compatible with Zipkin and combined with Zipkin for link tracking. The link tracking function of Spring Cloud Sleuth service can help us quickly find the root cause of errors and monitor and analyze the performance on each request link.

2. What is SpringCloud Sleuth?

(1) SpringCloud Sleuth is a set of solutions that provide complete service tracking. It provides tracking solutions in distributed systems and supports zipkin.
(2) Zipkin: Zipkin is a distributed tracking system. It helps to collect the timing data needed to solve the latency problem in the service architecture. The function includes the collection and search of this data. Zipkin was originally built to store data on Cassandra because Cassandra is scalable, flexible, and widely used on Twitter. In addition to Cassandra, ElasticSearch and MySQL are also supported. If there is a trace ID in the log file, you can jump directly to the trace ID. You can also query based on attributes, such as service, operation name, label and duration, percentage of time spent in the service, and whether the operation failed.

3. Solve the problem: monitor, track and record each service node through zipkin.

Build distributed request link

1, Zipkin download and installation concept
1. Download zipkin: https://repo1.maven.org/maven2/io/zipkin/zipkin-server/2.14.1/zipkin-server-2.14.1-exec.jar
It is worth noting that spring cloud does not need to build Zipkin Server by itself since version F. it only needs to call the jar package.

2. Run the downloaded jar package: java -jar zipkin-server-2.14.1-exec.jar.

3. Access: http://127.0.0.1:9411

4. Complete call link diagram: it represents a request link, and a link is uniquely identified by Trace Id. Span refers to the request information initiated, and each span is associated through parent id.

(1) Complete call link diagram (schematic diagram)

Trace: a Span collection similar to a tree structure, which represents the unique identification of the existence of a call link.
Span: refers to the source of the calling link, which can be understood as each request or response.

(2) After simplification, see the following figure:

(3) The dependencies of the whole link are as follows:

2, Service provider
1. Modify cloud-cloud-provider-ek-payment8001 (cloud-provider-ek-payment8002) load balancing service provider.
2. Import pom file

<!--Contains sleuth+zipkin-->
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

3. Modify yml configuration file

server:
  port: 8001


spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db2021?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

  # Sleuth zipkin configuration
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      # The sample rate value is between 0 and 1, and 1 means all collection
      probability: 1

eureka:
  client:
    #Indicates that the receiver registers itself with EurekaServer. The default value is true
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer is true by default. It doesn't matter for a single node. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
		#defaultZone: http://localhost:7001/eureka # standalone
        defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   #Cluster version
  instance:
    instance-id: payment8001
    #The access path can display the IP address
    prefer-ip-address: true
    #The time interval between the eureka client sending heartbeat to the server, in seconds (30 seconds by default)
    lease-renewal-interval-in-seconds: 1
    #The maximum waiting time of eureka server after receiving the last heartbeat, in seconds (the default is 90 seconds). If it times out, the service will be deleted
    lease-expiration-duration-in-seconds: 2

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: springcloud.atguigu.springcloud.entities #Package of all entity aliases

4,controller

@RestController
@Slf4j
public class PaymentController {
    //SpringCloud Sleuth and Zipkin request link tracking test
    @GetMapping(value = "/payment/zipkin")
    public String paymentZipkin() {
        return "Services:" + serverPort + "--> Zipkin Request link tracking test";
    }
}

3, Service consumers
1. Modify cloud-consumer-ek-order80 service consumer
2. Introducing pom dependency

<!--Contains sleuth+zipkin-->
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

3. Modify yml configuration file

server:
  port: 8002


spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db2021?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

  # Sleuth zipkin configuration
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      # The sample rate value is between 0 and 1, and 1 means all collection
      probability: 1

eureka:
  client:
    #Indicates that the receiver registers itself with EurekaServer. The default value is true
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer is true by default. It doesn't matter for a single node. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      #      defaultZone: http://localhost:7001/eureka # standalone
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   #Cluster version
  instance:
    instance-id: payment8002
    #The access path can display the IP address
    prefer-ip-address: true
    #The time interval between the eureka client sending heartbeat to the server, in seconds (30 seconds by default)
    lease-renewal-interval-in-seconds: 1
    #The maximum waiting time of eureka server after receiving the last heartbeat, in seconds (the default is 90 seconds). If it times out, the service will be deleted
    lease-expiration-duration-in-seconds: 2

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: springcloud.atguigu.springcloud.entities #Package of all entity aliases

4,controller

@RestController
@Slf4j
public class OrderController {

    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Resource
    private RestTemplate restTemplate;
    //SpringCloud Sleuth and Zipkin request link tracking test
    @GetMapping(value = "/consumer/payment/zipkin")
    public String paymentZipkin() {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/zipkin", String.class);
    }
}

4, Test verification
1. Address field input: http://localhost/consumer/payment/zipkin
2. Refresh Zipkin monitoring: http://localhost:9411
(1) Find: you can see the number of microservices, the depth of request calls, and the total number of spans.
(1) Dependency: cloud order server depends on cloud payment service

Keywords: zipkin

Added by neo926 on Thu, 04 Nov 2021 03:38:26 +0200