Spring Cloud Sleuth進(jìn)階實(shí)戰(zhàn)

轉(zhuǎn)載請(qǐng)標(biāo)明出處:
http://blog.csdn.net/forezp/article/details/76795269
本文出自方志朋的博客

為什么需要Spring Cloud Sleuth

微服務(wù)架構(gòu)是一個(gè)分布式架構(gòu)梯投,它按業(yè)務(wù)劃分服務(wù)單元寝贡,一個(gè)分布式系統(tǒng)往往有很多個(gè)服務(wù)單元。由于服務(wù)單元數(shù)量眾多,業(yè)務(wù)的復(fù)雜性捉片,如果出現(xiàn)了錯(cuò)誤和異常晶丘,很難去定位编曼。主要體現(xiàn)在靶剑,一個(gè)請(qǐng)求可能需要調(diào)用很多個(gè)服務(wù)深浮,而內(nèi)部服務(wù)的調(diào)用復(fù)雜性压怠,決定了問(wèn)題難以定位。所以微服務(wù)架構(gòu)中飞苇,必須實(shí)現(xiàn)分布式鏈路追蹤菌瘫,去跟進(jìn)一個(gè)請(qǐng)求到底有哪些服務(wù)參與,參與的順序又是怎樣的布卡,從而達(dá)到每個(gè)請(qǐng)求的步驟清晰可見(jiàn)雨让,出了問(wèn)題,很快定位忿等。

舉個(gè)例子栖忠,在微服務(wù)系統(tǒng)中,一個(gè)來(lái)自用戶的請(qǐng)求,請(qǐng)求先達(dá)到前端A(如前端界面)娃闲,然后通過(guò)遠(yuǎn)程調(diào)用虚汛,達(dá)到系統(tǒng)的中間件B、C(如負(fù)載均衡皇帮、網(wǎng)關(guān)等)卷哩,最后達(dá)到后端服務(wù)D、E属拾,后端經(jīng)過(guò)一系列的業(yè)務(wù)邏輯計(jì)算最后將數(shù)據(jù)返回給用戶将谊。對(duì)于這樣一個(gè)請(qǐng)求,經(jīng)歷了這么多個(gè)服務(wù)渐白,怎么樣將它的請(qǐng)求過(guò)程的數(shù)據(jù)記錄下來(lái)呢尊浓?這就需要用到服務(wù)鏈路追蹤。

Google開(kāi)源的 Dapper鏈路追蹤組件纯衍,并在2010年發(fā)表了論文《Dapper, a Large-Scale Distributed Systems Tracing Infrastructure》栋齿,這篇文章是業(yè)內(nèi)實(shí)現(xiàn)鏈路追蹤的標(biāo)桿和理論基礎(chǔ),具有非常大的參考價(jià)值襟诸。
目前瓦堵,鏈路追蹤組件有Google的Dapper,Twitter 的Zipkin歌亲,以及阿里的Eagleeye (鷹眼)等菇用,它們都是非常優(yōu)秀的鏈路追蹤開(kāi)源組件。

本文主要講述如何在Spring Cloud Sleuth中集成Zipkin陷揪。在Spring Cloud Sleuth中集成Zipkin非常的簡(jiǎn)單惋鸥,只需要引入相應(yīng)的依賴和做相關(guān)的配置即可。

基本術(shù)語(yǔ)

Spring Cloud Sleuth采用的是Google的開(kāi)源項(xiàng)目Dapper的專業(yè)術(shù)語(yǔ)悍缠。

  • Span:基本工作單元卦绣,發(fā)送一個(gè)遠(yuǎn)程調(diào)度任務(wù) 就會(huì)產(chǎn)生一個(gè)Span,Span是一個(gè)64位ID唯一標(biāo)識(shí)的飞蚓,Trace是用另一個(gè)64位ID唯一標(biāo)識(shí)的滤港,Span還有其他數(shù)據(jù)信息,比如摘要玷坠、時(shí)間戳事件蜗搔、Span的ID劲藐、以及進(jìn)度ID八堡。
  • Trace:一系列Span組成的一個(gè)樹(shù)狀結(jié)構(gòu)。請(qǐng)求一個(gè)微服務(wù)系統(tǒng)的API接口聘芜,這個(gè)API接口兄渺,需要調(diào)用多個(gè)微服務(wù),調(diào)用每個(gè)微服務(wù)都會(huì)產(chǎn)生一個(gè)新的Span汰现,所有由這個(gè)請(qǐng)求產(chǎn)生的Span組成了這個(gè)Trace挂谍。
  • Annotation:用來(lái)及時(shí)記錄一個(gè)事件的叔壤,一些核心注解用來(lái)定義一個(gè)請(qǐng)求的開(kāi)始和結(jié)束 。這些注解包括以下:
    • cs - Client Sent -客戶端發(fā)送一個(gè)請(qǐng)求口叙,這個(gè)注解描述了這個(gè)Span的開(kāi)始
  • sr - Server Received -服務(wù)端獲得請(qǐng)求并準(zhǔn)備開(kāi)始處理它炼绘,如果將其sr減去cs時(shí)間戳便可得到網(wǎng)絡(luò)傳輸?shù)臅r(shí)間。
  • ss - Server Sent (服務(wù)端發(fā)送響應(yīng))–該注解表明請(qǐng)求處理的完成(當(dāng)請(qǐng)求返回客戶端)妄田,如果ss的時(shí)間戳減去sr時(shí)間戳俺亮,就可以得到服務(wù)器請(qǐng)求的時(shí)間。
  • cr - Client Received (客戶端接收響應(yīng))-此時(shí)Span的結(jié)束疟呐,如果cr的時(shí)間戳減去cs時(shí)間戳便可以得到整個(gè)請(qǐng)求所消耗的時(shí)間脚曾。

