Amateur Grass Springcloud Tutorial | Nineth: Service link Tracking (Spring Cloud Sleuth) (Finchley version)

Source: Internet
Author: User

This article focuses on the service tracking component zipkin,spring Cloud Sleuth integrates Zipkin components.

First, Introduction

Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle examples), and you'll see the C Orrelation data being collected in logs, as long as is logging requests.

--Excerpt from official website

The main function of Spring Cloud sleuth is to provide a tracking solution in a distributed system, and compatible support for Zipkin, you only need to introduce the appropriate dependencies in the Pom file.

Second, service tracking analysis

MicroServices architecture on the service through the Division of services, through rest calls, exposed to an interface, may require a lot of service collaboration to complete this interface function, if any one of the links on the link service problems or network timeout, will be formed to cause the interface call failure. As the business continues to expand, calls between services are becoming more complex.

As services become more and more, the analysis of the call chain becomes more and more complex. The invocation relationship between them might look like this:

Iii. terminology
    • Span: basic unit of work, for example, sending an RPC in a new span is equivalent to sending a response request to rpc,span through a 64-bit ID that uniquely identifies the trace with another 64-bit ID, and span has other data information such as a digest, timestamp event, Key value Comments (tags), the ID of the span, and the progress ID (usually the IP address)
      Span is constantly starting and stopping, while recording time information, when you create a span, you must stop it at some point in the future.
    • Trace: A tree-like structure consisting of a series of spans, for example, if you are running a distributed Big Data project, you may need to create a trace.
    • Annotation: Used to record the existence of an event in a timely manner, some core annotations used to define the start and end of a request
      • Cs-client Sent-The client initiates a request, and this annotion describes the beginning of the span
      • Sr-server Received-The server gets the request and is ready to start processing it, if its SR minus CS Timestamp can get network delay
      • Ss-server Sent-Annotations indicate the completion of the request processing (when the request is returned to the client), if the SS minus the SR timestamp can get the processing request time required by the server
      • Cr-client Received-Indicates the end of span, the client successfully receives a reply from the server, and if the CR minus CS timestamp can get the client to get reply from the server all the time required
        The process of using the Zipkin annotations in a system with span and trace is graphical:

The process of using the Zipkin annotations in a system with span and trace is graphical:

Iv. Construction of the project

Basic knowledge explained, the following we come to the actual combat, the case of this article is mainly composed of three projects: a Server-zipkin, its main function uses the Zipkinserver function, collects the call data, and displays; a service-hi, external exposure Hi interface ; a service-miya, externally exposed Miya interface; These two services can be called each other, and only if the call is made, Server-zipkin collects the data, which is why it is called service tracking.

4.1 Building Server-zipkin

When spring cloud is the F version, you don't have to build Zipkin server yourself, just download the jar:

https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

can also be downloaded here:

Link: Https://pan.baidu.com/s/1w614Z8gJXHtqLUB6dKWOpQ Password: 26PF

After downloading the jar package, you need to run the jar, as follows:

Java-jar Zipkin-server-2.10.1-exec.jar

Access Browser localhost:9494

4.2 Creating Service-hi

In its pom introduction start dependent Spring-cloud-starter-zipkin, the code is as follows:

1<?xml version= "1.0" encoding= "UTF-8"?>2<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >4<modelVersion>4.0.0</modelVersion>5  6<groupId>com.forezp</groupId>7<artifactId>service-zipkin</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>jar</packaging>Ten   One<name>service-hi</name> A<description>demo Project forSpring boot</description> -   -<parent> the<groupId>com.forezp</groupId> -<artifactId>sc-f-chapter9</artifactId> -<version>0.0.1-SNAPSHOT</version> -</parent> +   -   +   A<dependencies> at   -<dependency> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-web</artifactId> -</dependency> -   in<dependency> -<groupId>org.springframework.cloud</groupId> to<artifactId>spring-cloud-starter-zipkin</artifactId> +</dependency> -   the   *   $</dependencies>Panax Notoginseng   -<build> the<plugins> +<plugin> A<groupId>org.springframework.boot</groupId> the<artifactId>spring-boot-maven-plugin</artifactId> +</plugin> -</plugins> $</build> $   -</project>

In its configuration file application.yml specifies the address of the Zipkin server, the header is specified by configuring "Spring.zipkin.base-url":

1 server.port=89882 spring.zipkin.base-url=http://localhost:94113 Spring.application.name=service-hi

