As we all know, microservices are transmitted through Feign. In a complex microservice architecture system, almost every front-end request will form a complex distributed service invocation link. Any delay timeout or error of any dependent service in each link may cause the final failure of the whole request. When the business process is sufficiently complex, a complete chain of HTTP request invocations typically passes through multiple microservice systems, making it no longer simple to trace the entire chain through a log. Sleuth can be used to easily see the time consumption of each collection request and analyze which service invocation is time-consuming. When the time consumption of service invocation increases with the increase of the number of requests, some optimization measures can be taken for the business. So we can solve this problem by we can solve this problem by Spring Cloud Sleuth. Here we show you how to track this process with Spring Cloud Sleuth and show it in a graphical interface with Zipkin. Before the presentation, introduce RabbitMQ, Sleuth, and Zinkip.

  1. rabbitmq

    • RabbitMQ is open source message broker software (also known as message-oriented middleware) that implements the Advanced Message Queuing Protocol (AMQP). RabbitMQ servers are written in the Erlang language, while clustering and failover are built on top of the open telecom platform framework. All major programming languages have client libraries that communicate with proxy interfaces.
  2. Sleuth and zinkip

    • Sleuth is a component of Spring Cloud that implements a distributed tracking solution for Spring Cloud applications that is compatible with ZinkIP,HTrace, and log-based tracking
    • Zipkin is an open source Distributed Tracking System based on Google Dapper’s paper and developed and contributed by Twitter. Its main function is to gather real-time monitoring data from various heterogeneous systems to track system delay in microservices architecture. Zipkin’s user interface can present a relational graph showing how many tracked requests go through each layer of the application. Zipkin uses a Trace structure to Trace a request and breaks each Trace into dependent spans. In a microservice architecture, a user request may be handled by several services in the background, so each service processing the request can be understood as a Span (which can include API services, caching services, database services, reporting services, etc.). Of course, this service may also continue to request other services, so the Span is a tree structure that represents the invocation relationships between services. Zipkin’s user interface can not only view the dependency relationships of Span, but also display the time consumption of each Span in the form of waterfall graph, which can see the performance of each service at a glance.

Some terms in sleuth

  1. Span: The basic unit of work, for example, is that sending an RPC in a newly created span is equivalent to sending a response request to an RPC. The span is uniquely identified by a 64-bit ID, the trace is represented by another 64-bit ID, and the span has other data information. For example, summaries, timestamp events, tags, SPAN ids, and progress ids (usually IP addresses). Spans are constantly started and stopped, and they record time information. When you create a span, you have to stop it at some point in the future.
  2. Trace: A tree of a series of spans. For example, if you’re running a distributed project, you might want to create a Trace.
  3. Annotations: Used to record the presence of an event in time. Some core annotations are used to define the start and end of a request
  • Cs-client Sent – The Client initiates a request. This annotion describes the start of the span
  • Sr-server Received – The Server gets the request and is ready to start processing it, and the network delay can be obtained by subtracting the CS timestamp from its SR
  • Ss-server Sent – annotation indicates that the request processing is complete (when the request is returned to the client). If SS subtracts the SR timestamp, the time required by the Server to process the request can be obtained
  • Cr-client Received – Indicates the end of the SPAN, the Client successfully Received the reply from the server, and if cr subtracts the CS timestamp, all the time required for the Client to get the reply from the server is obtained

Then we started to build Greenwich.SR1 for cloud version and 2.1.6 for boot

1. Introduce sleUTH dependency in POM. XML

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

2. Simulate two logs between these two services through feign call, by test call system, here originally has a registry, here will not demonstrate

The test module

@Slf4j @RestController public class TestController { @Autowired private IHelloService helloService; @getMapping ("hello") public String hello(String name) {log.info("Feign calls system/Hello "); @getMapping ("hello") public String Hello (String name) {log.info("Feign calls system/Hello "); return this.helloService.hello(name); }}Copy the code

Create IHelloService under the Service package of the Test module

@FeignClient(value = "system",contextId = "helloServiceClient")
public interface IHelloService {
    @GetMapping("hello")
    String hello(@RequestParam("name") String name);
}Copy the code

The system module

@Slf4j @RestController public class TestController { @GetMapping("hello") public String hello(String name) { Log.info ("/hello service called "); return "hello" + name; }}Copy the code

3. Access interface localhost: 8202 / test/hello? Name =sleuth: Two customized logs will appear

View generated by the test module on startup

The 2019-08-23 14:22:51. 774 INFO [test, 72 bb0469bee07104, 72 bb0469bee07104, false] 22728 - [nio - 8202 - exec - 1) C.M.F.S.T est. Controller. TestController: Feign/hello service call systemCopy the code

View generated by the System module on startup

The 2019-08-23 14:22:52. 469 INFO system, 72 bb0469bee07104, 43597 a6edded6f2e, false] 812 - [nio - 8201 - exec - 2] C.M.F.S.S.C ontroller. TestController: / hello service is invokedCopy the code

