簡述
- 使用 spring cloud 用到最多的是各種rest服務調用,Twitter的Zipkin 是一種實現分布式跟蹤解決方案,Sleuth則是用來共方便的集成Zipkin。
調用跟蹤系統的業務場景
- 隨著服務的拆分,系統的模塊變得越來越多,不同的模塊可能由不同的團隊維護,一個請求可能會涉及到幾十個服務的協同處理, 牽扯到多個團隊的業務系統,那么如何快速準確的定位到線上故障?比較成熟的解決方案是通過調用鏈的方式,把一次請求調用過程完整的串聯起來,這樣就實現了對請求調用路徑的監控。
- 故障快速定位
- 通過調用鏈跟蹤,一次請求的邏輯軌跡可以用完整清晰的展示出來。 開發中可以在業務日志中添加調用鏈ID,可以通過調用鏈結合業務日志快速定位錯誤信息。
- 各個調用環節的性能分析
- 在調用鏈的各個環節分別添加調用時延,可以分析系統的性能瓶頸,進行針對性的優化。
- 各個調用環節的可用性,持久層依賴等
- 通過分析各個環節的平均時延,QPS等信息,可以找到系統的薄弱環節,對一些模塊做調整,如數據冗余等。
- 數據分析等
- 調用鏈是一條完整的業務日志,可以得到用戶的行為路徑,匯總分析應用在很多業務場景。
Spring Cloud Sleuth
- Spring Cloud Sleuth為Spring Cloud實現分布式跟蹤解決方案。
Zipkin
- 為分布式鏈路調用監控系統,聚合各業務系統調用延遲數據,達到鏈路調用監控跟蹤。
使用
- Zipkin可以通過http收集信息,也可以通過mq收集信息
Zipkin server代碼
<?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.xx.xxx.server</groupId><artifactId>zipkin-server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>zipkin-server</name><description>zipkin追蹤服務</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><zipkin.version>2.3.1</zipkin.version><spring-cloud.version>Edgware.SR2</spring-cloud.version></properties><dependencies><dependency><groupId>io.zipkin.java</groupId><artifactId>zipkin-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-sleuth-zipkin-stream</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-stream-rabbit</artifactId></dependency><dependency><groupId>io.zipkin.java</groupId><artifactId>zipkin-autoconfigure-ui</artifactId><scope>runtime</scope></dependency><dependency><groupId>io.zipkin.java</groupId><artifactId>zipkin-autoconfigure-storage-mysql</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-sleuth-core</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><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></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.4.RELEASE</version><type>pom</type><scope>import</scope></dependency><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
server:port: 9411
spring:application:name: zipkin-serversleuth:enabled: false
rabbitmq:host: 172.16.10.71port: 5672username: adminpassword: adminlistener:simple:concurrency: 1max-concurrency: 10acknowledge-mode: auto
datasource:name: zipkindriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/zipkin?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=falseusername: rootpassword: 123456schema: classpath:/mysql.sqlinitialize: truecontinue-on-error: true
zipkin:storage:type: mysql
@SpringBootApplication
@EnableZipkinStreamServer
public class ZipkinServerApplication{public static void main(String[] args) {new SpringApplicationBuilder(ZipkinServerApplication.class).run(args);System.out.println("Server start succ");}
}
client 代碼
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-sleuth-core</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
#spring
spring:zipkin:enabled: true# zipkin server地址,如果使用mq收集,這個就不要配置base-url: http:# 應采樣的請求百分比。例如1.0= 100%的請求應該被抽樣。精度僅為全數(即不支持0.1%的痕跡)。開發過程使用1.0,生產根據實際情況設置sleuth:sampler:percentage: 1.0rabbitmq:host: 172.16.10.71port: 5672username: adminpassword: adminlistener:simple:concurrency: 1max-concurrency: 10acknowledge-mode: auto
先啟動server,然后啟動應用服務,然后在應用服務上調用其他服務
打開server中zipkin監控面板:http://localhost:9411