Spring Cloud Series 14 Distributed link monitoring Spring cloud sleuth

Source: Internet
Author: User
Tags sleep unique id visibility mysql database rabbitmq
1. Overview

Spring Cloud Sleuth for Distributed link monitoring on spring cloud
This article describes the sleuth related content, the main content is as follows: Spring Cloud Sleuth important terms and meanings: Span, Trance, Annotation zipkin graphically display Distributed link monitoring data and explain the meaning of the field spring Cloud Integrated Sleuth + Zipkin code Demo:sleuth integration Zipkin, Zipkin data persistence, etc. 2. Terminology

Span
span is the basic unit of work. Span includes a 64-bit unique ID, a 64-bit trace code, description information, timestamp event, Key-value annotation (tags), and the ID of the span processor (usually IP).
Initially, the initial span is called the root span, and the span ID and trace ID values are the same in this span.

Trance
Contains a series of spans that make up a tree structure

Annotation
For timely recording of events that exist. The common annotation is as follows Cs-client Sent: The client sends a request that represents the start of the span Sr-server Received: The server receives the request and begins to process it. (SR-CS) equals the delay of the network Ss-server Sent: The service-side processing request completes and the start returns to end to the server. (SS-SR) indicates the time at which the service-side processes the request Cr-client Received: The client finishes accepting the return result, at which time span ends. (CR-SR) indicates when the client receives server-side data

If the invocation relationship for a service is as follows:

Then the process of using the Zipkin annotations in a system with span and trace is graphically displayed:

Each color indicates a span (total 7 spans, from A to g), with similar information for each span

Trace id = X
Span id = D
Client Sent

This span indicates that the trance ID of the span is X,span ID is D, and it sends a client sent event

Spans's parent/child relationship is graphically as follows:
3. Graphical display of Distributed link monitoring data in Zipkin 3.1 spans information interpretation in Zipkin interface

The trace information in the image above is shown in the Zipkin, and the red box is a trace that calls span on the above image.

But when you click on this trace, we only see 4 spans

Why the two interfaces show a different number of spans, one is 7, one is 4. 1 spans: The interface Http:/start from Servier1 is invoked, respectively, by the server Received (SR) and server Sent (SS) annotations. 2 spans: Http:/foo interface from Service1 call Service2. The Service1 end has two spans, the client Sent (CS) and the client Received (CR) annotations, respectively. The Service2 end also has two spans, the server Received (SR) and the server Sent (SS), respectively. Physically he has 2 spans, but logically this they make up a span of RPC calls. 2 span: The Http:/bar interface from Service2 calls Service3, and the Service2 side has two spans, client Sent (CS) and client Received (CR) annotations, respectively. The Service3 end also has two spans, the server Received (SR) and the server Sent (SS), respectively. Physically he has 2 spans, but logically they are all spans of the same RPC invocation. 2 span: The Http:/bar interface from Service2 calls Service4, and the Service2 side has two spans, client Sent (CS) and client Received (CR) annotations, respectively. The Service4 side also has two spans, the Server Received (SR) and server Sent (SS), respectively. Physically he has 2 spans, but logically this is all a span of the same RPC invocation.

So we calculate the physical spans there are 7:1 from Http:/start are requested 2 from Service1 call Service2 2 from Service2 call Service3 2 from Service2 call Service4.

Logically, we only see 4 span:1 from Service1 interface Http:/start is requested 3 RCP interface call 3.2 from the service . Zipkin Visualization Errors

If an interface call fails in the call link, Zipkin will use the red display information by default, as shown in the following illustration:

Click on the red span to see a detailed failure message:
4. Spring Cloud Integrated Sleuth + Zipkin

This section demonstrates integrating sleuth in spring cloud and transferring link monitoring data to Zipkin, using Zipkin to present data, and configuring MySQL for data persistence 4.1. Related Projects

Cloud-registration-center
Registration Center

Cloud-service-zipkin
Provides a test service interface with two interfaces available externally:
-After the call succeeds, it returns a result: Http://127.0.0.1:17602//zipkin/simple
-After the call succeeds, the service sleep 5s before returning: Http://127.0.0.1:17602//zipkin/sleep

Cloud-consumer-zipkin
Consumer Services
This service will call through feign the two interfaces (/zipkin/sleep and/zipkin/simple) provided in Cloud-service-zipkin, and we will access the corresponding interfaces called by the following URLs:
Http://127.0.0.1:17603//zipkin/simple
Http://127.0.0.1:17603//zipkin/sleep

The code for the above 3 services is relatively simple, here the code is a little bit, you can see the GitHub code. Only the configurations associated with sleuth and Zipkin are listed below.

Cloud-dashboard-zipkin
Configure the Zipkin service to provide visibility of link monitoring
Zipkin Home: http://127.0.0.1:17601/zipkin/ 4.2. Using Sleuth+zipkin+http configuration in your project

Starting Sleuth,sleuth in Cloud-service-zipkin and Cloud-consumer-zipkin collects spans information and asynchronously sends spans information to Zipkin using HTTP, and then zipkin the display of information

Cloud-service-zipkin and Cloud-consumer-zipkin configuration Sleuth+zipkin
2 projects in order to integrate sleuth and Zipkin, it is necessary to add the following configuration on a general engineering basis: Pom.xml Add sleuth and Zipkin related jars

<!--sleuth configuration-
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!--Introducing Zipkin  -
<dependency>
    <groupId>org.springframework.cloud</groupId>
    < Artifactid>spring-cloud-starter-zipkin</artifactid>
