Spring Cloud Sleuth

An overview,

1, problem,

In micro service framework, a request by the client in the back-end system will go through a number of different service node calls to collaborate last request as a result, every paragraph request form a complex distributed service call link, link to any part of the high latency or mistakes can cause the failure of the entire request finally.

2. What is it

Liverpoolfc.tv: github.com/spring-clou…

Spring Cloud Sleuth provides a complete service tracking solution in distributed systems that is compatible with Zipkin

3, solve

Step 2 Set up link monitoring

1, zipkin

(1) to download

Address: repo1.maven.org/maven2/io/z…

Zipkin – server – 2.12.9 – exec. Jar

(2) run the jar

Java jar zipkin – server – 2.12.9 – exec. Jar

(3) Run the console

http://localhost:9411/zipkin/

A link is uniquely identified by a Trace Id and a Span identifies the requested information. The Span is associated by a parent Id.

Trace: A Span collection similar to the tree structure. It represents a call link and has a unique identifier.

Span: indicates the source of the call link. Popularly, span is a request information.

2. Service providers

(1)cloud-provider-payment8001

(2)POM

<! - contains the sleuth + zipkin - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <! --eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>Copy the code

(3)YML

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      # The sampling rate is between 0 and 1, where 1 indicates that all samples are collected
     probability: 1
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # Current data source operation type
    driver-class-name: org.gjt.mm.mysql.Driver              Mysql driver package
    url: jdbc:mysql://localhost:3306/db2019? useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

eureka:
  client:
    # specifies whether to register yourself with EurekaServer. Default is true.
    register-with-eureka: true
    Whether to fetch existing registration information from EurekaServer. Default to true. A single node does not matter. Clusters must be set to true to use load balancing with the ribbon
    fetchRegistry: true
    service-url:
      # defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka # cluster
      defaultZone: http://localhost:7001/eureka  Stand-alone version #
  instance:
    instance-id: payment8001
    Access path can display IP address
    prefer-ip-address: true
    #Eureka the interval between heartbeat messages sent by the client to the server, in seconds (default is 30 seconds)
    lease-renewal-interval-in-seconds: 1
    #Eureka Specifies the maximum amount of time a server can wait after receiving the last heartbeat, in seconds (default: 90 seconds)
    lease-expiration-duration-in-seconds: 2


mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities    # Package where all Entity alias classes reside
Copy the code

(4) Business class PaymentController

@GetMapping("/payment/zipkin")
public String paymentZipkin(a){
    return "Hi, I'm PaymentZipkin server fall back, welcome to atguigu, O(∩_∩)O haha ~";
}
Copy the code

3. Service Consumer (caller)

(1)cloud-consumer-order80

(2)POM

Same as above

(3)YML

server:
  port: 80

spring:
    application:
        name: cloud-order-service
    zipkin:
      base-url: http://localhost:9411
    sleuth:
      sampler:
        probability: 1

eureka:
  client:
    # specifies whether to register yourself with EurekaServer. Default is true.
    register-with-eureka: true
    Whether to fetch existing registration information from EurekaServer. Default to true. A single node does not matter. Clusters must be set to true to use load balancing with the ribbon
    fetchRegistry: true
    service-url:
      #defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
      defaultZone: http://eureka7001.com:7001/eureka

Copy the code

(4) Business class OrderController

// ====================> zipkin+sleuth
@GetMapping("/consumer/payment/zipkin")
public String paymentZipkin(a){
    String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
    return result;
}
Copy the code

4. Start eureka7001/8001/80 in turn

80 Call 8001 several times to test

5. Open a browser

http://localhost:9411

Principle: