中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot集成Zipkin實現分布式全鏈路監控

發布時間:2020-10-09 08:41:55 來源:腳本之家 閱讀:287 作者:海向 欄目:編程語言

Zipkin 簡介

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是一種分布式跟蹤系統。 它有助于收集解決服務體系結構中的延遲問題所需的計時數據。 功能包括收集和查找此數據。

Zipkin是Twitter基于google的分布式監控系統Dapper(論文)的開發源實現,zipkin用于跟蹤分布式服務之間的應用數據鏈路,分析處理延時,幫助我們改進系統的性能和定位故障。Dapper論文地址

如果日志文件中有跟蹤ID,則可以直接跳轉到該文件。 否則,你可以根據服務,操作名稱,tag標簽和持續時間等屬性進行查詢。 將為您總結一些有趣的數據,例如在服務中花費的時間占比,以及操作是否失敗。

應用程序需要“檢測”以向Zipkin報告跟蹤數據。 這通常意味著配置跟蹤器或檢測庫。 向Zipkin報告數據的最常用方法是通過http或Kafka,盡管存在許多其他選項,例如Apache ActiveMQ,gRPC和RabbitMQ。 提供給UI的數據存儲在內存中,或者持久存儲在受支持的后端(如Apache Cassandra或Elasticsearch)中。

本示例中是使用Zipkin中集成的http組件進行發送Span數據。

Springboot 集成 Zipkin

安裝啟動 zipkin

https://github.com/openzipkin/zipkin 中下載 zipkin.jar

java -jar zipkin.jar

版本說明

框架組件 Version
springboot 2.1.6.RELEASE
zipkin 3.9.0

項目結構

項目采用父工程集成多模塊的方式構建而成,demo-zipkin 父工程聚合了zipkin-1zipkin-2zipkin-3zipkin-4zipkin-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 依賴

<properties>
 <zipkin.version>3.9.0</zipkin.version>
</properties>

<!-- Springboot 相關 -->
<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相關 -->
<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>

配置文件、收集器的設置

配置 application.properties,以 zipkin-1 為例,其他工程中配置時將 zipkin.serviceNameserver.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收集器

設置收集器的詳細參數,包含超時時間、上傳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: 設置server的(服務端收到請求和服務端完成處理,并將結果發送給客戶端)過濾器
 * @Param:
 * @return: 過濾器
 */
 @Bean
 public BraveServletFilter braveServletFilter(Brave brave) {
 BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),
  brave.serverResponseInterceptor(), new DefaultSpanNameProvider());
 return filter;
 }

 /**
 * @Description: 設置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 發送請求進行測試

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/

查看服務調用耗時


SpringBoot集成Zipkin實現分布式全鏈路監控

查看服務依賴關系


SpringBoot集成Zipkin實現分布式全鏈路監控

核心概念

通過上圖可以了解到共有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文件中選取兩個子集進行分析

基本數據:用于跟蹤樹中節點的關聯和界面展示,包括traceId、spanId、parentId、name、timestamp和duration,其中parentId為null的Span將成為跟蹤樹的根節點來展示,當然它也是調用鏈的起點,為了節省一次spanId的創建開銷,讓頂級Span變得更明顯,頂級Span中spanId將會和traceId相同。timestamp用于記錄調用的起始時間,而duration表示此次調用的總耗時,所以timestamp+duration將表示成調用的結束時間,而duration在跟蹤樹中將表示成該Span的時間條的長度。需要注意的是,這里的name用于在跟蹤樹節點的時間條上展示。traceId:標記一次請求的跟蹤,相關的Spans都有相同的traceId。kind :zipkin最新V2版本的API中,不再要求在annotations中上傳cs,cr,sr,ss。而是通過kind標記是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"
 }
 }

總結

以上所述是小編給大家介紹的SpringBoot集成Zipkin實現分布式全鏈路監控,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

华阴市| 麦盖提县| 合山市| 上高县| 佛坪县| 阳新县| 吴川市| 宁国市| 宁武县| 麟游县| 上蔡县| 汉沽区| 西峡县| 河东区| 金乡县| 邢台市| 安达市| 鄂州市| 陇川县| 沙湾县| 香河县| 兰西县| 依安县| 湖南省| 滁州市| 德兴市| 友谊县| 唐海县| 静安区| 泽普县| 巴南区| 婺源县| 井陉县| 江川县| 太和县| 育儿| 西昌市| 昌邑市| 蕲春县| 台北县| 遵义市|