Springcloud Tutorial (ii) Service link Tracking (Spring Cloud Sleuth)

Source: Internet
Author: User

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

To build a spring-boot project named Server-zipkin, in which the Pom introduces dependency:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> &LT;ARTIFACTID&GT;SPRING-BOOT-STARTER-WEB&LT;/ARTIFAC tid> </dependency> <dependency> <groupid>org.springframework.boot</groupi        D> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.zipkin.java</groupId> & lt;artifactid>zipkin-server</artifactid> </dependency> <dependency> <grou Pid>io.zipkin.java</groupid> <artifactId>zipkin-autoconfigure-ui</artifactId> </ Dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> & Lt;groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</art                Ifactid> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymana Gement>

In its program entry class, add annotation @enablezipkinserver, turn on zipkinserver function:

@SpringBootApplication @enablezipkinserverpublic class Serverzipkinapplication {public    static void Main (string[] args) {        springapplication.run (serverzipkinapplication.class, args);}    }

Specify the service port in the configuration file application.yml:

server.port=9411

  

4.2 Creating Service-hi

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

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--compile (' Org.spri Ngframework.cloud:spring-cloud-starter-zipkin ')-<dependency> <groupid>org.springframew Ork.cloud</groupid> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependen cy> <dependency> <groupId>org.springframework.boot</groupId> &LT;ARTIFAC    Tid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency>                </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-dependencies     </artifactId>           <version>Dalston.RC1</version> <type>pom</type> <scope >import</scope> </dependency> </dependencies> </dependencyManagement>

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

Server.port=8988spring.zipkin.base-url=http://localhost:9411spring.application.name=service-hi

  

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

External exposure Interface:

@SpringBootApplication @restcontrollerpublic class Servicehiapplication {public    static void Main (string[] args) { C1/>springapplication.run (Servicehiapplication.class, args);    }    private static final Logger LOG = Logger.getlogger (ServiceHiApplication.class.getName ());    @Autowired    private resttemplate resttemplate;    @Bean public    resttemplate getresttemplate () {        return new resttemplate ();    }    @RequestMapping ("/hi") public    String Callhome () {        LOG.log (Level.info, "calling Trace Service-hi  ");        Return Resttemplate.getforobject ("Http://localhost:8989/miya", String.class);    }    @RequestMapping ("/info") public    String info () {        LOG.log (Level.info, "calling Trace Service-hi");        Return "I ' m Service-hi";    }    @Bean public    Alwayssampler Defaultsampler () {        return new Alwayssampler ();}    }

  

4.3 Creating Service-miya

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

External exposure Interface:

@SpringBootApplication @restcontrollerpublic class Servicemiyaapplication {public    static void Main (string[] args) {        Springapplication.run (servicemiyaapplication.class, args);    }    private static final Logger LOG = Logger.getlogger (ServiceMiyaApplication.class.getName ());    @RequestMapping ("/hi") Public    String Home () {        LOG.log (level.info, "Hi is being called");        Return "Hi i ' m miya!";    }    @RequestMapping ("/miya") public    String info () {        LOG.log (level.info, "info is being called");        Return Resttemplate.getforobject ("Http://localhost:8988/info", String.class);    }    @Autowired    private resttemplate resttemplate;    @Bean public    resttemplate getresttemplate () {        return new resttemplate ();}    }

  

4.4 Start-Up project, demo tracking

Start the above three projects 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:

Source Source

Springcloud Tutorial (ii) Service link Tracking (Spring Cloud Sleuth)

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.