zoukankan      html  css  js  c++  java
  • 业余草 SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件。

    一、简介

    Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle examples), and you will see the correlation data being collected in logs, as long as you are logging requests.

    —— 摘自官网

    Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可。

    二、服务追踪分析

    微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。

    Paste_Image.png

    随着服务的越来越多,对调用链的分析会越来越复杂。它们之间的调用关系也许如下:

    Paste_Image.png

    三、术语

    • Span:基本工作单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span通过一个64位ID唯一标识,trace以另一个64位ID表示,span还有其他数据信息,比如摘要、时间戳事件、关键值注释(tags)、span的ID、以及进度ID(通常是IP地址) 
      span在不断的启动和停止,同时记录了时间信息,当你创建了一个span,你必须在未来的某个时刻停止它。
    • Trace:一系列spans组成的一个树状结构,例如,如果你正在跑一个分布式大数据工程,你可能需要创建一个trace。
    • Annotation:用来及时记录一个事件的存在,一些核心annotations用来定义一个请求的开始和结束 
      • cs - Client Sent -客户端发起一个请求,这个annotion描述了这个span的开始
      • sr - Server Received -服务端获得请求并准备开始处理它,如果将其sr减去cs时间戳便可得到网络延迟
      • ss - Server Sent -注解表明请求处理的完成(当请求返回客户端),如果ss减去sr时间戳便可得到服务端需要的处理请求时间
      • cr - Client Received -表明span的结束,客户端成功接收到服务端的回复,如果cr减去cs时间戳便可得到客户端从服务端获取回复的所有所需时间 
        将Span和Trace在一个系统中使用Zipkin注解的过程图形化:

    将Span和Trace在一个系统中使用Zipkin注解的过程图形化:

    Paste_Image.png

    四、构建工程

    基本知识讲解完毕,下面我们来实战,本文的案例主要有三个工程组成:一个server-zipkin,它的主要作用使用ZipkinServer 的功能,收集调用数据,并展示;一个service-hi,对外暴露hi接口;一个service-miya,对外暴露miya接口;这两个service可以相互调用;并且只有调用了,server-zipkin才会收集数据的,这就是为什么叫服务追踪了。

    4.1 构建server-zipkin

    在spring Cloud为F版本的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可,下载地址:

    https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

    也可以在这里下载:

    链接: https://pan.baidu.com/s/1w614Z8gJXHtqLUB6dKWOpQ 密码: 26pf

    下载完成jar 包之后,需要运行jar,如下:

    java -jar zipkin-server-2.10.1-exec.jar

    访问浏览器localhost:9494

    4.2 创建service-hi

    在其pom引入起步依赖spring-cloud-starter-zipkin,代码如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5  
     6     <groupId>com.forezp</groupId>
     7     <artifactId>service-zipkin</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10  
    11     <name>service-hi</name>
    12     <description>Demo project for Spring Boot</description>
    13  
    14     <parent>
    15         <groupId>com.forezp</groupId>
    16         <artifactId>sc-f-chapter9</artifactId>
    17         <version>0.0.1-SNAPSHOT</version>
    18     </parent>
    19  
    20  
    21  
    22     <dependencies>
    23  
    24         <dependency>
    25             <groupId>org.springframework.boot</groupId>
    26             <artifactId>spring-boot-starter-web</artifactId>
    27         </dependency>
    28  
    29         <dependency>
    30             <groupId>org.springframework.cloud</groupId>
    31             <artifactId>spring-cloud-starter-zipkin</artifactId>
    32         </dependency>
    33  
    34  
    35  
    36     </dependencies>
    37  
    38     <build>
    39         <plugins>
    40             <plugin>
    41                 <groupId>org.springframework.boot</groupId>
    42                 <artifactId>spring-boot-maven-plugin</artifactId>
    43             </plugin>
    44         </plugins>
    45     </build>
    46  
    47 </project>

    在其配置文件application.yml指定zipkin server的地址,头通过配置“spring.zipkin.base-url”指定:

    1 server.port=8988
    2 spring.zipkin.base-url=http://localhost:9411
    3 spring.application.name=service-hi

    通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就可以了。

    对外暴露接口:

     1 package com.forezp;
     2  
     3 import brave.sampler.Sampler;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.boot.SpringApplication;
     6 import org.springframework.boot.autoconfigure.SpringBootApplication;
     7 import org.springframework.context.annotation.Bean;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 import org.springframework.web.bind.annotation.RestController;
    10 import org.springframework.web.client.RestTemplate;
    11 import java.util.logging.Level;
    12 import java.util.logging.Logger;
    13  
    14 @SpringBootApplication
    15 @RestController
    16 public class ServiceHiApplication {
    17  
    18     public static void main(String[] args) {
    19         SpringApplication.run(ServiceHiApplication.class, args);
    20     }
    21  
    22     private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName());
    23  
    24  
    25     @Autowired
    26     private RestTemplate restTemplate;
    27  
    28     @Bean
    29     public RestTemplate getRestTemplate(){
    30         return new RestTemplate();
    31     }
    32  
    33     @RequestMapping("/hi")
    34     public String callHome(){
    35         LOG.log(Level.INFO, "calling trace service-hi  ");
    36         return restTemplate.getForObject("http://localhost:8989/miya", String.class);
    37     }
    38     @RequestMapping("/info")
    39     public String info(){
    40         LOG.log(Level.INFO, "calling trace service-hi ");
    41  
    42         return "i'm service-hi";
    43  
    44     }
    45  
    46     @Bean
    47     public Sampler defaultSampler() {
    48         return Sampler.ALWAYS_SAMPLE;
    49     }
    50  
    51  
    52 }

    4.3 创建service-miya

    创建过程痛service-hi,引入相同的依赖,配置下spring.zipkin.base-url。

    对外暴露接口:

     1  
     2 package com.forezp;
     3  
     4 import brave.sampler.Sampler;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.boot.SpringApplication;
     7 import org.springframework.boot.autoconfigure.SpringBootApplication;
     8 import org.springframework.context.annotation.Bean;
     9 import org.springframework.web.bind.annotation.RequestMapping;
    10 import org.springframework.web.bind.annotation.RestController;
    11 import org.springframework.web.client.RestTemplate;
    12  
    13 import java.util.logging.Level;
    14 import java.util.logging.Logger;
    15  
    16 @SpringBootApplication
    17 @RestController
    18 public class ServiceMiyaApplication {
    19  
    20     public static void main(String[] args) {
    21         SpringApplication.run(ServiceMiyaApplication.class, args);
    22     }
    23  
    24     private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName());
    25  
    26  
    27     @RequestMapping("/hi")
    28     public String home(){
    29         LOG.log(Level.INFO, "hi is being called");
    30         return "hi i'm miya!";
    31     }
    32  
    33     @RequestMapping("/miya")
    34     public String info(){
    35         LOG.log(Level.INFO, "info is being called");
    36         return restTemplate.getForObject("http://localhost:8988/info",String.class);
    37     }
    38  
    39     @Autowired
    40     private RestTemplate restTemplate;
    41  
    42     @Bean
    43     public RestTemplate getRestTemplate(){
    44         return new RestTemplate();
    45     }
    46  
    47  
    48     @Bean
    49     public Sampler defaultSampler() {
    50         return Sampler.ALWAYS_SAMPLE;
    51     }
    52 }

    4.4 启动工程,演示追踪

    依次启动上面的工程,打开浏览器访问:http://localhost:9411/,会出现以下界面:

    Paste_Image.png

    访问:http://localhost:8989/miya,浏览器出现:

    i’m service-hi

    再打开http://localhost:9411/的界面,点击Dependencies,可以发现服务的依赖关系:

    Paste_Image.png

    点击find traces,可以看到具体服务相互调用的数据:

    Paste_Image.png

    本文源码下载:

    https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter9

    五、参考资料

    spring-cloud-sleuth

    http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

    业余草微信公众号

    感谢您的关注!可加QQ1群:135430763,QQ2群:454796847,QQ3群:187424846。QQ群进群密码:xttblog,想加微信群的朋友,可以微信搜索:xmtxtt,备注:“xttblog”,添加助理微信拉你进群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作可添加助理微信进行沟通!

  • 相关阅读:
    没人关注的角落是个好地方
    PICKIT3 WIN10 无法连接问题解决
    TM1650+msp430单片机 调试及遇到问题的总结
    即将会来日常更新stm32的学习过程
    Learn Arduino the hard way 1——点亮一个LED
    准备再次开始更新
    Tcl之Read files for synthesis
    Verilog之event
    Tcl之Math
    Digital design之Boolean Algebra
  • 原文地址:https://www.cnblogs.com/panda2/p/9419199.html
Copyright © 2011-2022 走看看