SpringCloud microservice Xiaobai can also take (Hoxton.SR8) (8) Sleuth|Service Link Tracking

Simple to get started, copy directly, you can build microservices (Hoxton.SR8) 2020.8.28 is released, the articles built by SpringCloud are being sorted out, don’t miss out on dry goods

Summary

Spring Cloud Sleuth is a tool for tracking calls between services in a distributed system. It can visually show the call process of a request. As our system becomes larger and larger, the calling relationship between various services becomes more and more complicated. When the client initiates a request, after the request passes through multiple services, the result is finally returned. Each service passed may be delayed or error, which may cause the request to fail. At this time, we need to request a link tracking tool to help us sort out the requested service link and solve the problem.

Sleuth has everything you need to get started. This includes where to report tracking data (span), how many tracks to keep (track), whether remote fields (bags) are sent, and which libraries to track. Sleuth configured these applications to record the time of these requests to the distributed tracking system Zipkin. Track UI visualization delays, such as time in one service and time waiting for other services.

1. Add link tracking support

1.1 Participation in services: user-service, feign-service, ribbon-service

We add dependencies to the above three services

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

1.2 yml add zipkin-server access address

spring:
  application:
    name: feign-service
  zipkin:
    base-url: http://localhost:9411
    sender:
      type: web
    #设置Sleuth的抽样收集概率,1=100%,因为我们目前是测试环境所以用1
    sleuth:
      sampler:
        probability: 1

2. Build a link tracking service center (zipkin-server)

zipkin documentation

2.1 How to build

  1. docker
  2. java -jar
  3. github source build
  4. Add dependency to build springboot by yourself (not officially recommended)

We choose the java method to build, and the docker build method is also very fast, but because there is no docker installed on the Windows computer, let's use the java -jar method to show the function first. The latest version I currently
download is 2.21.7 download link:  https://repo1.maven.org/maven2/io/zipkin/zipkin-server/2.21.7/zipkin-server-2.21.7-exec.jar
or previous Go to the official to get the latest version

2.2 Start zipkin

2.2.1 Open cmd to execute the startup command

java -jar zipkin-server-2.21.7-exec.jar

 2.2.2 Successful startup

Visit the browser: http://127.0.0.1:9411

3. Service test call

Call user-service through feign-service or ribbon-service, and then refresh the service page of zipkin

 

4. zipkin data persistence

When we want to save the link information, the link data will not disappear when Zipkin restarts, which is convenient for later query or location problems.

4.1 mysql

When the amount of data is very large, such as tens of millions of pieces of data, it will cause query efficiency problems. It is not recommended by the government. It can be used for transition. Elasticsearch can be used later.

4.1.1 Download sql file

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

4.1.2 New database

4.1.3 Run sql file

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;

4.1.4 Restart Zipkin

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

4.1.5 Perform interface call test again

  

4.2 Elasticsearch

4.2.1 Download

Version link: https://www.elastic.co/cn/downloads/past-releases#elasticsearch

Download link: https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-9-2

After downloading, unzip it out D:\elasticsearch-7.9.2\bin,
enter the bin directory and double-click the program marked by the red circle

4.2.2 Start elasticsearch

.\elasticsearch.bat

Test start: http://localhost:9200/?pretty

4.2.3 Management page

No installation is done here, and more elk blog posts will be added later

4.2.4 zipkin restart

java -jar zipkin-server-2.21.7-exec.jar --STORAGE_TYPE=elasticsearch --ES_HOSTS=localhost:9200

5. More configuration

https://github.com/openzipkin/zipkin/tree/master/zipkin-server#elasticsearch-storage

 

Guess you like

Origin blog.csdn.net/itjavaee/article/details/109214961