Spring Cloud Sleuth超详细实战

转载请标明出处:
blog.csdn.net/forezp/arti…
本文出自方志朋的博客html

为何须要Spring Cloud Sleuth

微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统每每有不少个服务单元。因为服务单元数量众多,业务的复杂性,若是出现了错误和异常,很难去定位。主要体如今,一个请求可能须要调用不少个服务,而内部服务的调用复杂性,决定了问题难以定位。因此微服务架构中,必须实现分布式链路追踪,去跟进一个请求到底有哪些服务参与,参与的顺序又是怎样的,从而达到每一个请求的步骤清晰可见,出了问题,很快定位。前端

举个例子,在微服务系统中,一个来自用户的请求,请求先达到前端A(如前端界面),而后经过远程调用,达到系统的中间件B、C(如负载均衡、网关等),最后达到后端服务D、E,后端通过一系列的业务逻辑计算最后将数据返回给用户。对于这样一个请求,经历了这么多个服务,怎么样将它的请求过程的数据记录下来呢?这就须要用到服务链路追踪。java

Google开源的 Dapper链路追踪组件,并在2010年发表了论文《Dapper, a Large-Scale Distributed Systems Tracing Infrastructure》,这篇文章是业内实现链路追踪的标杆和理论基础,具备很是大的参考价值。
目前,链路追踪组件有Google的Dapper,Twitter 的Zipkin,以及阿里的Eagleeye (鹰眼)等,它们都是很是优秀的链路追踪开源组件。mysql

本文主要讲述如何在Spring Cloud Sleuth中集成Zipkin。在Spring Cloud Sleuth中集成Zipkin很是的简单,只须要引入相应的依赖和作相关的配置便可。git

基本术语

Spring Cloud Sleuth采用的是Google的开源项目Dapper的专业术语。github

  • Span:基本工做单元,发送一个远程调度任务 就会产生一个Span,Span是一个64位ID惟一标识的,Trace是用另外一个64位ID惟一标识的,Span还有其余数据信息,好比摘要、时间戳事件、Span的ID、以及进度ID。
  • Trace:一系列Span组成的一个树状结构。请求一个微服务系统的API接口,这个API接口,须要调用多个微服务,调用每一个微服务都会产生一个新的Span,全部由这个请求产生的Span组成了这个Trace。
  • Annotation:用来及时记录一个事件的,一些核心注解用来定义一个请求的开始和结束 。这些注解包括如下:
    • cs - Client Sent -客户端发送一个请求,这个注解描述了这个Span的开始
    • sr - Server Received -服务端得到请求并准备开始处理它,若是将其sr减去cs时间戳即可获得网络传输的时间。
    • ss - Server Sent (服务端发送响应)–该注解代表请求处理的完成(当请求返回客户端),若是ss的时间戳减去sr时间戳,就能够获得服务器请求的时间。
    • cr - Client Received (客户端接收响应)-此时Span的结束,若是cr的时间戳减去cs时间戳即可以获得整个请求所消耗的时间。

案例实战

本文案例一共四个工程采用多Module形式。须要新建一个主Maven工程,主要指定了Spring Boot的版本为1.5.3,Spring Cloud版本为Dalston.RELEASE。包含了eureka-server工程,做为服务注册中心,eureka-server的建立过程这里不重复;zipkin-server做为链路追踪服务中心,负责存储链路数据;gateway-service做为服务网关工程,负责请求的转发,同时它也做为链路追踪客户端,负责产生数据,并上传给zipkin-service;user-service为一个应用服务,对外暴露API接口,同时它也做为链路追踪客户端,负责产生数据。web

构建zipkin-server工程

新建一个Module工程,取名为zipkin-server,其pom文件继承了主Maven工程的pom文件;做为Eureka Client,引入Eureka的起步依赖spring-cloud-starter-eureka,引入zipkin-server依赖,以及zipkin-autoconfigure-ui依赖,后两个依赖提供了Zipkin的功能和Zipkin界面展现的功能。代码以下:spring

<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>复制代码

在程序的启动类ZipkinServiceApplication加上@EnableZipkinServer开启ZipkinServer的功能,加上@EnableEurekaClient注解,启动Eureka Client。代码以下:sql

