这篇文章主要讲述服务追踪组件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调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。

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

三、术语

  • 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注解的过程图形化:

四、构建工程

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

4.1 构建server-zipkin

建一个spring-boot工程取名为server-zipkin,在其pom引入依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  在其程序入口类, 加上注解@EnableZipkinServer,开启ZipkinServer的功能:

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableZipkinServer
public class ServerZipkinApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerZipkinApplication.class, args);
    }
}

  在配置文件application.yml指定服务端口为:

1
server.port=9411

  

4.2 创建service-hi

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--compile('org.springframework.cloud:spring-cloud-starter-zipkin')-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

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

  

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

对外暴露接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@SpringBootApplication
@RestController
public class ServiceHiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceHiApplication.class, args);
    }
    private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName());
    @Autowired
    private RestTemplate restTemplate;
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
    @RequestMapping("/hi")
    public String callHome(){
        LOG.log(Level.INFO, "calling trace service-hi  ");
        return restTemplate.getForObject("http://localhost:8989/miya", String.class);
    }
    @RequestMapping("/info")
    public String info(){
        LOG.log(Level.INFO, "calling trace service-hi ");
        return "i'm service-hi";
    }
    @Bean
    public AlwaysSampler defaultSampler(){
        return new AlwaysSampler();
    }
}

  架构代码如下:

Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求:一七九一七四三三八零

转载于:https://www.cnblogs.com/sunnysunny/p/10455110.html

企业分布式微服务云SpringCloud SpringBoot mybatis (九)服务链路追踪(Spring Cloud Sleuth)...相关推荐

  1. 《深入理解 Spring Cloud 与微服务构建》第十四章 服务链路追踪 Spring Cloud Sleuth

    <深入理解 Spring Cloud 与微服务构建>第十四章 服务链路追踪 Spring Cloud Sleuth 文章目录 <深入理解 Spring Cloud 与微服务构建> ...

  2. (九)企业分布式微服务云SpringCloud SpringBoot mybatis-服务链路追踪(Spring Cloud Sleuth)...

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Add sleuth to the classpath of a Spring Bo ...

  3. java版b2b2c社交电商springcloud分布式微服务 (九)服务链路追踪(Spring Cloud Sleuth)...

    电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Spring cloud B ...

  4. springcloud上传文件_Spring Cloud实战:服务链路追踪Spring Cloud Sleuth

    推荐阅读: Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC 一个SpringBoot问题就干趴下了?我却凭着这份PDF文档吊打面试官. 前 ...

  5. 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)

    转:https://blog.csdn.net/forezp/article/details/70162074 这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了z ...

  6. 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

    转载请标明出处: http://blog.csdn.net/forezp/article/details/81041078 本文出自方志朋的博客 个人博客纯净版:https://www.fangzhi ...

  7. SpringCloud: 服务链路追踪(Spring Cloud Sleuth)

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且 ...

  8. SpringCloud 教程 (二) 服务链路追踪(Spring Cloud Sleuth)

    一.简介 Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle exampl ...

  9. SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

    转载请标明出处:  http://blog.csdn.net/forezp/article/details/81041078  本文出自方志朋的博客 这篇文章主要讲述服务追踪组件zipkin,Spri ...

最新文章

  1. 我的FizzBuzz和一点感想
  2. 如何搭建VUE开发环境
  3. python的快速入门-1.1、Python快速入门(0529)
  4. C# 编码规范和编程好习惯
  5. 手把手教你做一辆mini平衡自行车!
  6. PMP读书笔记(第7章)
  7. 【转】应用架构之道:分离业务逻辑和技术细节
  8. python查询模块路径_Visual Studio 2017中的Python无法通过“搜索路径”查找模块
  9. java7新添加的一些特性--转
  10. texstudio自动拼写检测_飞桨PaddleDetection物体检测统一框架详解
  11. 【图解深度学习】【章节:2-1.1 | 什么是机器学习?】连小学生都能看懂的深度学习基础总结
  12. Duilib资源文件打包成DLL并调用
  13. Springboot​ mybatis-plus
  14. mysql numeric int_关于mysql数据库的数据类型numeric和decimal_MySQL
  15. 我是阿圆,我们明年见。
  16. sim卡换新卡显示无服务器,SIM卡又现新漏洞 SIM卡无服务 SIM卡被锁怎么办?
  17. 会话、张量、变量OP
  18. 简述几种常用的编码器数据格式
  19. 7-1 sdut-循环-7-统计正数和负数的个数(II) (10 分)
  20. OpenFoam-6 导入并编译一个新湍流模型

热门文章

  1. LeNet5,AlexNet,MobileNet它们的前身你知道吗?
  2. 命名人工智能最高奖,破译德军密码,却被祖国逼得自杀-6月7日
  3. 机器学习面试题集-图解准确率,精确率,召回率
  4. 2018年终总结之AI领域开源框架汇总
  5. SAP WM 有无保存WM Level历史库存的Table?
  6. 干货回顾丨TensorFlow四种Cross Entropy算法的实现和应用
  7. 盘点52个全球人工智能和机器学习重要会议
  8. 免费教材丨第51期:数学基础课程----概率论教程、机器学习中的数学基础
  9. 用人工智能监督人工 遭遇非技术困局
  10. 机器人智能抓取系统:目前几种主流的解决方案