Spring Cloud学习--服务追踪(Sleuth)

一、 Spring Cloud Sleuth简介

微服务之间通过网络进行通信,要想跟踪每一个请求,了解请求消耗时间、网络延迟、业务逻辑消耗时间等指标信息。Spring Cloud Sleuth为Spring Cloud提供了分布式跟踪的解决方案。其原理图如下:

这里写图片描述

Sleuth术语简介:

Span(跨度):基本工作单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span通过一个64位ID唯一标识,span还有其他数据信息,比如摘要、时间戳事件、关键值注释(tags)、span的ID、span的父ID,以及进度ID(通常是IP地址),span在不断的启动和停止,同时记录了时间信息,当你创建了一个span,你必须在未来的某个时刻停止它。

Trace(跟踪):一组共享“root span”的span组成的一个树状结构,trace以另一个64位ID唯一标识,例如,如果你正在跑一个分布式大数据工程,你可能需要创建一个trace。

Annotation(标注):用来及时记录一个事件的存在,一些核心annotations用来定义一个请求的开始和结束。

cs - Client Sent -客户端发起一个请求,这个annotion描述了这个span的开始
sr - Server Received -服务端获得请求并准备开始处理它,如果将其sr减去cs时间戳便可得到网络延迟
ss - Server Sent -注解表明请求处理的完成(当请求返回客户端),如果ss减去sr时间戳便可得到服务端需要的处理请求时间
cr - Client Received -表明span的结束,客户端成功接收到服务端的回复,如果cr减去cs时间戳便可得到客户端从服务端获取回复的所有所需时间

二、 整合Spring Cloud Sleuth

1.将之前的EurekaClientA、EurekaClientB、EurekaClientC改造,(http://blog.csdn.net/u012482647/article/details/78017903)在pom文件中添加依赖:

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>

2.配置application.properties

spring.application.name=microservice-sleuth
server.port=8003
logging.level.root=info
logging.level.org.springframework.web.servlet.DispatcherServlet=debug

3.编写Controller类

package com.swc;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chao on 2017-9-18.
 */
@RestController
public class ComputeController {

    @RequestMapping(value="/getSome",method= RequestMethod.GET)
    public String getSome(){
        return "我是第一个服务器";
    }

    @RequestMapping(value="/getOther",method= RequestMethod.GET)
    public String getOther(){
        return "我是第二个服务器";
    }

}

4.启动项目,分别访问http://localhost:8003/getSome, 控制台打印如下信息,微服务整合Spring Cloud Sleuth成功。

2017-11-09 18:26:13.172 DEBUG [microservice-sleuth,c7b2f2d5f1e9412d,c7b2f2d5f1e9412d,false] 9944 --- [nio-8003-exec-5] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/getSome]
2017-11-09 18:26:13.173 DEBUG [microservice-sleuth,c7b2f2d5f1e9412d,c7b2f2d5f1e9412d,false] 9944 --- [nio-8003-exec-5] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/getSome] is: -1
2017-11-09 18:26:13.174 DEBUG [microservice-sleuth,c7b2f2d5f1e9412d,c7b2f2d5f1e9412d,false] 9944 --- [nio-8003-exec-5] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-11-09 18:26:13.174 DEBUG [microservice-sleuth,c7b2f2d5f1e9412d,c7b2f2d5f1e9412d,false] 9944 --- [nio-8003-exec-5] o.s.web.servlet.DispatcherServlet        : Successfully completed request

三、 Spring Cloud Sleuth和Zipkin配合使用

ZipKin是一个开源的分布式跟踪系统,基于Dapper的论文设计而来,其主要功能是手机系统的时序数据,追踪微服务架构的网络延迟。

3.1 编写Zipkin Server

1.新建一个Spring boot工程,添加依赖:

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

2.在启动类添加注解@EnableZipkinServer

package com.swc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.EnableZipkinServer;

@SpringBootApplication
@EnableZipkinServer
public class SpringZipkinServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringZipkinServerApplication.class, args);
    }
}

3.设定端口8004,启动项目,访问http://localhost:8004,出现如下界面。

这里写图片描述

3.2 整合Zipkin

1.复制上面的Spring Cloud Sleuth例子为SpringZipkinClient,在pom文件中添加依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

2.修改配置文件

#指定zipkin的地址
spring.zipkin.base-url=http://localhost:8004
#设置需求采样的请求的百分比
spring.sleuth.sample.percentage=1.0

3.这样项目就整合了Zipkin,重新启动ZipkinServer,先访问http://localhost:8003/getSome 再访问http://localhost:8004
选择起始日期,点击“find traces”,即可看本次请求的详细信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值