As you can see, appear in the log [Test, 72 bb0469bee07104, 72 bb0469bee07104, false] information, this information by Spring Cloud Sleuth, micro service request to track links. This information contains the values of four parts, which have the following meanings:

  1. The name of the system microservice, corresponding to Spring.application.name;
  2. 72BB0469BEE07104 is called the Trace ID, which is fixed throughout a full request link. Look at the log above to confirm this;
  3. 43597a6EDDED6F2e is called the Span ID, which represents a basic unit of work;
  4. False indicates whether to export this information to a service such as Zipkin for collection and display. We have not integrated Zipkin yet, so false.

Now let’s integrate Zipkin

Before we can integrate Zipkin, we need to build RabbitMQ. RabbitMQ is used to collect trace information provided by Sleuth and then Zipkin Server gets it from RabbitMQ to improve performance.

Before installing the RabbitMQ, need to install Erlang/OTP, download address is: www.erlang.org/downloads/,… Once installed, download RabbitMQ from www.rabbitmq.com/install-win…

After installing RabbitMQ, go to the RabbitMQ installation directory sbin and run the following command

rabbitmq-plugins enable rabbitmq_managementCopy the code

Enter http://localhost:15672 in the browser. The default user name and password are guest. After login, you can see:

Click Admin Tab to add a new user:

The user name is febs, the password is 123456, and the role is administrator. The newly added user is still in No Access state. After you authorize the user, the user can be remotely accessed using the user name. Click to add a user name. Enter the authorization page and click the Set Permission button to perform user authorization operations. With RabbitMQ installed, we started integrating Zipkin. In earlier versions of Spring Cloud, we can build Zipkin Server by ourselves. Now we can only use the official Zipkin Server at github.com/openzipkin/…

To download zipkin.jar, run the following command in a CMD window (you can run this command in git bash on Windows if you don’t have curl) :

curl -sSL https://zipkin.io/quickstart.sh | bash -sCopy the code

If the download speed is very slow, you can copy the link to the Thunderbolt download and rename it to zipkin.jar.

Zipkin supports saving trace information to the MySQL database, so before running zipkin.jar, we prepare the related library table. The SQL script address is:

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

When the library table is ready, run the following command to start zipkin.jar:

java -jar zipkin.jar --server.port=8402 --zipkin.storage.type=mysql --zipkin.storage.mysql.db=febs_cloud_base --zipkin.storage.mysql.username=root --zipkin.storage.mysql.password=123456 --zipkin.storage.mysql.host=localhost --zipkin.storage.mysql.port=3306 --zipkin.collector.rabbitmq.addresses=localhost:5672 --zipkin.collector.rabbitmq.username=febs --zipkin.collector.rabbitmq.password=123456Copy the code

The command above specifies the database link and RabbitMQ link information. For more optional configurations, unzip zipkin.jar and view the zipkin-server-shared.yml configuration class in zipkin\ boot-INF \classes.

Once zipkin.jar is started, introduce the following dependencies in the corresponding module’s POM:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
</dependency>Copy the code

Modify the application. Yml of the corresponding module

spring:
    zipkin:
    sender:
        type: rabbit
  sleuth:
    sampler:
      probability: 1
  rabbitmq:
    host: localhost
    port: 5672
    username: febs
    password: 123456Copy the code

Spring. Zipkin. Sender. Type specifies the use the RabbitMQ collect tracking information;

Spring. The sleuth. Sampler. Aim-listed probability the default value is 0.1, the sampling rate is only 1/10, sent 10 requests only a pen were collected. For testing purposes, we can set it to 1, which is 100% sampling;

Spring. Rabbitmq is used to configure the rabbitMQ connection information. You may ask why the rabbitMQ port is set to 15672, but it is set to 5672. No, 15672 is the RabbitMQ admin port and 5672 is the AMPQ port.

After add good configuration, the start system and the test module, sending a localhost: 8202 / test/hello? Name = summer request, use the browser to access the http://localhost:8402/zipkin/ link, and then click shown in the figure

To view dependencies:

View the data table to see if information is stored:


Finally, recently, many friends asked me for Linux learning roadmap, so I stayed up for a month in my spare time according to my own experience, and sorted out an e-book. Whether you are interviewing or self-improvement, I believe will help you! The directory is as follows:

Free to everyone, just ask everyone to point to me!

Ebook | Linux development learning roadmap

Also hope to have a small partner can join me, do this e-book more perfect!

Have a harvest? Hope the old iron people come to a triple whammy, give more people to see this article

Recommended reading:

  • Dry goods | programmers advanced architect necessary resources free of charge
  • Artifact | support resource site search