Spring Cloud微服务架构(十三)服务链路追踪(Spring Cloud Sleuth)

1、zipkin简介

Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,zipkin为分布式链路调用监控系统,聚合各业务系统调用延迟数据,达到链路调用监控跟踪。

随着微服务数量不断增长,它们之间的关系会越来越复杂,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败,需要跟踪一个请求从一个微服务到下一个微服务的传播过程。

分布式服务跟踪可以:

  1. 提供链路追踪,故障快速定位:可以通过调用链结合业务日志快速定位错误信息

  2. 可视化各个阶段耗时,进行性能分析

  3. 各个调用环节的可用性、梳理服务依赖关系以及优化

  4. 数据分析,优化链路:可以得到用户的行为路径,汇总分析应用在很多业务场景

zipkin涉及几个概念:

  1. Span:基本工作单元,一次链路调用(可以是RPC,DB等没有特定的限制)创建一个span,通过一个64位ID标识它, span通过还有其他的数据,例如描述信息,时间戳,key-value对的(Annotation)tag信息,parent-id等,其中parent-id ,可以表示span调用链路来源,通俗的理解span就是一次请求信息

  2. Trace:类似于树结构的Span集合,表示一条调用链路,存在唯一标识。

  3. Annotation: 注解,用来记录请求特定事件相关信息(例如时间),通常包含四个注解信息 cs - Client Start,表示客户端发起请求 sr - Server Receive,表示服务端收到请求 ss - Server Send,表示服务端完成处理,并将结果发送给客户端 cr - Client Received,表示客户端获取到服务端返回信息

2、构建zipkin

Spring Cloud微服务架构(十三)服务链路追踪(Spring Cloud Sleuth)_第1张图片

创建完pom文件修改如下



         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.vesus
    springcloud-zipkin
    0.0.1-SNAPSHOT
    jar

    springcloud-zipkin
    Demo project for Spring Boot

    
        com.vesus
        springcloud-demo
        0.0.1-SNAPSHOT
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
        
            io.zipkin.java
            zipkin-server
        
        
            io.zipkin.java
            zipkin-autoconfigure-ui
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

3、创建application.yml配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ # 注册中心地址
spring:
  application:
    name: springcloud-zipkin
server:
  port: 8773

4、入口方法加上注解@EnableZipkinServer,开启ZipkinServer的功能。

package com.vesus.springcloudzipkin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import zipkin.server.EnableZipkinServer;

@SpringBootApplication
@EnableDiscoveryClient
@EnableZipkinServer
public class SpringcloudZipkinApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringcloudZipkinApplication.class, args);
    }
}

5、修改springcloud-service增加依赖spring-cloud-starter-zipkin,引入后的pom如下:



         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.vesus
    springcloud-service
    0.0.1-SNAPSHOT
    jar

    springcloud-service
    Demo project for Spring Boot

    
        com.vesus
        springcloud-demo
        0.0.1-SNAPSHOT
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
         
            org.springframework.cloud
            spring-cloud-sleuth-zipkin
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

6、修改springcloud-service的application.yml,加入zipkin server收集信息的地址

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ # 注册中心地址
spring:
  application:
    name: spring-cloud-service
  zipkin:
    base-url: http://localhost:8773
server:
  port: 8762

7、修改springcloud-ribbon增加依赖spring-cloud-starter-zipkin,引入后的pom如下:



         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.vesus
    springcloud-ribbon
    0.0.1-SNAPSHOT
    jar

    springcloud-ribbon
    Demo project for Spring Boot

    
        com.vesus
        springcloud-demo
        0.0.1-SNAPSHOT
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
        
            org.springframework.cloud
            spring-cloud-starter-ribbon
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
        
            org.springframework.cloud
            spring-cloud-sleuth-zipkin
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

8、修改springcloud-ribbon的application.yml,加入zipkin server收集信息的地址

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ # 注册中心地址
spring:
  application:
    name: spring-cloud-ribbon
  zipkin:
    base-url: http://localhost:8773
server:
  port: 8763

9、访问 http://localhost:8773/,显示

Spring Cloud微服务架构(十三)服务链路追踪(Spring Cloud Sleuth)_第2张图片

10、访问地址 http://localhost:8763,刷新zipkin,显示

Spring Cloud微服务架构(十三)服务链路追踪(Spring Cloud Sleuth)_第3张图片

源码:https://gitee.com/vesus198/springcloud-demo/tree/master/springcloud-zipkin

你可能感兴趣的:(spring,cloud)