案例實(shí)戰(zhàn)

本文案例一共四個(gè)工程采用多Module形式。需要新建一個(gè)主Maven工程启具,主要指定了Spring Boot的版本為1.5.3本讥,Spring Cloud版本為Dalston.RELEASE。包含了eureka-server工程鲁冯,作為服務(wù)注冊(cè)中心拷沸,eureka-server的創(chuàng)建過(guò)程這里不重復(fù);zipkin-server作為鏈路追蹤服務(wù)中心晓褪,負(fù)責(zé)存儲(chǔ)鏈路數(shù)據(jù)堵漱;gateway-service作為服務(wù)網(wǎng)關(guān)工程,負(fù)責(zé)請(qǐng)求的轉(zhuǎn)發(fā),同時(shí)它也作為鏈路追蹤客戶端涣仿,負(fù)責(zé)產(chǎn)生數(shù)據(jù)勤庐,并上傳給zipkin-service;user-service為一個(gè)應(yīng)用服務(wù)好港,對(duì)外暴露API接口愉镰,同時(shí)它也作為鏈路追蹤客戶端,負(fù)責(zé)產(chǎn)生數(shù)據(jù)钧汹。

構(gòu)建zipkin-server工程

新建一個(gè)Module工程丈探,取名為zipkin-server,其pom文件繼承了主Maven工程的pom文件拔莱;作為Eureka Client碗降,引入Eureka的起步依賴spring-cloud-starter-eureka,引入zipkin-server依賴塘秦,以及zipkin-autoconfigure-ui依賴讼渊,后兩個(gè)依賴提供了Zipkin的功能和Zipkin界面展示的功能。代碼如下:


    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sleuth</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>



    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-server</artifactId>
        </dependency>

        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>
        </dependency>
    </dependencies>

在程序的啟動(dòng)類ZipkinServiceApplication加上@EnableZipkinServer開(kāi)啟ZipkinServer的功能尊剔,加上@EnableEurekaClient注解爪幻,啟動(dòng)Eureka Client。代碼如下:

@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class ZipkinServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZipkinServerApplication.class, args);
    }
}

在配置文件application.yml文件,指定程序名為zipkin-server挨稿,端口為9411仇轻,服務(wù)注冊(cè)地址為http://localhost:8761/eureka/奶甘。

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 9411
spring:
  application:
    name: zipkin-server

構(gòu)建user-service

在主Maven工程下建一個(gè)Module工程篷店,取名為user-service,作為應(yīng)用服務(wù)臭家,對(duì)外暴露API接口船庇。pom文件繼承了主Maven工程的pom文件,并引入了Eureka的起步依賴spring-cloud-starter-eureka侣监,Web起步依賴spring-boot-starter-web鸭轮,Zipkin的起步依賴spring-cloud-starter-zipkin,代碼如下:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

在配置文件applicatiom.yml橄霉,指定了程序名為user-service窃爷,端口為8762,服務(wù)注冊(cè)地址為http://localhost:8761/eureka/姓蜂,Zipkin Server地址為http://localhost:9411按厘。spring.sleuth.sampler.percentage為1.0,即100%的概率將鏈路的數(shù)據(jù)上傳給Zipkin Server,在默認(rèn)的情況下钱慢,該值為0.1逮京,代碼如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: user-service
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      percentage: 1.0