@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class ZipkinServerApplication {

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

在配置文件application.yml文件,指定程序名为zipkin-server,端口为9411,服务注册地址为http://localhost:8761/eureka/。数据库

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

构建user-service

在主Maven工程下建一个Module工程,取名为user-service,做为应用服务,对外暴露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,服务注册地址为http://localhost:8761/eureka/,Zipkin Server地址为http://localhost:9411。spring.sleuth.sampler.percentage为1.0,即100%的几率将链路的数据上传给Zipkin Server,在默认的状况下,该值为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类建一个“/user/hi”的API接口,对外提供服务,代码以下:

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

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

最后做为Eureka Client,须要在程序的启动类UserServiceApplication加上@EnableEurekaClient注解。

构建gateway-service

新建一个名为gateway-service工程,这个工程做为服务网关,将请求转发到user-service,做为Zipkin客户端,须要将链路数据上传给Zipkin Server,同时它也做为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,服务注册地址为http://localhost:8761/eureka/,Zipkin Server地址为http://localhost:9411,以“/user-api/**”开头的Uri请求,转发到服务名为 user-service的服务。配置代码以下:

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复制代码

在程序的启动类GatewayServiceApplication,加上@EnableEurekaClient注解开启Eureka Client,加上@EnableZuulProxy注解,开启Zuul代理功能。代码以下:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class GatewayServiceApplication {

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

项目演示

完整的项目搭建完毕,依次启动eureka-server、zipkin-server、user-service、gateway-service。在浏览器上访问http://localhost:5000/user-api/user/hi,浏览器显示:

I'm forezp

访问http://localhost:9411,即访问Zipkin的展现界面,界面显示如图1所示:

image.png
image.png

这个界面主要用来查找服务的调用状况,能够根据服务名、开始时间、结束时间、请求消耗的时间等条件来查找。点击“Find Trackes”按钮,界面如图所示。从图可知服务的调用状况,好比服务调用时间、服务的消耗时间,服务调用的链路状况。

image.png
image.png

点击Dependences按钮,能够查看服务的依赖关系,在本案例中,gateway-service将请求转发到了user-service,它们的依赖关系如图:

image.png
image.png

怎么在链路数据中添加自定义数据

如今须要实现这样一个功能,须要在链路数据中加上操做人。这须要在gateway-service上实现。建一个ZuulFilter过滤器,它的类型为“post”,order为900,开启拦截。在拦截逻辑方法里,经过Tracer的addTag方法加上自定义的数据,好比本案例中加入了链路的操做人。另外也能够在这个过滤器中获取当前链路的traceId信息,traceId做为链路数据的惟一标识,能够存储在log日志中,方便后续查找。

@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进行链路通信

在上述的案例中,最终gateway-service收集的数据,是经过Http上传给zip-server的,在Spring Cloud Sleuth中支持消息组件来通信的,在这一小节使用RabbitMQ来通信。首先来改造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复制代码

在程序的启动类ZipkinServerApplication上@EnableZipkinStreamServer注解,开启ZipkinStreamServer。代码以下:

@SpringBootApplication
@EnableEurekaClient
@EnableZipkinStreamServer
public class ZipkinServerApplication {

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

如今来改造下Zipkin Client(包括gateway-service、user-service),在pom文件中将spring-cloud-starter-zipkin以来改成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>复制代码

同时在applicayion.yml文件加上RabbitMQ的配置,同zipkin-server工程。

这样,就将链路的上传数据从Http改了为用消息代组件RabbitMQ。

将链路数据存储在Mysql数据库

在上述的例子中,Zipkin Server是将数据存储在内存中,一旦程序重启,以前的链路数据所有丢失,那么怎么将链路数据存储起来呢?Zipkin支持Mysql、Elasticsearch、Cassandra存储。这一小节讲述用Mysql存储,下一节讲述用Elasticsearch存储。

首先,在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加上数据源的配置,包括数据库的Url、用户名、密码、链接驱动,另外须要配置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数据库中初始化数据库脚本,数据库脚本地址:github.com/openzipkin/…

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`);复制代码

将链路数据存储在ElasticSearch

使用Mysql存储链路数据,在并发高的状况下,显然不合理,这时能够选择使用ElasticSearch存储。读者须要自行安装ElasticSearch、Kibana(下一小节使用),下载地址为www.elastic.co/products/el…

安装的过程能够参考个人这篇文章:blog.csdn.net/forezp/arti…

本小节的案例在上上小节的案例的基础上进行改造。首先在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的存储类型为elasticsearch,使用的StorageComponent为elasticsearch。而后须要配置elasticsearch,包括hosts,能够配置多个,用“,”隔开;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上展现

上一小节讲述了如何将链路数据存储在ElasticSearch,ElasticSearch能够和Kibana结合,将链路数据展现在 Kibana上。安装完Kibana,并启动,它默认会向本地的9200端口的ElasticSearch读取数据,它默认的端口为5601。访问http://localhost:5601,显示的界面以下:

image.png
image.png

在上述的界面点击"Management"按钮,而后点击“Add New”,添加一个index,在上节咱们在ElasticSearch中写入链路数据的index配置为“zipkin”,那么在界面填写为“zipkin-*”,点击“Create”按钮。

image.png
image.png

建立完index以后,点击Discover,就能够在界面上展现链路数据了。

image.png
image.png

源码下载

最原始的工程:

github.com/forezp/Spri…

采用RabbitMq通信的工程:

github.com/forezp/Spri…

采用Mysql存储的工程:

github.com/forezp/Spri…

采用ES存储的工程:

github.com/forezp/Spri…

参考资料

cloud.spring.io/spring-clou…

github.com/openzipkin/…

关注个人公众号

精彩内容不能错过!

forezp.jpg
forezp.jpg
相关文章
相关标签/搜索