Distributed traceability with Spring Cloud: Sleuth and Zipkin

时间:2023-03-09 06:17:36
Distributed traceability with Spring Cloud: Sleuth and Zipkin

I. Sleuth

0. Concept

  • Trace
      A set of spans that form a call tree structure, forms the trace of the request.
  • Span
      It is the basic unit of work, for example a call to a service. They are identified with a span ID and a trace ID to which span is owned. They have start and end, and with it you get track the response time between requests.
  • Tag
      Key/value pair that identifies certain information in the span. It doesn't contain timestamps, it just identifies.

Annotation: Used to record the existence of an event in time. With Brave instrumentation, we no longer need to set special events for Zipkin to understand who the client and server are, where the request started, and where it ended. For learning purposes, however, we mark these events to highlight what kind of an action took place.

  • cs: Client Sent. The client has made a request. This annotation indicates the start of the span.
  • sr: Server Received: The server side got the request and started processing it. Subtracting the cs timestamp from this timestamp reveals the network latency.
  • ss: Server Sent. Annotated upon completion of request processing (when the response got sent back to the client). Subtracting the sr timestamp from this timestamp reveals the time needed by the server side to process the request.
  • cr: Client Received. Signifies the end of the span. The client has successfully received the response from the server side. Subtracting the cs timestamp from this timestamp reveals the whole time needed by the client to receive the response from the server.

Distributed traceability with Spring Cloud: Sleuth and Zipkin

1. pom

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

2. config

1) sampler

---
spring:
  sleuth:
    sampler:
      probability: 1.0

II. zipkin

0. way

1) http

Distributed traceability with Spring Cloud: Sleuth and Zipkin

2) Messaging Brokers

Distributed traceability with Spring Cloud: Sleuth and Zipkin

0. zipkin client

1) pom

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

2) config

(1) base url

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

(2) sender

A. RabbitMQ
---
spring:
  zipkin:
    sender:
      type: RABBIT
B. Kafka
---
spring:
  zipkin:
    sender:
      type: KAFKA

C. Web

default

1. zipkin server

upgrade to Spring Boot 2.0 NoClassDefFoundError UndertowEmbeddedServletContainerFactory

1) down

https://mvnrepository.com/artifact/io.zipkin.java/zipkin-server

v2.11.13

2) run

es2.4.x安装在WSL中。详细 see 《Elasticsearch 2.4 安装

(1) for Rabbit

version: RabbitMQ3.7.13(erlang 21.3)

java -jar zipkin-server--exec.jar --RABBIT_URI=amqp://admin:admin@192.168.42.124:5672/sleuth --STORAGE_TYPE=elasticsearch --ES_HOSTS=http://localhost:9200 --ES_HTTP_LOGGING=BASIC

(2) for kafka

version: Kafka1.0.2(Scale 2.11)

java -jar zipkin-server--exec.jar  --KAFKA_BOOTSTRAP_SERVERS=,, --STORAGE_TYPE=elasticsearch --ES_HOSTS=http://localhost:9200 --ES_HTTP_LOGGING=BASIC

III. MQ

1. RabbitMQ

0) pom

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

1) config

---
spring:
  rabbitmq:
    addresses: 192.168.42.124
    port: 5672
    username: admin
    password: admin
    virtual-host: sleuth

2) virtual hosts

切换到Admin选项卡,点击右侧的virtual hosts

Distributed traceability with Spring Cloud: Sleuth and Zipkin

2. Kafka

0) pom

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>

1) config

spring:
  kafka:
    bootstrap-servers:
      - 192.168.42.186:9092
      - 192.168.42.187:9092
      - 192.168.42.188:9092

IV. Effect Diagram

0. gateway

http://localhost:8311/user/listPage

Distributed traceability with Spring Cloud: Sleuth and Zipkin

1. zipkin server

http://localhost:9411

Distributed traceability with Spring Cloud: Sleuth and Zipkin

2. RabbitMQ

连接了4个服务:zipkin server

gateway->user-service->userDetails

http://192.168.42.124:15672/#/connections

Distributed traceability with Spring Cloud: Sleuth and Zipkin

3. ES2.4.x

http://localhost:9200/_plugin/elasticsearch-head

Distributed traceability with Spring Cloud: Sleuth and Zipkin

Reference:

1. Trazabilidad Distribuida con Spring Cloud: Sleuth y Zipkin