7.1 Introduction to Link Tracing
7.1.1 Why Do I Need Link Tracing
\
Microservice architecture is a distributed architecture. It divides service units by business. A distributed system often has many service units. Due to the large number of service units and the complexity of business, it is difficult to locate errors and exceptions if they occur. It is mainly reflected in that a request may need to invoke many services, and the complexity of internal service invocation determines that the problem is difficult to locate. Therefore, in the microservice architecture, it is necessary to implement distributed link tracking to track which services participate in a request and in what order, so that the steps to reach each request can be clearly seen and problems can be quickly located.
, for example, in A micro service system, A request from the user, the request to front A first (e.g., the front-end interface), and then through the remote invocation, to achieve the system middleware B and C (such as load balancing, gateway, etc.), and finally achieve the back-end service D, E, the back-end business logic through A series of calculation finally bring the data back to the user. For such a request, through so many services, how to record the data of its request process? This is where service link tracing comes in.
Google open source Dapper link tracking component, and published the paper “Dapper, a Large-scale Distributed Systems Tracing Infrastructure” in 2010, This article is the benchmark and theoretical basis of link tracing in the industry, and has great reference value. Currently, link tracking components such as Google’s Dapper, Twitter’s Zipkin, and Alibaba’s Eagleeye are excellent open source link tracking components.
7.1.2 Basic Terms
Spring Cloud Sleuth is the technical term for Google’s open source project Dapper. See Figure 7-1
- Span: the basic unit of work. When sending a remote scheduling task, a Span is generated. A Span is uniquely identified by a 64-bit ID, and a Trace is uniquely identified by another 64-bit ID.
- Trace: a tree composed of a series of spans. A request for a microservice system API interface requires multiple microservices to be called. Each microservice call generates a new Span. All the Span generated by the request form the Trace.
- Annotations: Core annotations that record an event in time, define the start and end of a request. These notes include the following:
- Cs-client Sent – The Client sends a request that describes the beginning of the Span
- Sr-server Received – The Server receives the request and is ready to process it. If the Server subtracts its SR from the CS timestamp, the network transmission time is obtained.
- Ss-server Sent – This annotation indicates the completion of the request processing (when the request is returned to the client). If the SS timestamp is subtracted from the SR timestamp, the time of the Server request is obtained.
- Cr-client Received – This is the end of the Span, if the cr timestamp is subtracted from the CS timestamp to get the time consumed by the entire request.
Figure 7-1 Link tracing terms
7.2 Actual case
\
This article focuses on integrating Zipkin into Spring Cloud Sleuth. Integrating Zipkin into Spring Cloud Sleuth is as simple as introducing dependencies and configuring it.
7.2.1 Zipkin – Server
\
To prepare a Zzipkin-server, do as follows.
\
- Download Zipkin – Server
\
Download address: archiva-maven-storage-prod.oss-cn-beijing.aliyuncs.com/repository/…
- Start the Zipkin – Server
Java jar zipkin – server – 2.23.2 – exec. Jar
7.2.2 Zipkin – Client
\
To transform the payment microservice project and order microservice project as Zipkin clients, link data needs to be uploaded to Zipkin Server. The specific steps are as follows.
\
- Add the dependent
Add the following dependencies in the POM.xml that pays for service and order microservice items.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
Copy the code
\
- The configuration file
\
Configure Spring Cloud Sleuth link tracing in the payment and order project configuration file application.yml
Spring: application: name: cloud - payment - service zipkin: base - url: http://192.168.220.12:9411 sender: type: web sleuth: sampler: probability: 1Copy the code
\
- Spring.zipkin. base-URL: Specifies the Zipkin server used to send link reports
- Spirng. Zipkin. Sender. Type: web presentation using HTTP to send data
- Spring. The sleuth. Sampler. Aim-listed probability: sampling rate, the value of [0, 1], this says 100% sampling report
7.2.3 Starting and Testing
\
Start Zipkin-server, Eureka, payment service and order service respectively, and then request the order service for several times. Then the link report will be sent to ZipkinServer and the link data of ZipkinServer mobile phone through web, as shown in Figure 7-2.
Figure 7-2 Link tracing display
\
\
7.2.4 Transferring Link Data Using RabbitMQ
\
In this case, the collected data was uploaded to Zzipkin-server over HTTP. The messaging component in Spring Cloud Sleuth supports link data transfer. This section uses RabbitMQ to transfer link data.
- Install the RabbitMQ
Install RabbitMQ using Docker.
docker pull rabbitmq:management
docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq rabbitmq:management
Copy the code
\
- Zipkin-Server
When zzipkin-server is started, specify to get link message data from RabbitMQ. The command is as follows.
RABBIT_ADDRESSES=localhost java-jar zip-server-2.23.2-exec.jar # or the following command Java -jar zip-server-2.23.2-exec.jar --zipkin.collector.rabbitmq.addresses=localhostCopy the code
Note that localhost is used here, so you need to upload zipkin-server-2.23.2-exec.jar to the virtual machine where Docker resides.
- Zipkin-Client
Modify the configuration in the pay microservice and order microservice to send link data to RabbitMQ. The configuration is as follows.
Spring: zipkin: the rabbitmq: addresses: 192.168.220.12:5672 # base - the url: http://192.168.220.12:9411 sender: type: rabbit sleuth: sampler: probability: 1Copy the code
\
Because link data is sent in type=rabbit, spring.zipkin.rabbitmq needs to be configured, and base-URL is not required.
- Start and test
After zzipkin-server and Zzipkin-client are restarted, link data is sent to Zzipkin-server via RabbitMQ.
7.2.5 Using ElasticSearch to Store Link Data
\
In the example above, Zipkin Server stores link data in memory, and once the program restarts, all previous link data is lost. , then how can jianglian road data be stored? , Zipkin supports storing link data in MySql, Elasticsearch, and Cassandra databases. This section explains how to use Elasticsearch for storage. The steps are as follows.
- Install Elasticsearch and Kibana
Install Elasticsearch and Kibana using Docker. The command is as follows.
\
Docker Pull ElasticSearch :7.13.4 # Create a custom network (used to connect to other services connected to the same network (such as Kibana)) docker Network Create SomenetWork # Elasticsearch docker run -d --name Elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "Discovery.type =single-node" ElasticSearch :7.13.4 Docker pull Kibana :7.3.4 Kibana: 5601-5601 somenetwork -p 7.13.4Copy the code
\
- Zipkin-Server
When starting Zipkin Server, specify that ES is used to store data, using the following command
RABBIT_ADDRESSES=localhost STORAGE_TYPE=elasticsearch ES_HOSTS=http://localhost:9200 java -jar Zzipkin-server-2.23.2-exec.jar # or java-jar zzipkin-server-2.23.2-exec.jar --zipkin.collector.rabbitmq.addresses=localhost --STORAGE_TYPE=elasticsearch --ES_HOSTS=http://localhost:9200Copy the code
- Start and test
If you restart Zzipkin-server, you can see that link tracking data has been saved to Elasticsearch. If you restart Zzipkin-server again, the data still exists. See Figure 7-3.
Figure 7-3 Link data for Elasticsearch