Spring Cloud Alibaba - Zipkin link tracking visualization (introduction / simple use / persistence)

Backtracking

In the last blog post Spring Cloud Alibaba - Sleuth link tracking technology , describes how to use Sleuth to realize link tracking, but problems also follow. We can't analyze it through logs, so we need to integrate another component ZipKin for visual query analysis. Please refer to the previous blog post for the front-end environment.

ZipKin introduction

Zipkin is an open source project of Twitter. It is implemented based on Google Dapper. It is committed to collecting service timing data to solve the delay problem in the micro service architecture, including data collection, storage, search and presentation. ps (data is the log information of the previous blog post).

We can use it to collect the trace data of the request link on each server, and use the REST API interface it provides to help us query the trace data to implement the monitoring program for the distributed system, so as to timely find the problem of increasing delay in the system and find the root cause of the system performance bottleneck.

In addition to the development oriented API interface, it also provides convenient UI components to help us intuitively search tracking information and analyze request link details. For example, you can query the processing time of each user request within a certain period of time. ps (this blog group will talk about the use of UI interface).

Zipkin provides pluggable data storage methods: in memory, MySql, Cassandra and Elasticsearch. However, by default, the service is stored in memory. In this way, once the service is suspended, it will disappear. Therefore, persistence is still required.

The figure above shows the infrastructure of Zipkin, which is mainly composed of four core components:

  • Collector: a collector component, which is mainly used to process tracking information sent from external systems and convert these information into Span format processed by Zipkin internally to support subsequent storage, analysis, display and other functions.
  • Storage: storage component, which is mainly used to process the tracking information received by the collector. By default, these information will be stored in memory. We can also modify this storage policy to store the tracking information in the database by using other storage components.
  • RESTful API: API component, which is mainly used to provide external access interfaces. For example, show the tracking information to the client, or access the external system to realize monitoring.
  • Web UI: UI component, the upper application based on API component. Through UI components, users can easily and intuitively query and analyze tracking information.

Zipkin is divided into two ends. One is Zipkin server and the other is Zipkin client. The client is the application of microservices. The client will configure the URL address of the server. Once a call between services occurs, it will be monitored by Sleuth's listener configured in the micro service, and generate the corresponding Trace and Span information to send to the server.

ZipKin use

ZipKin server installation

First, we need to download the jar package of the server. Click https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/ , select the latest version


On the download path page, open cmd

Enter the startup command and wait for startup. The command is as follows:

java -jar zipkin-server-2.12.9-exec.jar

Wait for startup.

Start complete access http://localhost:9411.

ZipKin client integration

In the gateway, producers and consumers add the following dependencies:

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

Add the following configuration:

spring:
  zipkin:
    base-url: http://127.0.0.1:9411/ \zipkin server request address
    discoveryClientEnabled: false #Let nacos treat it as a URL instead of a service name
  sleuth:
    sampler:
      probability: 1.0 #Percentage of samples

probability: sampling percentage. Since this place is used for testing, 1 is filled in, which means 100%. If it is a real environment, you can set the percentage yourself.

Click the request path once to refresh the server. We mind seeing the request.

Click in to analyze the time and so on.

There are various parameters. You can locate the possible conditions of the service according to the length of time.

After code adjustment, you can observe the request again:


After comparison, we can find that the modified code makes the program call more fluent.

ZipKin data persistence

As explained above, ZipKin's data is saved in memory by default, which is very unfriendly to our program, so we need to persist the data.

ZipKin data persistence to MySql

First of all, we need to create the tables specified by ZipKin, but we don't need to actively write them. We can get the execution statements through the official documents:

https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql-v1/src/main/resources/mysql.sql

To prevent someone from being unable to open this web page, I still print out the execution statement:

CREATE TABLE IF NOT EXISTS zipkin_spans (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL,
  `id` BIGINT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `remote_service_name` VARCHAR(255),
  `parent_id` BIGINT,
  `debug` BIT(1),
  `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
  PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';

CREATE TABLE IF NOT EXISTS zipkin_annotations (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';

CREATE TABLE IF NOT EXISTS zipkin_dependencies (
  `day` DATE NOT NULL,
  `parent` VARCHAR(255) NOT NULL,
  `child` VARCHAR(255) NOT NULL,
  `call_count` BIGINT,
  `error_count` BIGINT,
  PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

Then when you start Zipkin, you can add the command to link the database.

java -jar zipkin-server-2.12.9-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=root

ZipKin data persistence to Elasticsearch

First, you need to download Elasticsearch

https://www.elastic.co/cn/downloads/elasticsearch

Click download:

After extracting the file, double-click

Start. Just like mysql, add Elasticsearch connection information when starting.

java -jar zipkin-server-2.12.9-exec.jar --STORAGE_TYPE=elasticsearch --ESHOST=localhost:9200

Tags: Java Spring Cloud zipkin

Posted by guttyguppy on Tue, 31 May 2022 06:13:18 +0530