亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot集成Zipkin實(shí)現(xiàn)分布式全鏈路監(jiān)控

 更新時(shí)間:2019年09月11日 08:58:00   作者:海向  
這篇文章主要介紹了SpringBoot集成Zipkin實(shí)現(xiàn)分布式全鏈路監(jiān)控的方法啊,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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是一種分布式跟蹤系統(tǒng)。 它有助于收集解決服務(wù)體系結(jié)構(gòu)中的延遲問題所需的計(jì)時(shí)數(shù)據(jù)。 功能包括收集和查找此數(shù)據(jù)。

Zipkin是Twitter基于google的分布式監(jiān)控系統(tǒng)Dapper(論文)的開發(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)用程序需要“檢測”以向Zipkin報(bào)告跟蹤數(shù)據(jù)。 這通常意味著配置跟蹤器或檢測庫。 向Zipkin報(bào)告數(shù)據(jù)的最常用方法是通過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

版本說明

框架組件 Version
springboot 2.1.6.RELEASE
zipkin 3.9.0

項(xiàng)目結(jié)構(gòu)

項(xiàng)目采用父工程集成多模塊的方式構(gòu)建而成,demo-zipkin 父工程聚合了zipkin-1、zipkin-2zipkin-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.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收集器

設(shè)置收集器的詳細(xì)參數(shù),包含超時(shí)時(shí)間、上傳span間隔、以及配置采集率等,進(jìn)而對收集器進(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ù)端收到請求和服務(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 返回請求實(shí)例
 */
 @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ā)送請求進(jìn)行測試

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)行分析

在地址欄請求urlhttp://localhost:8081/zipkin/service1,然后訪問http://localhost:9411/zipkin/

查看服務(wù)調(diào)用耗時(shí)


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


核心概念

通過上圖可以了解到共有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ù):用于跟蹤樹中節(jié)點(diǎn)的關(guān)聯(lián)和界面展示,包括traceId、spanId、parentId、name、timestamp和duration,其中parentId為null的Span將成為跟蹤樹的根節(jié)點(diǎn)來展示,當(dāng)然它也是調(diào)用鏈的起點(diǎn),為了節(jié)省一次spanId的創(chuàng)建開銷,讓頂級Span變得更明顯,頂級Span中spanId將會(huì)和traceId相同。timestamp用于記錄調(diào)用的起始時(shí)間,而duration表示此次調(diào)用的總耗時(shí),所以timestamp+duration將表示成調(diào)用的結(jié)束時(shí)間,而duration在跟蹤樹中將表示成該Span的時(shí)間條的長度。需要注意的是,這里的name用于在跟蹤樹節(jié)點(diǎn)的時(shí)間條上展示。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實(shí)現(xiàn)分布式全鏈路監(jiān)控,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Nacos負(fù)載均衡策略總結(jié)

    Nacos負(fù)載均衡策略總結(jié)

    Nacos 作為目前主流的微服務(wù)中間件,包含了兩個(gè)頂級的微服務(wù)功能:配置中心和注冊中心,本文給大家總結(jié)了幾種Nacos負(fù)載均衡策略,通過圖文結(jié)合介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • springboot整合logback實(shí)現(xiàn)日志管理操作

    springboot整合logback實(shí)現(xiàn)日志管理操作

    本章節(jié)是記錄logback在springboot項(xiàng)目中的簡單使用,本文將會(huì)演示如何通過logback將日志記錄到日志文件或輸出到控制臺等管理操作,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(47)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(47)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-08-08
  • Git工具 conflict沖突問題解決方案

    Git工具 conflict沖突問題解決方案

    這篇文章主要介紹了Git工具 conflict沖突問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 散列表的原理與Java實(shí)現(xiàn)方法詳解

    散列表的原理與Java實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了散列表的原理與Java實(shí)現(xiàn)方法,詳細(xì)分析了散列表的原理,并結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)散列表相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • Java消息隊(duì)列JMS實(shí)現(xiàn)原理解析

    Java消息隊(duì)列JMS實(shí)現(xiàn)原理解析

    這篇文章主要介紹了Java消息隊(duì)列JMS實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 詳解SpringBoot通過restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)

    詳解SpringBoot通過restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)

    本篇文章主要介紹了詳解使用RestTemplate消費(fèi)spring boot的Restful服務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Spring容器獲取Bean的9種方式總結(jié)

    Spring容器獲取Bean的9種方式總結(jié)

    本文為大家整理匯總了常見的獲取Bean的方式,并提供一些優(yōu)劣分析,方便大家在使用到時(shí)有更好的選擇,同時(shí),也會(huì)為大家適當(dāng)?shù)钠占昂屯卣挂恍┫嚓P(guān)知識,需要的可以參考一下
    2023-07-07
  • Java集合List相關(guān)面試題整理大全

    Java集合List相關(guān)面試題整理大全

    這篇文章主要給大家介紹了關(guān)于Java集合List相關(guān)面試題整理的相關(guān)資料,下面將提供一些常見的Java集合類面試題及其解答,幫助讀者更好地準(zhǔn)備面試,需要的朋友可以參考下
    2024-01-01
  • 使用Spring Security OAuth2實(shí)現(xiàn)單點(diǎn)登錄

    使用Spring Security OAuth2實(shí)現(xiàn)單點(diǎn)登錄

    在本教程中,我們將討論如何使用Spring Security OAuth和Spring Boot實(shí)現(xiàn)SSO - 單點(diǎn)登錄。感興趣的朋友跟隨小編一起看看吧
    2019-06-06

最新評論