Microservice Combat 05 - Service Link Tracking

In the previous example, we have two microservices, namely order service and logistics service. As the business increases, more and more microservices will exist, and there will be more complex calling relationships between them .

This call relationship will become more and more difficult to identify just by observing the code, so it is necessary to track the server through the zipkin service link to identify it with pictures.

Zipkin is a distributed service tracking system that can help us collect performance data of distributed systems and track the flow of requests between different services. It helps us understand the state of interaction between different subsystems and analyze performance metrics by sending lightweight trace data between different services and storing them.

The core of Zipkin is to aggregate, store and display the tracking information of distributed transactions, and provide powerful tools to analyze these data and locate system problems. Zipkin mainly includes four components: Collector, Storage, API and UI, which are introduced below.

  1. Collector: Used to aggregate, process and store tracking data, and send the data to Storage for storage.

  2. Storage: Store the received tracking data in back-end storage (such as Mysql, Elasticsearch, etc.) for later query and analysis.

  3. API: used to store and query tracking data, and display query results through UI.

  4. UI: Displays the tracking data queried through the API, as well as related analysis tools.

Transformation order + logistics service

Add dependencies first:

zipkin-dependency

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

Both configuration files are added

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

sampling strategy

Zipkin supports controlling the amount of collected and stored tracking data by configuring sampling strategies, so as to avoid excessive system resource usage and reduce system availability. Zipkin provides three basic sampling strategies:

  1. AlwaysSample: Indicates that all request data is always sampled. No matter how frequently your system requests, it will have a great impact on performance and is not recommended for use in a production environment.

  2. PercentageBasedSampler: Indicates that sampling is based on a certain ratio. Among them, the rate is specified by the parameter "rate". For example, setting it to 0.5 means that only half of the requests will be kept and the other half will be dropped. This strategy is suitable for most production environments.

  3. ProbabilitySampler: Indicates whether to sample a request with a defined probability. The probability value is determined by the parameter "samplingProbability", which defaults to 0.001.

If the above sampling strategies cannot meet your needs, you can also implement one yourself. Zipkin provides an interface for you to expand, and you only need to implement the Sampler interface.

Configure the Sampler sampling strategy in the startup class: ALWAYS_SAMPLE means continuous sampling

@Bean
public Sampler defaultSampler() {
   return Sampler.ALWAYS_SAMPLE;
}  

The startup classes of both services must be added.

download zipkin

You can download the latest version of the Zipkin Server jar package from the Zipkin official website. The download address is: https://search.maven.org/artifact/io.zipkin.java/zipkin-server You can also find the dependencies of the corresponding version in the Maven central warehouse. The specific dependency information is as follows:

<dependency> 
  <groupId>io.zipkin.java</groupId> 
  <artifactId>zipkin-server</artifactId> 
  <version>2.23.2</version> 
</dependency> 

You can modify the version number in the above configuration to obtain different versions of the Zipkin Server jar package.

After downloading, use java -jarthe command to run. If the port is occupied, use the following method to release it. (The following method is only applicable to the situation I encountered)

Query the usage of port 9411

netstat -ano | findstr ":9411"

found to be occupied by 19500

  TCP    0.0.0.0:9411           0.0.0.0:0              LISTENING       19500
  TCP    [::]:9411              [::]:0                 LISTENING       19500

Check what process 19500 is

 tasklist | findstr "19500"

have to

javaw.exe   19500 Console   1    585,120 K

In the end, it was found that it was javaw. Just execute the command taskkill /im javaw.exe /f to kill the process. It is best to use taskkill /pid process number /f to kill the process by process number.

Restarted the jar and it worked.

verify

Start the registration center and two services in sequence, among which oms has a cluster with two ports.

Visit: http://localhost:9411/zipkin/dependency/

Visit once: http://localhost:8084/logistic/create This triggers a feign call.

When I visit zipkin again, I see this:

Summarize

Zipkin is a distributed tracing system that helps developers identify performance issues in distributed systems. Here are the pros and cons of Zipkin:

advantage:

  1. Ease of use: Zipkin is easy to install and configure, and provides a simple and intuitive user interface, making it easy for developers to use and understand system performance and bottlenecks.

  2. Ability to identify delays: Zipkin can help developers identify delays in distributed systems and analyze their causes. Developers can use this information to find bottlenecks and optimize performance.

  3. Scalability: Zipkin uses a code base and a modular structure to accommodate large distributed systems, so it can scale easily as the system grows.

shortcoming:

  1. For small-scale projects, the configuration of Zipkin may be tedious and complicated, requiring a certain learning cost.

  2. Zipkin needs to store tracking data in a dedicated storage system, and the maintenance and management of the storage system may require a certain amount of cost and effort.

  3. In highly concurrent environments, Zipkin's performance may suffer because of the large amount of trace data it needs to capture and process.

zipkin stores data

Note that this tutorial does not use a database to provide storage for zipkin. Zipkin Server stores tracking data in memory by default. This method is not suitable for production environments. Once the server is shut down and restarted or the service crashes, the historical data will disappear. Zipkin supports modifying the storage strategy to use other storage components, such as MySQL, Elasticsearch, etc.

1. Database script

(store link tracking data in MySQL for synchronous processing)

Open the MySQL database, create the zipkin library, and execute the SQL script.

Official website address: zipkin/mysql.sql at master openzipkin/zipkin GitHub

 
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;

Transfer the above code into a sql file

2. Deploy the Zipkin server

Add startup parameters and redeploy the server

Official website address: zipkin/zipkin-server-shared.yml at master openzipkin/zipkin GitHub

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

Call the service again, and you can see that the data is stored in mysql.

Guess you like

Origin blog.csdn.net/weixin_39570751/article/details/129618881