SpringBoot集成Zipkin實(shí)現(xiàn)分布式全鏈路監(jiān)控
Zipkin 簡(jiǎn)介
Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in service architectures. Features include both the collection and lookup of this data.
If you have a trace ID in a log file, you can jump directly to it. Otherwise, you can query based on attributes such as service, operation name, tags and duration. Some interesting data will be summarized for you, such as the percentage of time spent in a service, and whether or not operations failed.
Application's need to be “instrumented” to report trace data to Zipkin. This usually means configuration of a tracer or instrumentation library. The most popular ways to report data to Zipkin are via http or Kafka, though many other options exist, such as Apache ActiveMQ, gRPC and RabbitMQ. The data served to the UI is stored in-memory, or persistently with a supported backend such as Apache Cassandra or Elasticsearch.
Zipkin是一種分布式跟蹤系統(tǒng)。 它有助于收集解決服務(wù)體系結(jié)構(gòu)中的延遲問(wèn)題所需的計(jì)時(shí)數(shù)據(jù)。 功能包括收集和查找此數(shù)據(jù)。
Zipkin是Twitter基于google的分布式監(jiān)控系統(tǒng)Dapper(論文)的開(kāi)發(fā)源實(shí)現(xiàn),zipkin用于跟蹤分布式服務(wù)之間的應(yīng)用數(shù)據(jù)鏈路,分析處理延時(shí),幫助我們改進(jìn)系統(tǒng)的性能和定位故障。Dapper論文地址
如果日志文件中有跟蹤ID,則可以直接跳轉(zhuǎn)到該文件。 否則,你可以根據(jù)服務(wù),操作名稱,tag標(biāo)簽和持續(xù)時(shí)間等屬性進(jìn)行查詢。 將為您總結(jié)一些有趣的數(shù)據(jù),例如在服務(wù)中花費(fèi)的時(shí)間占比,以及操作是否失敗。
應(yīng)用程序需要“檢測(cè)”以向Zipkin報(bào)告跟蹤數(shù)據(jù)。 這通常意味著配置跟蹤器或檢測(cè)庫(kù)。 向Zipkin報(bào)告數(shù)據(jù)的最常用方法是通過(guò)http或Kafka,盡管存在許多其他選項(xiàng),例如Apache ActiveMQ,gRPC和RabbitMQ。 提供給UI的數(shù)據(jù)存儲(chǔ)在內(nèi)存中,或者持久存儲(chǔ)在受支持的后端(如Apache Cassandra或Elasticsearch)中。
本示例中是使用Zipkin中集成的http組件進(jìn)行發(fā)送Span數(shù)據(jù)。
Springboot 集成 Zipkin
安裝啟動(dòng) zipkin
https://github.com/openzipkin/zipkin 中下載 zipkin.jar
java -jar zipkin.jar
版本說(shuō)明
| 框架組件 | Version |
|---|---|
| springboot | 2.1.6.RELEASE |
| zipkin | 3.9.0 |
項(xiàng)目結(jié)構(gòu)
項(xiàng)目采用父工程集成多模塊的方式構(gòu)建而成,demo-zipkin 父工程聚合了zipkin-1、zipkin-2、zipkin-3、zipkin-4、zipkin-5 五個(gè) Module。
demo-zipkin zipkin-1 |-SpanCollectorConfig |-application.properties |-ZipkinController |-Application1 zipkin-2 |-SpanCollectorConfig |-application.properties |-ZipkinController2 |-Application1 zipkin-3 |-SpanCollectorConfig |-application.properties |-ZipkinController3 |-Application1 zipkin-4 |-SpanCollectorConfig |-application.properties |-ZipkinController4 |-Application1 zipkin-5 |-SpanCollectorConfig |-application.properties |-ZipkinController5 |-Application1
工程端口分配
每個(gè) Module 使用不同的端口,分別啟動(dòng)自己的Application。
| Module名稱 | 端口 | Application |
|---|---|---|
| zipkin-1 | 8081 | Application1 |
| zipkin-2 | 8082 | Application2 |
| zipkin-3 | 8083 | Application3 |
| zipkin-4 | 8084 | Application4 |
| zipkin-5 | 8085 | Application5 |
引入 Maven 依賴
<properties>
<zipkin.version>3.9.0</zipkin.version>
</properties>
<!-- Springboot 相關(guān) -->
<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>
<!-- zipkin相關(guān) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-core</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-spancollector-http</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-web-servlet-filter</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-apache-http-interceptors</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-okhttp</artifactId>
<version>${zipkin.version}</version>
</dependency>
配置文件、收集器的設(shè)置
配置 application.properties,以 zipkin-1 為例,其他工程中配置時(shí)將 zipkin.serviceName與server.port更改為 myzipkin-2 …與8081...等即可
zipkin.serviceName=myzipkin-1 zipkin.url=http://localhost:9411 zipkin.connectTimeout=6000 zipkin.readTimeout=6000 zipkin.flushInterval=1 zipkin.compressionEnabled=true zipkin.samplerRate=1 server.port=8081 server.servlet.context-path=/
配置Span收集器
設(shè)置收集器的詳細(xì)參數(shù),包含超時(shí)時(shí)間、上傳span間隔、以及配置采集率等,進(jìn)而對(duì)收集器進(jìn)行初始化。
package com.anqi.zipkin.bean;
import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.Brave.Builder;
import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler;
import com.github.kristofa.brave.Sampler;
import com.github.kristofa.brave.SpanCollector;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.HttpSpanCollector;
import com.github.kristofa.brave.http.HttpSpanCollector.Config;
import com.github.kristofa.brave.okhttp.BraveOkHttpRequestResponseInterceptor;
import com.github.kristofa.brave.servlet.BraveServletFilter;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpanCollectorConfig {
@Value("${zipkin.url}")
private String url;
@Value("${zipkin.serviceName}")
private String serviceName;
/*
連接超時(shí)時(shí)間
*/
@Value("${zipkin.connectTimeout}")
private int connecTimeout;
/*
是否啟動(dòng)壓縮
*/
@Value("${zipkin.compressionEnabled}")
private boolean compressionEnabled;
/*
上傳 span 的間隔時(shí)間
*/
@Value("${zipkin.flushInterval}")
private int flushInterval;
/*
讀取超時(shí)時(shí)間
*/
@Value("${zipkin.readTimeout}")
private int readTimeout;
@Value("${zipkin.samplerRate}")
private float samplerRate;
/**
* 配置 span 收集器
* @return
*/
@Bean
public SpanCollector spanCollector() {
Config config = Config.builder()
.connectTimeout(connecTimeout)
.compressionEnabled(compressionEnabled)
.flushInterval(flushInterval)
.readTimeout(readTimeout)
.build();
return HttpSpanCollector.create(url, config, new EmptySpanCollectorMetricsHandler());
}
/**
* 配置采集率
* @param spanCollector
* @return
*/
@Bean
public Brave brave(SpanCollector spanCollector) {
Builder builder = new Builder(serviceName);
builder.spanCollector(spanCollector)
.traceSampler(Sampler.create(samplerRate))
.build();
return builder.build();
}
/**
* @Description: 設(shè)置server的(服務(wù)端收到請(qǐng)求和服務(wù)端完成處理,并將結(jié)果發(fā)送給客戶端)過(guò)濾器
* @Param:
* @return: 過(guò)濾器
*/
@Bean
public BraveServletFilter braveServletFilter(Brave brave) {
BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),
brave.serverResponseInterceptor(), new DefaultSpanNameProvider());
return filter;
}
/**
* @Description: 設(shè)置client的 rs和cs的攔截器
* @Param:
* @return: OkHttpClient 返回請(qǐng)求實(shí)例
*/
@Bean
public OkHttpClient okHttpClient(Brave brave) {
OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(new BraveOkHttpRequestResponseInterceptor(
brave.clientRequestInterceptor(),
brave.clientResponseInterceptor(),
new DefaultSpanNameProvider())).build();
return httpClient;
}
}
編寫(xiě) Controller 發(fā)送請(qǐng)求進(jìn)行測(cè)試
Zipkin-1中的Controller
package com.anqi.zipkin.controller;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("zipkin")
public class ZipkinController {
public static final String url = "http://localhost:8082/zipkin/service2";
@Autowired
OkHttpClient client;
@GetMapping("/service1")
public String service() {
Request request = new Request.Builder().url(url).build();
Response response;
try {
response = client.newCall(request).execute();
return response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
return "null";
}
}
Zipkin-2中的Controller
package com.anqi.zipkin.controller;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("zipkin")
public class ZipkinController2 {
public static final String url = "http://localhost:8083/zipkin/service3";
public static final String url2 = "http://localhost:8084/zipkin/service4";
@Autowired
OkHttpClient client;
@GetMapping("/service2")
public String service() throws Exception {
System.out.println("loading-----");
Request request1 = new Request.Builder().url(url).build();
Request request2 = new Request.Builder().url(url2).build();
Response response1 = client.newCall(request1).execute();
Response response2 = client.newCall(request2).execute();
return "con2 + "+ response1.body().string() + "-" + response2.body().string();
}
}
Zipkin-3中的Controller
package com.anqi.zipkin.controller;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("zipkin")
public class ZipkinController3 {
public static final String url = "http://localhost:8084/zipkin/service4";
@Autowired
OkHttpClient client;
@GetMapping("/service3")
public String service() throws Exception {
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
return "con3 + "+ response.body().string();
}
}
Zipkin-4中的Controller
package com.anqi.zipkin.controller;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("zipkin")
public class ZipkinController4 {
public static final String url = "http://localhost:8085/zipkin/service5";
@Autowired
OkHttpClient client;
@GetMapping("/service4")
public String service() throws Exception {
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
return "con4 + "+ response.body().string();
}
}
Zipkin-5中的Controller
package com.anqi.zipkin.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("zipkin")
public class ZipkinController5 {
@GetMapping("/service5")
public String service() throws Exception {
return "service5 -----";
}
}
Springboot 啟動(dòng)類
package com.anqi.zipkin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application1 {
public static void main(String[] args) {
SpringApplication.run(Application1.class);
}
}
運(yùn)行分析
在地址欄請(qǐng)求urlhttp://localhost:8081/zipkin/service1,然后訪問(wèn)http://localhost:9411/zipkin/
查看服務(wù)調(diào)用耗時(shí)

查看服務(wù)依賴關(guān)系

核心概念
通過(guò)上圖可以了解到共有7個(gè)span,分別為
{
"zipkin-1":["Server Start", "Server Finish"],
"myzipkin-1,myzipkin-2":
["Client Start", "Server Start", "Client Finish", "Server Finish"],
"myzipkin-2,myzipkin-3":
["Client Start", "Server Start", "Client Finish", "Server Finish"],
"myzipkin-3,myzipkin-4":
["Client Start", "Server Start", "Client Finish", "Server Finish"],
"myzipkin-4,myzipkin-5":
["Client Start", "Server Start", "Client Finish", "Server Finish"],
"myzipkin-2,myzipkin-4":
["Client Start", "Server Start", "Client Finish", "Server Finish"],
"myzipkin-4,myzipkin-5":
["Client Start", "Server Start", "Client Finish", "Server Finish"],
}
在json文件中選取兩個(gè)子集進(jìn)行分析
基本數(shù)據(jù):用于跟蹤樹(shù)中節(jié)點(diǎn)的關(guān)聯(lián)和界面展示,包括traceId、spanId、parentId、name、timestamp和duration,其中parentId為null的Span將成為跟蹤樹(shù)的根節(jié)點(diǎn)來(lái)展示,當(dāng)然它也是調(diào)用鏈的起點(diǎn),為了節(jié)省一次spanId的創(chuàng)建開(kāi)銷,讓頂級(jí)Span變得更明顯,頂級(jí)Span中spanId將會(huì)和traceId相同。timestamp用于記錄調(diào)用的起始時(shí)間,而duration表示此次調(diào)用的總耗時(shí),所以timestamp+duration將表示成調(diào)用的結(jié)束時(shí)間,而duration在跟蹤樹(shù)中將表示成該Span的時(shí)間條的長(zhǎng)度。需要注意的是,這里的name用于在跟蹤樹(shù)節(jié)點(diǎn)的時(shí)間條上展示。traceId:標(biāo)記一次請(qǐng)求的跟蹤,相關(guān)的Spans都有相同的traceId。kind :zipkin最新V2版本的API中,不再要求在annotations中上傳cs,cr,sr,ss。而是通過(guò)kind標(biāo)記是server-side span還是client-side span,兩端記錄自己的timestap來(lái)取代cs和sr,記錄duration來(lái)取代cr和ss
{
"traceId": "867a9e3867736b17",
"parentId": "96f19423db38c90d",
"id": "6c9fd521b6589b7f",
"kind": "SERVER",
"name": "get",
"timestamp": 1568103422569000,
"duration": 6000,
"localEndpoint": {
"serviceName": "myzipkin-4",
"ipv4": "192.168.1.160"
},
"tags": {
"http.status_code": "200",
"http.url": "/zipkin/service4"
}
},
{
"traceId": "867a9e3867736b17",
"parentId": "867a9e3867736b17",
"id": "96f19423db38c90d",
"kind": "CLIENT",
"name": "get",
"timestamp": 1568103422447000,
"duration": 139000,
"localEndpoint": {
"serviceName": "myzipkin-1",
"ipv4": "192.168.1.160"
},
"tags": {
"http.url": "http://localhost:8082/zipkin/service2"
}
}
總結(jié)
以上所述是小編給大家介紹的SpringBoot集成Zipkin實(shí)現(xiàn)分布式全鏈路監(jiān)控,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Spring Boot集成mongodb數(shù)據(jù)庫(kù)過(guò)程解析
這篇文章主要介紹了Spring Boot集成mongodb數(shù)據(jù)庫(kù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
java中實(shí)現(xiàn)創(chuàng)建目錄與創(chuàng)建文件的操作實(shí)例
用Java創(chuàng)建文件或目錄非常簡(jiǎn)單,下面這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)創(chuàng)建目錄與創(chuàng)建文件的操作實(shí)例,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
java實(shí)例方法被覆蓋,靜態(tài)方法被隱藏Explain(詳解)
下面小編就為大家?guī)?lái)一篇java實(shí)例方法被覆蓋,靜態(tài)方法被隱藏Explain(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
SpringBoot集成Redis實(shí)現(xiàn)驗(yàn)證碼的簡(jiǎn)單案例
本文主要介紹了SpringBoot集成Redis實(shí)現(xiàn)驗(yàn)證碼的簡(jiǎn)單案例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Java鏈表元素查找實(shí)現(xiàn)原理實(shí)例解析
這篇文章主要介紹了Java鏈表元素查找實(shí)現(xiàn)原理實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
JAVA簡(jiǎn)單鏈接Oracle數(shù)據(jù)庫(kù) 注冊(cè)和登陸功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了JAVA鏈接Oracle并實(shí)現(xiàn)注冊(cè)與登錄功能的代碼實(shí)例,有需要的朋友可以參考一下2014-01-01
利用Java實(shí)現(xiàn)更改Word中的頁(yè)面大小和頁(yè)面方向
這篇文章主要為大家詳細(xì)介紹了一種高效便捷的方法——通過(guò)Java應(yīng)用程序,以編程方式更改Word中的頁(yè)面大小和頁(yè)面方向,感興趣的可以了解一下2023-03-03
IDEA創(chuàng)建SpringBoot父子Module項(xiàng)目的實(shí)現(xiàn)
本文主要介紹了IDEA創(chuàng)建SpringBoot父子Module項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05

