SpringCloud 链路追踪应用

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


目录(?)[-]

    一简介
    二服务追踪分析
    三术语
    四构建工程
        41 构建server-zipkin
        42 创建service-hi
        43 创建service-miya
        44 启动工程演示追踪
    五参考资料
    优秀文章推荐

这篇文章主要讲述服务追踪组件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-boot工程取名为server-zipkin,在其pom引入依赖:


<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>

    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
    40

    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
    40

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

@SpringBootApplication
@EnableZipkinServer
public class ServerZipkinApplication {

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

指定端口:

server.port=9411

    1

    1

4.2 创建service-hi

在其pom引入依赖:

<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>

    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

    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

在其配置文件指定“spring.zipkin.base-url”:


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

    1
    2
    3
    4
    5

    1
    2
    3
    4
    5

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

对外暴露接口:

@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();
    }
}


    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
    40

    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
    40

4.3 创建service-miya

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

对外暴露接口:


@SpringBootApplication
@RestController
public class ServiceMiyaApplication {

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

    private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName());


    @RequestMapping("/hi")
    public String home(){
        LOG.log(Level.INFO, "hi is being called");
        return "hi i'm miya!";
    }

    @RequestMapping("/miya")
    public String info(){
        LOG.log(Level.INFO, "info is being called");
        return restTemplate.getForObject("http://localhost:8988/info",String.class);
    }

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

    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

    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

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/chapter9
五、参考资料

spring-cloud-sleuth

利用Zipkin对Spring Cloud应用进行服务追踪分析

Spring Cloud Sleuth使用简介
优秀文章推荐:

    史上最简单的 SpringCloud 教程 | 终章
    史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
    史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)


本文出自方志朋的博客, 原文地址:http://blog.csdn.net/forezp/article/details/70162074


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Cloud 可以通过使用 Sleuth 库来实现链路追踪。 Sleuth 是一个用于在分布式系统中跟踪请求的库,它会在请求中注入一些信息,如请求 ID、调用链等,这些信息可以在整个请求的生命周期中传递。 首先,需要在 Spring Boot 应用中添加 Sleuth 的依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> ``` 然后,需要配置日志系统,以便 Sleuth 能够记录请求信息。比如,可以使用 Logback 和 SLF4J 进行配置: ``` <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> ``` 最后,可以使用各种工具(如 Zipkin、Elastic Stack 等)来收集和展示请求信息。 这样,就可以在分布式系统中实现链路追踪了。 ### 回答2: Spring Cloud提供了一种称为"Sleuth"的组件,它可以实现链路追踪链路追踪的目的是可视化和监控微服务架构中的请求流。下面是Spring Cloud Sleuth如何实现链路追踪的步骤: 1. 引入依赖:在项目的pom.xml文件中,引入Spring Cloud Sleuth的依赖。例如: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> ``` 2. 配置跟踪信息:在微服务的配置文件(例如application.yml)中,配置服务的名称(spring.application.name)和端口(server.port)。例如: ```yaml spring: application: name: your-service-name server: port: 8080 ``` 3. 启用链路追踪:在微服务的主类上,使用`@EnableSleuth`注解来启用Spring Cloud Sleuth的链路追踪功能。例如: ```java @SpringBootApplication @EnableSleuth public class YourServiceApplication { public static void main(String[] args) { SpringApplication.run(YourServiceApplication.class, args); } } ``` 4. 发起请求:通过HTTP或其他方式在微服务之间发起请求。每个请求都会包含一个唯一的跟踪ID。 5. 查看链路追踪信息:在日志中,可以看到每个请求的跟踪ID和相应的父跟踪ID。这些信息可以帮助我们追踪和分析请求的流动。 通过使用Spring Cloud Sleuth,我们可以轻松地实现在微服务架构中进行链路追踪。这对于追踪和分析请求流是非常有价值的,可以提高微服务系统的性能和可维护性。 ### 回答3: Spring Cloud提供了一种称为Sleuth的链路追踪解决方案,可以帮助我们追踪分布式系统中的请求流程。下面是Spring Cloud Sleuth实现链路追踪的步骤: 1. 配置依赖:在项目的pom.xml文件中,添加Spring Cloud Sleuth的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> ``` 2. 配置日志:在系统的主配置文件中,配置日志相关的属性,例如日志输出格式、日志级别等。 3. 创建并配置Zipkin服务器:Zipkin是一个用于收集、存储和展示追踪数据的服务器。我们可以使用Docker或者下载其JAR包来启动Zipkin服务器。 4. 配置请求追踪:在项目的主类上添加@EnableZipkinServer注解,启用Zipkin追踪。 5. 添加日志跟踪:在程序的关键组件中(例如Controller层、Service层)添加相关注解,Spring Cloud Sleuth会自动为每个请求生成一个唯一的请求ID,并将该ID添加到日志中。 这样,当有请求进入系统时,系统就会自动为该请求生成一个唯一的ID,然后在请求经过不同的模块和服务时,这个ID会在日志中进行传递和记录。通过查看日志信息,我们可以很方便地追踪请求在系统中的流向和调用。 总之,通过使用Spring Cloud Sleuth,我们可以很容易地实现链路追踪功能,帮助我们分析和排查分布式系统中的问题,并提高系统的可用性和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值