springcloud sleuth

基本术语

Spring Cloud Sleuth 采用了 Google 开源项目 Dapper 专业术语:
   1) Span 基本工作单元,发送 个远程调度任务就会产生一个 Span, Span 是用 一个64ID 标识的, Trace 是用另 一个64 ID 标识的。span 还包含了其他的信息,例如摘要、时间戳事件 Span 及进程 ID
   2) Trace :由 系列 Span 组成的,呈树状结构。请求一个微服务系统的 API ,这个API 接口需要调用多个微服务单元,调用 一个微服务单元都会产生 一个新的 span, 所有由这个请求产生 Span 组成了这个 Trace
   3) Annotation :用于记录 个事件, 一些核 注解用于定义 一个请求的开始和结束,这些注解如下:
       cs-Client Sent 客户端发送 一个请求,这个注解描述了 Span 的开始。
       sr-Server Received :服务端获得请求并准备开始处理它,如果将其 减去 cs 时间戳,便可得到网络传输的时间。
       ss-Server Sent :服务端发送响应, 该注解表明请求处理的完成(当请求返回客户端),用ss的时间戳减 sr 时间戳,便可以得到服务器响应的时间。
       cr-Client Received 客户端接收响应, 此时 Span 结束,如果cr 的时间戳减去 cs 时间戳,便可以得到整个请求所消耗的时间。
 

实例

项目结构图

zipkin-server模块

引入依赖

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

配置文件

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

启动类添加注解

@EnableZipkinServer

网关服务模块 gateway-serice

引入依赖

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

配置文件

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
    api-b:
      path: /user-api02/**
      serviceId: user02-service

启动类添加注解

无需添加其它注解

服务提供者,服务消费者user-service模块

引入依赖

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

配置文件

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

#这个可以不写
#spring.zipkin.base-url=http://localhost:9411   

启动类添加注解

无需添加其它注解

测试

依次启动项目,浏览器输入网关请求,访问zipkin  http://localhost:9411/

服务链路数据存储

服务链路数据可以存储到 rabbitMQ ,数据库或elasticSearch服务器上。只要简单的集成到zipkin-server模块上即可

猜你喜欢

转载自www.cnblogs.com/tanouou/p/12339669.html