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.
公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出海北州免費做網(wǎng)站回饋大家。
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)中的延遲問題所需的計時數(shù)據(jù)。 功能包括收集和查找此數(shù)據(jù)。
Zipkin是Twitter基于google的分布式監(jiān)控系統(tǒng)Dapper(論文)的開發(fā)源實現(xiàn),zipkin用于跟蹤分布式服務(wù)之間的應(yīng)用數(shù)據(jù)鏈路,分析處理延時,幫助我們改進系統(tǒng)的性能和定位故障。Dapper論文地址
如果日志文件中有跟蹤ID,則可以直接跳轉(zhuǎn)到該文件。 否則,你可以根據(jù)服務(wù),操作名稱,tag標(biāo)簽和持續(xù)時間等屬性進行查詢。 將為您總結(jié)一些有趣的數(shù)據(jù),例如在服務(wù)中花費的時間占比,以及操作是否失敗。
應(yīng)用程序需要“檢測”以向Zipkin報告跟蹤數(shù)據(jù)。 這通常意味著配置跟蹤器或檢測庫。 向Zipkin報告數(shù)據(jù)的最常用方法是通過http或Kafka,盡管存在許多其他選項,例如Apache ActiveMQ,gRPC和RabbitMQ。 提供給UI的數(shù)據(jù)存儲在內(nèi)存中,或者持久存儲在受支持的后端(如Apache Cassandra或Elasticsearch)中。
本示例中是使用Zipkin中集成的http組件進行發(fā)送Span數(shù)據(jù)。
安裝啟動 zipkin
https://github.com/openzipkin/zipkin 中下載 zipkin.jar
java -jar zipkin.jar
版本說明
框架組件 | Version |
---|---|
springboot | 2.1.6.RELEASE |
zipkin | 3.9.0 |
項目結(jié)構(gòu)
項目采用父工程集成多模塊的方式構(gòu)建而成,demo-zipkin
父工程聚合了zipkin-1
、zipkin-2
、zipkin-3
、zipkin-4
、zipkin-5
五個 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
工程端口分配
每個 Module 使用不同的端口,分別啟動自己的Application。
Module名稱 | 端口 | Application |
---|---|---|
zipkin-1 | 8081 | Application1 |
zipkin-2 | 8082 | Application2 |
zipkin-3 | 8083 | Application3 |
zipkin-4 | 8084 | Application4 |
zipkin-5 | 8085 | Application5 |
引入 Maven 依賴
3.9.0 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web io.zipkin.brave brave-core ${zipkin.version} io.zipkin.brave brave-spancollector-http ${zipkin.version} io.zipkin.brave brave-web-servlet-filter ${zipkin.version} io.zipkin.brave brave-apache-http-interceptors ${zipkin.version} io.zipkin.brave brave-okhttp ${zipkin.version}
配置文件、收集器的設(shè)置
配置 application.properties,以 zipkin-1
為例,其他工程中配置時將 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ù),包含超時時間、上傳span間隔、以及配置采集率等,進而對收集器進行初始化。
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; /* 連接超時時間 */ @Value("${zipkin.connectTimeout}") private int connecTimeout; /* 是否啟動壓縮 */ @Value("${zipkin.compressionEnabled}") private boolean compressionEnabled; /* 上傳 span 的間隔時間 */ @Value("${zipkin.flushInterval}") private int flushInterval; /* 讀取超時時間 */ @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ù)端收到請求和服務(wù)端完成處理,并將結(jié)果發(fā)送給客戶端)過濾器 * @Param: * @return: 過濾器 */ @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 返回請求實例 */ @Bean public OkHttpClient okHttpClient(Brave brave) { OkHttpClient httpClient = new OkHttpClient.Builder() .addInterceptor(new BraveOkHttpRequestResponseInterceptor( brave.clientRequestInterceptor(), brave.clientResponseInterceptor(), new DefaultSpanNameProvider())).build(); return httpClient; } }
編寫 Controller 發(fā)送請求進行測試
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 啟動類
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); } }
運行分析
在地址欄請求urlhttp://localhost:8081/zipkin/service1
,然后訪問http://localhost:9411/zipkin/
查看服務(wù)調(diào)用耗時
查看服務(wù)依賴關(guān)系
核心概念
通過上圖可以了解到共有7個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文件中選取兩個子集進行分析
基本數(shù)據(jù):用于跟蹤樹中節(jié)點的關(guān)聯(lián)和界面展示,包括traceId、spanId、parentId、name、timestamp和duration,其中parentId為null的Span將成為跟蹤樹的根節(jié)點來展示,當(dāng)然它也是調(diào)用鏈的起點,為了節(jié)省一次spanId的創(chuàng)建開銷,讓頂級Span變得更明顯,頂級Span中spanId將會和traceId相同。timestamp用于記錄調(diào)用的起始時間,而duration表示此次調(diào)用的總耗時,所以timestamp+duration將表示成調(diào)用的結(jié)束時間,而duration在跟蹤樹中將表示成該Span的時間條的長度。需要注意的是,這里的name用于在跟蹤樹節(jié)點的時間條上展示。traceId:標(biāo)記一次請求的跟蹤,相關(guān)的Spans都有相同的traceId。kind :zipkin最新V2版本的API中,不再要求在annotations中上傳cs,cr,sr,ss。而是通過kind標(biāo)記是server-side span還是client-side span,兩端記錄自己的timestap來取代cs和sr,記錄duration來取代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實現(xiàn)分布式全鏈路監(jiān)控,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!