</dependency>
Application-zipkin.yml
Zipkin+sleuth Configuration parameters:
SPRING.ZIPKIN.BASEURL: Specifies the service address of the Cloud-dashboard-zipkin, using the real IP in this example. In the new version spring-cloud-sleuth-core-1.3.0.release, you can implement access through the service name Spring.sleuth.sampler.percentage: Set the sample rate, in order to test settings 100% acquisition
Spring:
   zipkin:
     enabled:true
     # Zipkkin Dashboard Address: Access via real IP address
     baseurl:http://localhost:17601/
     # The Service name access registered to the registry via Cloud-dashboard-zipkin, this version (Spring-cloud-sleuth-core-1.2.5.release) is not supported, Need to start supporting this feature from Spring-cloud-sleuth-core-1.3.0.release
     # configuration as follows:
     # baseurl:http://cloud-dashboard-zipkin/
   Sleuth:
     Sampler:
       #  Default value is 0.1f, now for test settings 100% acquisition
       Percentage:1

Cloud-dashboard-zipkin
Configuring the Zipkin service Pom.xml

<!--Spring-cloud-starter-zipkin--
<dependency>
    <groupid>org.springframework.cloud </groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!--Zipkin interface--
<dependency>
    <groupId>io.zipkin.java</groupId>
    < artifactid>zipkin-autoconfigure-ui</artifactid>
</dependency>
<!--Zipkin service class--
<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId> Zipkin-server</artifactid>
</dependency>
Configuration parameters
Bootstrap-zipkin-http.yml
# port
Server:
  port:17601

Spring:
  application:
    # This service registers to the name registered to the server, this name is the service identifier
    that is called when the service is invoked later Name:cloud-dashboard-zipkin
Eureka:
  client:
    serviceurl:
      # Server Register/Get server zone
      defaultzone: http://127.0.0.1:10761/eureka/
      # defaultzone:http://192.168.21.3:10761/eureka/,http://192.168.21.4:10761/ eureka/
  instance:
    prefer-ip-address:true
Application-zipkin-http.yml
Turn off the push to Zipkin service feature for this project
Spring:
  Zipkin:
    enabled:false

Startup class :
@EnableZipkinServer: Annotations This class is for Zipkin service

@EnableEurekaClient//Configuration This app will use service registration and service Discovery
@SpringBootApplication
@EnableZipkinServer//start Zipkin service
public class Zipkindashboardcloudapplication {public

    static void Main (string[] args) {
        args = new string[1];
  
   args[0] = "--spring.profiles.active=zipkin-http";
        Springapplication.run (Zipkindashboardcloudapplication.class, args);
    }
  
4.3. Testing

start the Zipkin service to provide visibility of link monitoring
Zipkin Access Address: http://127.0.0.1:17601/zipkin/

we demonstrate link tracking and access to the following two requests :
Http://127.0.0.1:17603/zipkin/simple: Normal method
Http://127.0.0.1:17603/zipkin/sleep: Access Timeout

Enter the Zipkin, the access success is blue, the failure is the red part. Also shows the time spent by each service

Click on the blue record to enter the detailed interface
The entire interface spends about 19ms,cloud-zipkin-service business execution 15ms,cloud-zipkin-consumer Access Cloud-zipkin-service network latency is 2ms, The network delay that returns the result to Cloud-zipkin-service is 4s

Click Cloud-zipkin-service to get more accurate data

4.4. Zipkin Data Persistence

By default, Zipkin stores records to memory, and if the service restarts, all records are lost. To ensure durability, Zipkin supports MySQL, Elasticsearch, Cassandra Storage. We demonstrate the configuration of MySQL database for Zipkin, the other two configurations are similar, this article slightly
Pom.xml introducing a new jar for Cloud-dashboard-zipkin

<!--Zipkin Storage to a database needs to introduce this class--
<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
</dependency>

<!-- Saving to a database requires the following dependencies-
<dependency>
    <groupId>org.springframework.boot</groupId>
    < artifactid>spring-boot-starter-jdbc</artifactid>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>

Configuration Parameters :
APPLICATION-ZIPKIN-HTTP.YML:
Configuration starts when the database is initialized according to Classpath:/mysql.sql

Spring:
  zipkin:
    enabled:false
  # config MySQL
  datasource:
    schema:classpath:/mysql.sql
    # URL: Jdbc:mysql://127.0.0.1/test
    url:jdbc:mysql://127.0.0.1:3306/test?zerodatetimebehavior=converttonull& Servertimezone=gmt%2b8
    username:root
    password:root
# Switch This in to create the schema on startup:
    Initialize:true
    continueonerror:true
  Sleuth:
    enabled:false
Zipkin:
  storage:
    type : MySQL

Restart Service , after successful service, view database, generate database automatically

So the database is configured, all the spans information is stored in the database, the service is restarted, and no records are lost. 4.5 using sleuth+zipkin+ Spring Cloud stream configuration in the project

This release (Spring-cloud-sleuth-core-1.2.5.release) supports integrated Spring-cloud-sleuth-stream, However, Spring-cloud-sleuth-stream is not supported at the beginning of the new version from Spring-cloud-sleuth-core-1.3.0.release. Recommended for direct use via integrated RABBITMQ or Kafka. With regard to integrating RABBITMQ or Kafka, the key is to introduce Spring-rabbit or Spring-kafka dependency packages. The default destination name is Zipkin.
For this use demo. Here for the time being slightly 5. Code

The detailed code above is shown below
GitHub code, please use tag v0.11, do not use master, because I can not guarantee that the master code has not changed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.