By introducing Spring-cloud-starter-zipkin dependency and setting the Spring.zipkin.base-url on it.

External exposure Interface:

1  PackageCom.forezp;2  3 ImportBrave.sampler.Sampler;4 Importorg.springframework.beans.factory.annotation.Autowired;5 Importorg.springframework.boot.SpringApplication;6 Importorg.springframework.boot.autoconfigure.SpringBootApplication;7 ImportOrg.springframework.context.annotation.Bean;8 Importorg.springframework.web.bind.annotation.RequestMapping;9 ImportOrg.springframework.web.bind.annotation.RestController;Ten Importorg.springframework.web.client.RestTemplate; One ImportJava.util.logging.Level; A ImportJava.util.logging.Logger; -   - @SpringBootApplication the @RestController -  Public classservicehiapplication { -   -      Public Static voidMain (string[] args) { +Springapplication.run (servicehiapplication.class, args); -     } +   A     Private Static FinalLogger LOG = Logger.getlogger (servicehiapplication.class. GetName ()); at   -   - @Autowired -     Privateresttemplate resttemplate; -   - @Bean in      Publicresttemplate getresttemplate () { -         return Newresttemplate (); to     } +   -@RequestMapping ("/hi") the      PublicString callhome () { *LOG.log (Level.info, "calling Trace Service-hi"); $         returnResttemplate.getforobject ("Http://localhost:8989/miya", String.class);Panax Notoginseng     } -@RequestMapping ("/info") the      PublicString info () { +LOG.log (Level.info, "calling Trace Service-hi"); A   the         return"I ' m Service-hi"; +   -     } $   $ @Bean -      PublicSampler Defaultsampler () { -         returnsampler.always_sample; the     } -  Wuyi   the}

4.3 Creating Service-miya

Create process Pain Service-hi, introduce the same dependency, configure under Spring.zipkin.base-url.

External exposure Interface:

1  2  PackageCom.forezp;3  4 ImportBrave.sampler.Sampler;5 Importorg.springframework.beans.factory.annotation.Autowired;6 Importorg.springframework.boot.SpringApplication;7 Importorg.springframework.boot.autoconfigure.SpringBootApplication;8 ImportOrg.springframework.context.annotation.Bean;9 Importorg.springframework.web.bind.annotation.RequestMapping;Ten ImportOrg.springframework.web.bind.annotation.RestController; One Importorg.springframework.web.client.RestTemplate; A   - ImportJava.util.logging.Level; - ImportJava.util.logging.Logger; the   - @SpringBootApplication - @RestController -  Public classservicemiyaapplication { +   -      Public Static voidMain (string[] args) { +Springapplication.run (servicemiyaapplication.class, args); A     } at   -     Private Static FinalLogger LOG = Logger.getlogger (servicemiyaapplication.class. GetName ()); -   -   -@RequestMapping ("/hi") -      PublicString Home () { inLOG.log (Level.info, "Hi is being called"); -         return"Hi i ' m miya!"; to     } +   -@RequestMapping ("/miya") the      PublicString info () { *LOG.log (Level.info, "INFO is being called"); $         returnResttemplate.getforobject ("Http://localhost:8988/info", String.class);Panax Notoginseng     } -   the @Autowired +     Privateresttemplate resttemplate; A   the @Bean +      Publicresttemplate getresttemplate () { -         return Newresttemplate (); $     } $   -   - @Bean the      PublicSampler Defaultsampler () { -         returnsampler.always_sample;Wuyi     } the}

4.4 Start-Up project, demo tracking

Start the above project in turn, open the browser access: http://localhost:9411/, the following interface appears:

Access: Http://localhost:8989/miya, Browser appears:

I ' M service-hi

Then open the Http://localhost:9411/interface, click Dependencies, you can find the dependencies of the service:

Click Find traces to see the data that the service calls to each other:

This article source code download:

Https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter9

V. References

Spring-cloud-sleuth

Http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

Thank you for your attention! Add QQ1 Group: 135430763,QQ2 Group: 454796847,QQ3 Group: 187424846. QQ Group into the group password: xttblog, want to add a group of friends, you can search: Xmtxtt, note: "Xttblog", add Assistant pull you into the group. Note errors do not agree with a friend application. Thanks again for your attention! Follow up with wonderful content will be sent to you the first time! Please send the original article to [email protected] email. Business cooperation can add assistants to communicate!

Amateur Grass Springcloud Tutorial | Nineth: Service link Tracking (Spring Cloud Sleuth) (Finchley version)

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.