在UserController類建一個(gè)“/user/hi”的API接口,對(duì)外提供服務(wù)束莫,代碼如下:

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/hi")
    public String hi(){
        return "I'm forezp";
    }
}

最后作為Eureka Client懒棉,需要在程序的啟動(dòng)類UserServiceApplication加上@EnableEurekaClient注解。

構(gòu)建gateway-service

新建一個(gè)名為gateway-service工程览绿,這個(gè)工程作為服務(wù)網(wǎng)關(guān)策严,將請(qǐng)求轉(zhuǎn)發(fā)到user-service,作為Zipkin客戶端饿敲,需要將鏈路數(shù)據(jù)上傳給Zipkin Server妻导,同時(shí)它也作為Eureka Client。它在pom文件除了需要繼承主Maven工程的 pom怀各,還需引入的依賴如下:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>RELEASE</version>
        </dependency>

</dependencies>

在application.yml文件倔韭,配置程序名為gateway-service,端口為5000瓢对,服務(wù)注冊(cè)地址為http://localhost:8761/eureka/寿酌,Zipkin Server地址為http://localhost:9411,以“/user-api/**”開(kāi)頭的Uri請(qǐng)求沥曹,轉(zhuǎn)發(fā)到服務(wù)名為 user-service的服務(wù)份名。配置代碼如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

server:
  port: 5000
spring:
  application:
    name: gateway-service
  sleuth:
    sampler:
      percentage: 1.0
  zipkin:
    base-url: http://localhost:9411

