【SpringCloud】 第九篇: 服務鏈路追蹤(Spring Cloud Sleuth)

前言:

必需學會SpringBoot基礎知識html

簡介:

spring cloud 爲開發人員提供了快速構建分佈式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分佈式會話等等。它運行環境簡單,能夠在開發人員的電腦上跑。java

工具:

JDK8git

apache-maven-3.5.2github

IntelliJ IDEA 2018.1 x64web

 

1、簡介

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

—— 摘自官網apache

Spring Cloud Sleuth 主要功能就是在分佈式系統中提供追蹤解決方案,而且兼容支持了 zipkin,你只須要在pom文件中引入相應的依賴便可。瀏覽器

2、服務追蹤分析

微服務架構上經過業務來劃分服務的,經過REST調用,對外暴露的一個接口,可能須要不少個服務協同才能完成這個接口功能,若是鏈路上任何一個服務出現問題或者網絡超時,都會造成致使接口調用失敗。隨着業務的不斷擴張,服務之間互相調用會愈來愈複雜。網絡

                                SpringCloud_配置中心9-1

隨着服務的愈來愈多,對調用鏈的分析會愈來愈複雜。它們之間的調用關係也許以下:架構

                                 SpringCloud_配置中心9-2

 

3、術語

  • 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註解的過程圖形化:

    SpringCloud_配置中心9-3

 

4、構建工程

基本知識講解完畢,下面咱們來實戰,本文的案例主要有三個工程組成:一個eureka-server-zipkin,它的主要做用使用ZipkinServer 的功能,收集調用數據,並展現;一個eureka-server-zipkin-client,對外暴露hi接口;一個eureka-server-zipkin-eddie,對外暴露eddie接口;這兩個service能夠相互調用;而且只有調用了,eureka-server-zipkin纔會收集數據的,這就是爲何叫服務追蹤了。

理論差很少, 就不繼續探討, 都是那句老話: 師傅帶入門, 本領看本身
4.1 建立 eureka-server-zipkin

pom引入依賴:

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

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

    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-server</artifactId>
    </dependency>

    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-autoconfigure-ui</artifactId>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

在其程序入口類, 加上註解@EnableZipkinServer,開啓ZipkinServer的功能:

package com.lwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.EnableZipkinServer;


/**
 * @author Eddie
 */
@EnableZipkinServer
@SpringBootApplication
public class EurekaServerZipkinApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerZipkinApplication.class, args);
    }
}

在配置文件application.yml指定服務端口爲:

server:
port: 9411
4.2 建立 eureka-server-zipkin-client

在其pom引入起步依賴spring-cloud-starter-zipkin,代碼以下:

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

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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在其配置文件application.yml指定zipkin server的地址,頭經過配置「spring.zipkin.base-url」指定:

server:
  port: 8988
spring:
  zipkin:
    base-url: http://localhost:9411
  application:
    name: eureka-server-zipkin-client

經過引入spring-cloud-starter-zipkin依賴和設置spring.zipkin.base-url就能夠了。

對外暴露接口:

package com.lwc.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author eddie.lee
 * @Package com.lwc.controller
 * @ClassName ZipkinController
 * @description
 * @date created in 2018-04-09 13:55
 * @modified by
 */
@Slf4j
@RestController
public class ZipkinController {


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hi")
    public String callHome() {
        log.info(" callHome() ==> calling trace eureka-server-zipkin-client");
        return restTemplate.getForObject("http://localhost:8989/eddie", String.class);
    }

    @GetMapping("/info2")
    public String info2() {
        log.info(" info2() ==> calling trace eureka-server-zipkin-client");
        return "this is eureka-server-zipkin-client";

    }

}

4.3 建立 eureka-server-zipkin-eddie

建立過程 eureka-server-zipkin-client,引入相同的依賴,配置下spring.zipkin.base-url。

對外暴露接口:

package com.lwc.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author eddie.lee
 * @Package com.lwc.controller
 * @ClassName EddieController
 * @description
 * @date created in 2018-04-09 15:56
 * @modified by
 */
@Slf4j
@RestController
public class EddieController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hi")
    public String home(){
        log.info("==> hi is being called");
        return "hi i'm eddie.lee!";
    }

    @GetMapping("/eddie")
    public String info(){
        log.info("==> info is being called");
        return restTemplate.getForObject("http://localhost:8988/info2",String.class);
    }
}

4.4 啓動工程,演示追蹤

依次啓動上面的三個工程,打開瀏覽器訪問:http://localhost:9411/,會出現如下界面:

                   image

訪問:http://localhost:8989/eddie,瀏覽器出現:

this is eureka-server-zipkin-client

 

再打開http://localhost:9411/的界面,點擊 依賴分析 ,能夠發現服務的依賴關係:

                  image

點擊 查找  能夠看到具體服務相互調用的數據:

                image

 

PS: 在網上不少出現依賴找不到, 或者是WEB UI查看不到依賴分析等狀況, 請按本文章對應代碼參考, 歡迎評論;

 

源碼下載:

標籤 9-1

https://github.com/eddie-code/SpringCloudDemo

相關文章
相關標籤/搜索