Zipkin是一個開源的分佈式實時數據追蹤系統(Distributed Tracking System),每個Service向Zipkin報告請求數據,Zipkin會根據調用關係通過Zipkin UI生成依賴關係圖,讓開發者通過一個Web前端輕鬆的收集和分析數據,如用戶每次請求服務的處理事件等,可方便分析系統中存在的瓶頸。
前言
Zipkin是一個開源的分佈式實時數據追蹤系統(Distributed Tracking System),每個Service向Zipkin報告請求數據,Zipkin會根據調用關係通過Zipkin UI生成依賴關係圖,讓開發者通過一個Web前端輕鬆的收集和分析數據,如用戶每次請求服務的處理事件等,可方便分析系統中存在的瓶頸。
隨着業務越來越複雜,系統也隨之需要進行拆分,特別是隨着微服務架構和容器技術的興起,看似簡單的應用,後端可能需要多個Service的支持。當前端向後發送請求,後端可能需要進行多次Service調用才能完成,當請求變慢或者不可用時,單憑我們無法得知是哪個後端Service引起的,這時就需要快速定位故障點,Zipkin就可以很好的解決這樣的問題。
這裏我是在Spring Cloud的基礎上搭建的Zipkin。
pom.xml
創建Zipkin微服務,並引入依賴,主要是兩個zipkin-server
和zipkin-autoconfigure-ui
:
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.11.8</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.11.8</version>
</dependency>
需要注意的是:
- 這裏的version如果不匹配會發生各種奇怪的異常導致程序跑不起來,所以如果你的程序報異常,建議你檢查一下zipkin版本以及SpringBoot、SpringCloud的版本;
zipkin-server
中包含了log4j-slf4j-impl
這個組件,這可能會與springboot中的logback
重複產生異常如下,如果出現只需在zipkin-server
的dependency中添加<exclusion>
將log4j-slf4j-impl
排除即可。
[ERROR] [XXX Enforcer Rules] find DuplicateClasses
Found in:
org.apache.logging.log4j:log4j-slf4j-impl:jar:2.6.2:compile
ch.qos.logback:logback-classic:jar:1.1.7:compile
Duplicate classes:
org/slf4j/impl/StaticMDCBinder.class
org/slf4j/impl/StaticMarkerBinder.class
org/slf4j/impl/StaticLoggerBinder.class
另外引入如下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-actuator</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>
配置文件
修改配置文件:
# 將zipkin註冊到erueka上
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
# 配置zipkin端口
server:
port: 8769
# 配置服務名字
spring:
application:
name: spring-cloud-zipkin2
上面就是我們給zipkin的配置,但是若是在啓動過程中報出異常[java.lang.IllegalArgumentException: Prometheus requires that all meters with the same name have the same set of tag keys.]
,此時我們就需要在配置文件中添加:
management:
metrics:
web:
server:
auto-time-requests: false
Application
修改Application添加註解@EnableEurekaClient
以及EnableZipkinServer
。
package com.giotto.demozipkin2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import zipkin2.server.internal.EnableZipkinServer;
@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class DemoZipkin2Application {
public static void main(String[] args) {
SpringApplication.run(DemoZipkin2Application.class, args);
}
}
Service端
當ZipkinServer端配置好後,我們同樣需要在Service中配置Zipkin,這樣才能將數據實時發送到ZipkinServer,首先給需要配置Zipkin的Service添加如下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
在配置文件中,配置Zipkin:
spring:
zipkin:
base-url: http://localhost:port
sleuth:
sampler:
probability: 1.0
運行
啓動後,只需訪問http://localhost:port/zipkin/
即可。