Link Tracking Solution for SMEs-Spring Cloud Alibaba+Spring Cloud Sleuth + Zipkin

1. Background and selection

1.1 Background

At the current stage, microservices have been widely used in small and medium-sized enterprises, so there is a problem. When an error occurs in service D in the figure below, you don’t know that service B, C, or D has an error. Therefore, it is necessary to use microservice link tracking to accurately and quickly locate the error location.
insert image description here

1.2 Selection

For SMEs, I think there are the following requirements:

  1. open source/free
  2. Low coupling, no intrusion into code
  3. Easy to integrate and use, moderate throughput

In the above scenario, give up mycat with more intrusive code and skywalking and pinpoint with poor compatibility and more dependencies. I chose the simpler and easier-to-use Spring Cloud Sleuth + Zipkin. Although the functions provided are relatively simple, they can fully meet the daily needs of small and medium-sized enterprises.

1.3 Simple principle of Sleuth

If you want to know which service has a problem in the microservice call chain, you need to string these services together to know the upstream service and downstream service.
insert image description here
When all calls and return requests carry these parameters in the figure, by carrying the information, you can get information such as the link between services, the name of the calling service, the return result, and the time-consuming of the service. The effect of link tracking can be completed.

If you want to know the detailed principle, you can read it carefully: https://www.cnblogs.com/pingyeaa/p/10987438.html

2. Integrate Sleuth

2.1 Integration

Add the following code to the pom.xml file of all microservice projects.

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

2.2 Verification

When the following style appears in the log, it means success.
insert image description here
Among them, the four parameters provided by Sleuth are shown in the figure.

  1. flowable-resource refers to the application.name of the current microservice
  2. trace (trace): It is a collection of the following spans, which can be understood as the current service needs to call multiple services for one interface, and trace id is used to represent the collection of all calls
  3. Span (span): the unique id that sends an http request for the current service to call other microservices
  4. True whether to transfer the data to Zipkin, since Zipkin is connected here, it is true

2.3 view

insert image description here
insert image description here
As can be seen from the above figure, flowable-resource calls the corresponding relationship of systemportal, and you can know which service the error is in. However, in the case of many logs, it is very troublesome and complicated to only observe the logs, so the next step is to introduce Zipkin. Log visualization output is convenient for daily viewing.

3. Integrate Zipkin

3.1 Build

Visit the official website: https://github.com/openzipkin/zipkin Click the link below to download.
insert image description here

3.2 start

java -jar zipkin-server-2.23.16-exec.jar

After the download is complete, execute the above command to start the jar package, and visit http://localhost:9411/zipkin to access the page.

3.3 Integration projects

Add the following code to all microservice project pom.xml.

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

In all microservice project application. yml to add the following configuration.

  zipkin:
    base-url: http://127.0.0.1:9411/ #zipkin地址
    discovery-client-enabled: false  #不用开启服务发现
  sleuth:
    sampler:
      rate: 10000 #每秒数据采集量,多余将丢弃
      probability: 1.0 #采样百分比:收集请求书数量的百分比。(默认为0.1,如果100次访问只将10次上传给zipkin )

3.4 Display information

Visit the homepage to see a list of microservice calls, including whether the call is successful, the number of http requests sent, the call time, and the call. You can
insert image description here
also click show to view the details of the call. You can see whether the sending request is successful, the reason for the failure, the calling time, etc.
insert image description here
insert image description here

3.5 Using the database

When using Zipkin, the memory storage used by default, if the service is interrupted, all the records will be lost, so we can use Mysql to save the calling data.

After downloading the database script, execute the following command.
Database script address: https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql-v1/src/main/resources/mysql.sql

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

MYSQL_HOST:数据库地址
MYSQL_TCP_PORT:数据库端口
MYSQL_USER:用户名
MYSQL_PASS:密码

According to official documents, Mysql is only recommended for testing and demonstration, and ElasticSearch is recommended for production environments.

Guess you like

Origin blog.csdn.net/qq_20143059/article/details/122937266