Spring Cloud Distributed Micro Service Cloud Architecture Part 9: Service Link Tracking (Spring Cloud Sleut)

This article focuses on the service tracking component zipkin, which Spring Cloud Sleuth integrates.

1. Introduction

Add sleuth to the classpath of a Spring Boot application (see below
for Maven and Gradle examples), and you will see the correlation data
being collected in logs, as long as you are logging requests.

--- From the official website

The main function of Spring Cloud Sleuth is to provide tracing solutions in distributed systems, and zipkin is compatible. You only need to introduce appropriate dependencies in the pom file.

II. Service Tracking Analysis
In the micro-service architecture, services are divided by business. Understanding the springcloud architecture requires additional requirements: 3536227259, through REST calls, an interface exposed to the outside world may require a lot of service collaboration to complete this interface function. If any of the services on the link has problems or the network times out, the interface call will fail.As the business continues to expand, calls between services become more and more complex.

With more and more services, the analysis of call chains becomes more and more complex.The call relationships between them may be as follows:
![]

Terminology
Span: A basic unit of work, for example, sending an RPC in a new space is equivalent to sending a response request to the RPC. Span is uniquely identified by a 64-bit ID, trace is represented by another 64-bit ID, and span has other data information, such as a summary, timestamp events, key value annotations (tags), the ID of the span, and the progress ID (typically an IP address) span is constantly starting and spanningStop and record the time. When you create a span, you must stop it at some point in the future.

  • Trace: A tree of spans, for example, if you're running a distributed big data project, you might need to create a trace.
  • Annotation: Used to keep a timely record of the existence of an event, and some core annotations are used to define the start and end of a request
  • cs - Client Sent - The client initiates a request, and this annotation describes the beginning of the space
  • sr - Server Received - The server gets the request and is ready to start processing it. If it subtracts the cs timestamp from its sr, it will get network latency
  • ss - Server Sent - Comment indicates that request processing is complete (when the request returns to the client), and if ss subtracts the sr timestamp, the processing time required by the server can be obtained
  • cr - Client Received - Indicates the end of the span, the client successfully receives a reply from the server, and if cr subtracts the cs timestamp, all the time it takes for the client to get a reply from the server
    Graphics the process of using Zipkin annotations with Span and Trace in a system:
    Graphics the process of using Zipkin annotations with Span and Trace in a system:


4. Construction Project
After explaining the basic knowledge, let's go to battle. The case of this article mainly consists of three projects: a server-zipkin, whose main function is to use the functions of Zipkin Server to collect call data and show it; a service-hi, to expose hi interface to the outside; a service-miya, to expose Miya interface to the outside; the two services can be invoked each other; and only the server-z service can be invoked.Ipkins collect data, which is why it's called service tracking.

4.1 Building server-zipkin
When spring Cloud was version F, you no longer needed to build your own Zipkin Server, just download jar, download address:

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

You can also download it here:

Links: https://pan.baidu.com/s/1w614Z8gJXHtqLUB6dKWOpQ Password: 26pf

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

java -jar zipkin-server-2.10.1-exec.jar

Access browser localhost:9494

4.2 Create service-hi
Start-up dependency on spring-cloud-starter-zipkin is introduced in its pom with the following code:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
<modelVersion>4.0.0</modelVersion>

<groupId>com.forezp</groupId>
<artifactId>service-zipkin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>service-hi</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>com.forezp</groupId>
    <artifactId>sc-f-chapter9</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

The zipkin server address is specified in its configuration file application.yml, and the header is specified by configuring "spring.zipkin.base-url":

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

By introducing the spring-cloud-starter-zipkin dependency and setting the spring.zipkin.base-url, you can.

Exposure Interface:

package com.forezp;

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.logging.Level;
import java.util.logging.Logger;

@SpringBootApplication
@RestController
public class ServiceHiApplication {

    public static void main(String[] args) {
        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 Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

}

4.3 Create service-miya
Create process pain service-hi, introduce the same dependency, configure spring.zipkin.base-url.

Exposure Interface:

package com.forezp;

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.logging.Level;
import java.util.logging.Logger;

@SpringBootApplication
@RestController
public 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();
    }

    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }
}

4.4 Start a project, demonstrate tracking
Start the above project in turn, and open the browser to access: http://localhost:9411/, the following interface appears:

Visit: http://localhost:8989/miya, browser appears:

i'm service-hi

Open the interface at http://localhost:9411/and click Dependencies to discover the dependencies of the service:

Click find traces to see the data for specific service calls to each other:

Keywords: Programming Spring Maven Java Apache

Added by ponies3387 on Wed, 18 Dec 2019 05:21:25 +0200