zuul:
  routes:
    api-a:
      path: /user-api/**
      serviceId: user-service

在程序的啟動(dòng)類GatewayServiceApplication,加上@EnableEurekaClient注解開(kāi)啟Eureka Client妓美,加上@EnableZuulProxy注解僵腺,開(kāi)啟Zuul代理功能。代碼如下:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class GatewayServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServiceApplication.class, args);
    }
}

項(xiàng)目演示

完整的項(xiàng)目搭建完畢壶栋,依次啟動(dòng)eureka-server辰如、zipkin-server、user-service贵试、gateway-service琉兜。在瀏覽器上訪問(wèn)http://localhost:5000/user-api/user/hi,瀏覽器顯示:

I'm forezp

訪問(wèn)http://localhost:9411毙玻,即訪問(wèn)Zipkin的展示界面豌蟋,界面顯示如圖1所示:

image.png

這個(gè)界面主要用來(lái)查找服務(wù)的調(diào)用情況,可以根據(jù)服務(wù)名桑滩、開(kāi)始時(shí)間梧疲、結(jié)束時(shí)間、請(qǐng)求消耗的時(shí)間等條件來(lái)查找运准。點(diǎn)擊“Find Trackes”按鈕幌氮,界面如圖所示。從圖可知服務(wù)的調(diào)用情況胁澳,比如服務(wù)調(diào)用時(shí)間该互、服務(wù)的消耗時(shí)間,服務(wù)調(diào)用的鏈路情況韭畸。

image.png

點(diǎn)擊Dependences按鈕宇智,可以查看服務(wù)的依賴關(guān)系,在本案例中胰丁,gateway-service將請(qǐng)求轉(zhuǎn)發(fā)到了user-service普筹,它們的依賴關(guān)系如圖:

image.png

怎么在鏈路數(shù)據(jù)中添加自定義數(shù)據(jù)

現(xiàn)在需要實(shí)現(xiàn)這樣一個(gè)功能,需要在鏈路數(shù)據(jù)中加上操作人隘马。這需要在gateway-service上實(shí)現(xiàn)太防。建一個(gè)ZuulFilter過(guò)濾器,它的類型為“post”酸员,order為900蜒车,開(kāi)啟攔截。在攔截邏輯方法里幔嗦,通過(guò)Tracer的addTag方法加上自定義的數(shù)據(jù)酿愧,比如本案例中加入了鏈路的操作人。另外也可以在這個(gè)過(guò)濾器中獲取當(dāng)前鏈路的traceId信息,traceId作為鏈路數(shù)據(jù)的唯一標(biāo)識(shí)邀泉,可以存儲(chǔ)在log日志中嬉挡,方便后續(xù)查找钝鸽。

@Component
public class LoggerFilter extends ZuulFilter {

    @Autowired
    Tracer tracer;
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return 900;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

        tracer.addTag("operator","forezp");
        System.out.print(tracer.getCurrentSpan().traceIdString());
        return null;
    }
}

使用spring-cloud-starter-stream-rabbit進(jìn)行鏈路通訊

在上述的案例中,最終gateway-service收集的數(shù)據(jù)庞钢,是通過(guò)Http上傳給zip-server的拔恰,在Spring Cloud Sleuth中支持消息組件來(lái)通訊的,在這一小節(jié)使用RabbitMQ來(lái)通訊基括。首先來(lái)改造zipkin-server颜懊,在pom文件將zipkin-server的依賴去掉,加上spring-cloud-sleuth-zipkin-stream和spring-cloud-starter-stream-rabbit风皿,代碼如下:


    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>

在application.yml配置上RabbitMQ的配置河爹,包括host、端口桐款、用戶名咸这、密碼,如下:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

在程序的啟動(dòng)類ZipkinServerApplication上@EnableZipkinStreamServer注解魔眨,開(kāi)啟ZipkinStreamServer炊苫。代碼如下:

@SpringBootApplication
@EnableEurekaClient
@EnableZipkinStreamServer
public class ZipkinServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZipkinServerApplication.class, args);
    }
}

現(xiàn)在來(lái)改造下Zipkin Client(包括gateway-service、user-service)冰沙,在pom文件中將spring-cloud-starter-zipkin以來(lái)改為spring-cloud-sleuth-zipkin-stream和spring-cloud-starter-stream-rabbit侨艾,代碼如下:


<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>

        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>

同時(shí)在applicayion.yml文件加上RabbitMQ的配置,同zipkin-server工程拓挥。

這樣唠梨,就將鏈路的上傳數(shù)據(jù)從Http改了為用消息代組件RabbitMQ。

將鏈路數(shù)據(jù)存儲(chǔ)在Mysql數(shù)據(jù)庫(kù)

在上述的例子中侥啤,Zipkin Server是將數(shù)據(jù)存儲(chǔ)在內(nèi)存中当叭,一旦程序重啟,之前的鏈路數(shù)據(jù)全部丟失盖灸,那么怎么將鏈路數(shù)據(jù)存儲(chǔ)起來(lái)呢蚁鳖?Zipkin支持Mysql、Elasticsearch赁炎、Cassandra存儲(chǔ)醉箕。這一小節(jié)講述用Mysql存儲(chǔ),下一節(jié)講述用Elasticsearch存儲(chǔ)徙垫。

首先讥裤,在zipkin-server工程加上Mysql的連接依賴mysql-connector-java,JDBC的起步依賴spring-boot-starter-jdbc姻报,代碼如下:

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

在配置文件application.yml加上數(shù)據(jù)源的配置己英,包括數(shù)據(jù)庫(kù)的Url、用戶名吴旋、密碼损肛、連接驅(qū)動(dòng)厢破,另外需要配置zipkin.storage.type為mysql,代碼如下:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/spring-cloud-zipkin?useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
zipkin:
  storage:
    type: mysql

另外需要在Mysql數(shù)據(jù)庫(kù)中初始化數(shù)據(jù)庫(kù)腳本治拿,數(shù)據(jù)庫(kù)腳本地址:https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql/src/main/resources/mysql.sql

CREATE TABLE IF NOT EXISTS zipkin_spans (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL,
  `id` BIGINT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `parent_id` BIGINT,
  `debug` BIT(1),
  `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`) COMMENT 'ignore insert on duplicate';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`) COMMENT 'for joining with zipkin_annotations';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';

CREATE TABLE IF NOT EXISTS zipkin_annotations (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';

CREATE TABLE IF NOT EXISTS zipkin_dependencies (
  `day` DATE NOT NULL,
  `parent` VARCHAR(255) NOT NULL,
  `child` VARCHAR(255) NOT NULL,
  `call_count` BIGINT,
  `error_count` BIGINT
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);

將鏈路數(shù)據(jù)存儲(chǔ)在ElasticSearch

使用Mysql存儲(chǔ)鏈路數(shù)據(jù)摩泪,在并發(fā)高的情況下,顯然不合理忍啤,這時(shí)可以選擇使用ElasticSearch存儲(chǔ)。讀者需要自行安裝ElasticSearch仙辟、Kibana(下一小節(jié)使用)同波,下載地址為https://www.elastic.co/products/elasticsearch。安裝完成后并啟動(dòng)它們叠国,其中ElasticSearch的默認(rèn)端口為9200未檩,Kibana的端口為5601。

安裝的過(guò)程可以參考我的這篇文章:http://blog.csdn.net/forezp/article/details/71189836

本小節(jié)的案例在上上小節(jié)的案例的基礎(chǔ)上進(jìn)行改造粟焊。首先在pom文件冤狡,加上zipkin的依賴和zipkin-autoconfigure-storage-elasticsearch-http的依賴,代碼如下:


        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin</artifactId>
            <version>1.28.0</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
            <version>1.28.0</version>
        </dependency>

在application.yml文件加上Zipkin的配置项棠,配置了zipkin的存儲(chǔ)類型為elasticsearch悲雳,使用的StorageComponent為elasticsearch。然后需要配置elasticsearch香追,包括hosts合瓢,可以配置多個(gè),用“透典,”隔開(kāi)晴楔;index為zipkin等,具體配置如下:


zipkin:
  storage:
    type: elasticsearch
    StorageComponent: elasticsearch
    elasticsearch:
      cluster: elasticsearch
      max-requests: 30
      index: zipkin
      index-shards: 3
      index-replicas: 1
      hosts: localhost:9200

在kibana上展示

上一小節(jié)講述了如何將鏈路數(shù)據(jù)存儲(chǔ)在ElasticSearch峭咒,ElasticSearch可以和Kibana結(jié)合税弃,將鏈路數(shù)據(jù)展示在 Kibana上。安裝完Kibana凑队,并啟動(dòng)则果,它默認(rèn)會(huì)向本地的9200端口的ElasticSearch讀取數(shù)據(jù),它默認(rèn)的端口為5601漩氨。訪問(wèn)http://localhost:5601短条,顯示的界面如下:

image.png

在上述的界面點(diǎn)擊"Management"按鈕,然后點(diǎn)擊“Add New”才菠,添加一個(gè)index茸时,在上節(jié)我們?cè)贓lasticSearch中寫(xiě)入鏈路數(shù)據(jù)的index配置為“zipkin”,那么在界面填寫(xiě)為“zipkin-*”,點(diǎn)擊“Create”按鈕赋访。

image.png

創(chuàng)建完index之后可都,點(diǎn)擊Discover缓待,就可以在界面上展示鏈路數(shù)據(jù)了。

image.png

源碼下載

最原始的工程:

https://github.com/forezp/SpringCloudLearning/tree/master/chapter-sleuth

采用RabbitMq通訊的工程:

https://github.com/forezp/SpringCloudLearning/tree/master/chapter-sleuth-stream

采用Mysql存儲(chǔ)的工程:

https://github.com/forezp/SpringCloudLearning/tree/master/chapter-sleuth-stream-mysql

采用ES存儲(chǔ)的工程:

https://github.com/forezp/SpringCloudLearning/tree/master/chapter-sleuth-stream-elasticsearch

參考資料

http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html

https://github.com/openzipkin/zipkin

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末渠牲,一起剝皮案震驚了整個(gè)濱河市旋炒,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌签杈,老刑警劉巖瘫镇,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異答姥,居然都是意外死亡铣除,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)鹦付,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)尚粘,“玉大人,你說(shuō)我怎么就攤上這事敲长±杉蓿” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵祈噪,是天一觀的道長(zhǎng)泽铛。 經(jīng)常有香客問(wèn)我,道長(zhǎng)辑鲤,這世上最難降的妖魔是什么厚宰? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮遂填,結(jié)果婚禮上铲觉,老公的妹妹穿的比我還像新娘。我一直安慰自己吓坚,他們只是感情好撵幽,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著礁击,像睡著了一般盐杂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上哆窿,一...
    開(kāi)封第一講書(shū)人閱讀 51,708評(píng)論 1 305
  • 那天链烈,我揣著相機(jī)與錄音,去河邊找鬼挚躯。 笑死强衡,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的码荔。 我是一名探鬼主播漩勤,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼感挥,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了越败?” 一聲冷哼從身側(cè)響起触幼,我...
    開(kāi)封第一講書(shū)人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎究飞,沒(méi)想到半個(gè)月后置谦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡亿傅,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年媒峡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片袱蜡。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡丝蹭,死狀恐怖慢宗,靈堂內(nèi)的尸體忽然破棺而出坪蚁,到底是詐尸還是另有隱情,我是刑警寧澤镜沽,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布敏晤,位于F島的核電站,受9級(jí)特大地震影響缅茉,放射性物質(zhì)發(fā)生泄漏嘴脾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一蔬墩、第九天 我趴在偏房一處隱蔽的房頂上張望译打。 院中可真熱鬧,春花似錦拇颅、人聲如沸奏司。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)韵洋。三九已至,卻和暖如春黄锤,著一層夾襖步出監(jiān)牢的瞬間搪缨,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工鸵熟, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留副编,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓流强,卻偏偏與公主長(zhǎng)得像齿桃,于是被迫代替她去往敵國(guó)和親惑惶。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容