Distributed traceability with Spring Cloud: Sleuth and Zipkin

zipkin 2.12/2.14+

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. ms info 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.

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

  • http http way
  • Messaging Brokers Messaging Brokers way

0. zipkin client

1) pom

spring boot 2.4.x(2020.0) before:

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

spring boot 2.4.x(2020.0) later:

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

2) config

base url

old:

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

new:

---
spring:
  zipkin:
    baseUrl: http://localhost:9411

sender

RabbitMQ
---
spring:
  zipkin:
    sender:
      type: RABBIT
Kafka
---
spring:
  zipkin:
    sender:
      type: KAFKA
Web

default

1. zipkin server

upgrade to Spring Boot 2.0 NoClassDefFoundError UndertowEmbeddedServletContainerFactory

1) down

Note: ES2.4.x最高只能匹配到v2.12.9

About v2.14+:

The Elasticsearch component uses Elasticsearch 5+ features, but is tested against Elasticsearch 6-7.x

Simply put, only supports v6-7.x.

2) run

es2.4.x/7.x安装在WSL中。

详细 seeElasticsearch 2.4 安装

详细 seeElasticsearch 7.x 安装

es2.4.x

  • for Rabbit
java -jar zipkin-server-2.12.9-exec.jar --RABBIT_URI=amqp://admin:admin@192.168.0.19:5672/sleuth --STORAGE_TYPE=elasticsearch --ES_HOSTS=192.168.0.116:9200 --ES_HTTP_LOGGING=BASIC
  • for kafka
java -jar zipkin-server-2.12.9-exec.jar --KAFKA_BOOTSTRAP_SERVERS=192.168.188.107:9092,192.168.188.108:9092,192.168.188.109:9092 --STORAGE_TYPE=elasticsearch --ES_HOSTS=http://localhost:9200 --ES_HTTP_LOGGING=BASIC

es7.x

  • for Rabbit
java -jar zipkin-server-2.23.16-exec.jar --RABBIT_URI=amqp://admin:admin@192.168.3.50:5672/sleuth --STORAGE_TYPE=elasticsearch --ES_HOSTS=192.168.3.128:9200 --ES_HTTP_LOGGING=BASIC
  • for kafka
java -jar zipkin-server-2.23.16-exec.jar --KAFKA_BOOTSTRAP_SERVERS=192.168.3.107:9092,192.168.3.108:9092,192.168.3.109:9092 --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-binder-rabbit</artifactId>
</dependency>

1) config

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

2) virtual hosts

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

Add a new virtual host:

name: sleuth
``

## 2. Kafka
### 0) pom
```xml
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>

1) config

spring:
  kafka:
    bootstrap-servers:
      - 192.168.3.127:9092
      - 192.168.3.128:9092
      - 192.168.3.129:9092

IV. Effect Diagram

0. gateway

http://localhost:8311/user/listPage browser info1

1. zipkin server

http://localhost:9411 browser info2

2. RabbitMQ

连接了4个服务:zipkin server

gateway->user-service->userDetails

http://192.168.3.19:15672/#/connections browser info3

3. ES2.4.x

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

Reference