1. 程式人生 > >史上最簡單的SpringCloud教程 | 第九篇: 服務鏈路追蹤(Spring Cloud Sleuth)(Finchley版本)

史上最簡單的SpringCloud教程 | 第九篇: 服務鏈路追蹤(Spring Cloud Sleuth)(Finchley版本)

這篇文章主要講述服務追蹤元件zipkin,Spring Cloud Sleuth集成了zipkin元件。

一、簡介

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.

—— 摘自官網

Spring Cloud Sleuth 主要功能就是在分散式系統中提供追蹤解決方案,並且相容支援了 zipkin,你只需要在pom檔案中引入相應的依賴即可。

二、服務追蹤分析

微服務架構上通過業務來劃分服務的,通過REST呼叫,對外暴露的一個介面,可能需要很多個服務協同才能完成這個介面功能,如果鏈路上任何一個服務出現問題或者網路超時,都會形成導致介面呼叫失敗。隨著業務的不斷擴張,服務之間互相呼叫會越來越複雜。

Paste_Image.png

隨著服務的越來越多,對呼叫鏈的分析會越來越複雜。它們之間的呼叫關係也許如下:

Paste_Image.png

三、術語

  • Span:基本工作單元,例如,在一個新建的span中傳送一個RPC等同於傳送一個迴應請求給RPC,span通過一個64位ID唯一標識,trace以另一個64位ID表示,span還有其他資料資訊,比如摘要、時間戳事件、關鍵值註釋(tags)、span的ID、以及進度ID(通常是IP地址) span在不斷的啟動和停止,同時記錄了時間資訊,當你建立了一個span,你必須在未來的某個時刻停止它。
  • Trace:一系列spans組成的一個樹狀結構,例如,如果你正在跑一個分散式大資料工程,你可能需要建立一個trace。
  • Annotation:用來及時記錄一個事件的存在,一些核心annotations用來定義一個請求的開始和結束
    • cs - Client Sent -客戶端發起一個請求,這個annotion描述了這個span的開始
    • sr - Server Received -服務端獲得請求並準備開始處理它,如果將其sr減去cs時間戳便可得到網路延遲
    • ss - Server Sent -註解表明請求處理的完成(當請求返回客戶端),如果ss減去sr時間戳便可得到服務端需要的處理請求時間
    • cr - Client Received -表明span的結束,客戶端成功接收到服務端的回覆,如果cr減去cs時間戳便可得到客戶端從服務端獲取回覆的所有所需時間 將Span和Trace在一個系統中使用Zipkin註解的過程圖形化:

將Span和Trace在一個系統中使用Zipkin註解的過程圖形化:

Paste_Image.png

四、構建工程

基本知識講解完畢,下面我們來實戰,本文的案例主要有三個工程組成:一個server-zipkin,它的主要作用使用ZipkinServer 的功能,收集呼叫資料,並展示;一個service-hi,對外暴露hi介面;一個service-miya,對外暴露miya介面;這兩個service可以相互呼叫;並且只有呼叫了,server-zipkin才會收集資料的,這就是為什麼叫服務追蹤了。

4.1 構建server-zipkin

在spring Cloud為F版本的時候,已經不需要自己構建Zipkin Server了,只需要下載jar即可,下載地址:

也可以在這裡下載:

下載完成jar 包之後,需要執行jar,如下:

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

訪問瀏覽器localhost:9494

4.2 建立service-hi

在其pom引入起步依賴spring-cloud-starter-zipkin,程式碼如下:

<?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">
    <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>

在其配置檔案application.yml指定zipkin server的地址,頭通過配置“spring.zipkin.base-url”指定:



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

通過引入spring-cloud-starter-zipkin依賴和設定spring.zipkin.base-url就可以了。

對外暴露介面:

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 建立service-miya

建立過程痛service-hi,引入相同的依賴,配置下spring.zipkin.base-url。

對外暴露介面:


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 啟動工程,演示追蹤

Paste_Image.png

i’m service-hi

再開啟http://localhost:9411/的介面,點選Dependencies,可以發現服務的依賴關係:

Paste_Image.png

點選find traces,可以看到具體服務相互呼叫的資料:

Paste_Image.png

本文原始碼下載:

本文來自 方誌朋 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/forezp/article/details/81041078?utm_source=copy