Spring Cloud Sleuth 作爲Spring Cloud 的一個組件,其主要作用是解決分佈式系統當中提供服務鏈路追蹤的。
爲什麼要使用鏈路追蹤?
在微服務系統中,一個來自用戶的請求,請求先達到前端A(如前端界面)然後通過遠程調用,到達系統中間件B,C(負載均衡,網關等),最後達到後端服務D,E,後端經過一系列的業務邏輯最後將數據返回給用戶,對於這樣一個請求,經歷了這麼多個服務,怎麼樣將它的請求過程的數據記錄下來呢?這就需要用到服務鏈路追蹤。
Sleuth基本術語
-
Span:基本工作單元,例如,在一個新建的span中發送一個RPC等同於發送一個迴應請求給RPC,span通過一個64位ID唯一標識,trace以另一個64位ID表示,span還有其他數據信息,比如摘要、時間戳事件、關鍵值註釋(tags)、span的ID、以及進度ID(通常是IP地址)
span在不斷的啓動和停止,同時記錄了時間信息,當你創建了一個span,你必須在未來的某個時刻停止它。
-
Trace:一系列spans組成的一個樹狀結構,例如,如果你正在跑一個分佈式大數據工程,你可能需要創建一個trace。
-
Annotation:用來及時記錄一個事件的存在,一些核心註解用來定義一個請求的開始和結束,註解如下:
cs - Client Sent -客戶端發起一個請求,這個annotion描述了這個span的開始 sr - Server
Received -服務端獲得請求並準備開始處理它,如果將其sr減去cs時間戳便可得到網絡延遲 ss - Server Sent
-註解表明請求處理的完成(當請求返回客戶端),如果ss減去sr時間戳便可得到服務端需要的處理請求時間 cr - Client Received -表明span的結束,客戶端成功接收到服務端的回覆,如果cr減去cs時間戳便可得到客戶端從服務端獲取回覆的所有所需時間
案例構建
這次的搭建案例一共有4個工程,採用Maven工程的多Module形式。 新建一個主Maven工程,。 主工程的pom文件制定Spring Boot版本爲2.0.1,Spring Cloud版本爲Finchley.RELEASE。
工程將會以zipkin-server作爲鏈路追蹤的服務中心,存儲鏈路追蹤數據。以gateway-service作爲服務的網關,負責請求的轉發,同時也會作爲鏈路追蹤的客戶端,負責產生鏈路數據
,並將數據上傳給zipkin-server。eureka-server將會作爲註冊中心,而user-service則會作爲一個服務提供者,對外提供接口,主要負責產生鏈路數據。
1.構建主工程
新建一個Maven項目,刪除掉src文件夾,修改pom文件。
<?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>springcloud</groupId>
<artifactId>sleuth</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>zipkin-server</module>
<module>user-service</module>
<module>gateway-service</module>
<module>eureka-server</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath></relativePath>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.構建ZipKin Server
新建一個Module工程,取名爲zipkin-server,pom文件主要需要Eureka的起步依賴
spring-cloud-starter-netflix-eureka-client、ZipKin的服務及UI依賴zipkin-server,
以及存入es所需依賴
<?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">
<parent>
<artifactId>sleuth</artifactId>
<groupId>springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zipkin-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- zipkin-->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.8.4</version>
</dependency>
<!-- -->
<!--鏈路數據存入elasticsearch中-->
<!-- https://mvnrepository.com/artifact/io.zipkin.java/zipkin -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin</artifactId>
<version>2.8.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.zipkin.java/zipkin-autoconfigure-storage-elasticsearch-http -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
<version>2.8.4</version>
</dependency>
<!-- -->
</dependencies>
</project>
在配置文件application.yml文件添加相關配置,指定程序名,端口號,服務註冊地址,設置zip kin 的存入es當中的相關配置。
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 9411
spring:
application:
name: zipkin-server
#鏈路追蹤的數據存入elasticsearch
zipkin:
storage:
StorageComponent: elasticsearch
elasticsearch:
cluster: elasticsearch
hosts: localhost:9200 #es地址
index: zipkin #索引名稱
index-replicas: 1
index-shards: 5
type: elasticsearch #持久化類型爲es
management:
metrics:
web:
server:
auto-time-requests: false
在zipkin-server的啓動類中添加相應註解,@EnableZipkinServer開啓zipkin的服務端功能,@EnableEurekaClient開啓Eureka的客戶端功能。
package com.zipkin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import zipkin.server.EnableZipkinServer;
@SpringBootApplication
@EnableZipkinServer
@EnableEurekaClient
public class ZipKinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipKinApplication.class,args);
}
}
3.構建User Service
在主 Maven 工程下建 Module 工程,取名爲 user-service ,作爲服務提供者,對外暴露API 接口。 user-service 工程 porn 文件繼承了主 Maven 工程的 porn 文件,並引入了 Eureka的起步依賴spring-cloud-starter-netflix-eureka-client,Web 步依賴 spring-boot-starter-web ,以 Zipkin 的起步依賴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">
<parent>
<artifactId>sleuth</artifactId>
<groupId>springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加sleuth全鏈路追蹤監控依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
</project>
添加user service的配置文件application.yml文件,指定程序名,端口號,服務註冊的地址以及將數據傳輸概率設置爲100%上傳給zip server。
server:
port: 8762
spring:
application:
name: user-service
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
percentage: 1.0 # 100%概率將鏈路的數據上傳給ZipKin Server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
在user service的啓動類上添加@EnableEurekaClient註解,開啓Eureka Client功能。
package com.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
新增一個UserController作爲暴露出去的API接口
package com.user.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class UserController {
@RequestMapping("hi")
public String hi(){
return "user-service 調用成功!";
}
}
4.構建Gateway-Service
新建一個名爲 gateway-service 工程,這個工程作爲服務網關,將請求轉發到 user-service,並且作爲 zipkin客戶端 需要將鏈路數據上傳給 Zipkin Server ,同時它也作爲 Eureka Client 。
<?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">
<parent>
<artifactId>sleuth</artifactId>
<groupId>springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.zuul</groupId>
<artifactId>zuul-core</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 添加sleuth全鏈路追蹤監控依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
</project>
在程序的配置文件application.yml添加相應的配置。
server:
port: 5000
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: gateway-service
sleuth:
sampler:
percentage: 1.0 #數據百分百概率上傳
zipkin:
base-url: http://localhost:9411 #zipkin地址
zuul:
routes:
api-a:
path: /user-api/**
serviceId: user-service
在服務的啓動類上添加相應的註解,開啓Zuul的代理以及Eureka Client
package com.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
//開啓zuul代理功能
@EnableZuulProxy
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
5.搭建Eureka Server
繼續新建一個Maven項目,名字叫做eureka-server,作爲註冊中心。配置文件只需要添加Eureka Server端依賴,由於是這個版本的Spring Cloud 還需要添加熔斷器的依賴。
<?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">
<parent>
<artifactId>sleuth</artifactId>
<groupId>springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>
添加eureka-server的application.yml配置文件。
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
#下面兩個配置是爲了防止eureka自己註冊自己
register-with-eureka: false
fetch-registry: false
最後在eureka-server的啓動類上添加相應註解,開啓Eureka Server功能。
package com.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
6.項目啓動
首先啓動elasticsearch+kibbna,這裏我選擇的都是6.7.0版本,注意了zipkin在存數據的時候是有es版本限制的,低版本的zipkin依賴或許不能鏈接高版本的es,這裏建議大家和我進行統一版本。
還不會es啓動的可以參考我之前的文章,分佈式系統下的日誌框架
啓動好es之後,依次啓動eureka-server,gateway-service,user-service,zipkin-server。 接着通過網關訪問服務,點擊訪問
服務調用成功後zip server就可以查詢到鏈路數據。 現在我們繼續訪問http://localhost:9411,Zipkin的展示頁面,點擊查詢後即可展示數據。這裏我就不在贅述zipkin頁面上的功能了,因爲我寫累了。
至於如何在es上查看索引,仍然查看我之前的文章------》分佈式系統下的日誌框架
7.項目地址
項目我已經上傳到github上了,需要參考的可以點擊這裏。
如果還有不懂或者需要es資源的,可以在我的羣裏一起來討論。
773180314 歡迎你的加入