第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

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

一、简介

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

二、服务追踪分析

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

三、构建工程

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.gblfy</groupId>
        <artifactId>sc-f-chapter9</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.gblfy</groupId>
    <artifactId>service-zipkin</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>service-zipkin</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>
</project>

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

server:
  port: 8988
spring:
  application:
    name: service-zipkin
  zipkin:
    base-url: http://localhost:9411/
    sender:
      type: web
  sleuth:
      sampler:
        probability: 1
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就可以了
启动类作以下修改:

@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class ServiceZipkinApplication {

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

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

    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }
}

对外暴露接口:

@RestController
public class ClientController {

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

    @Autowired
    private RestTemplate 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";

    }
}

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.gblfy</groupId>
        <artifactId>sc-f-chapter9</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.gblfy</groupId>
    <artifactId>service-miya</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>service-miya</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>

</project>
server:
  port: 8989
spring:
  zipkin:
    base-url: http://localhost:9411/
    sender:
      type: web
  application:
    name: service-miya
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

启动类作以下修改:

@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class ServiceMiyaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceMiyaApplication.class, args);
    }
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }


    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }
}

对外暴露接口:

@RestController
public class ClientController {

    private static final Logger LOG = Logger.getLogger(ClientController.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;
}

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

访问:http://localhost:8989/miya,

浏览器出现:

i’m service-hi

再打开http://localhost:9411/的界面,点击Dependencies,可以发现服务的依赖关系:
点击find traces,可以看到具体服务相互调用的数据:

本文源码下载:

https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Spring Cloud Sleuth是一个分布式跟踪解决方案,它可以帮助开发人员在微服务架构中追踪请求的流程和调用链路。它通过为每个请求生成唯一的跟踪ID和跨服务的调用ID来实现这一目标。这些ID可以用于跟踪请求的流程和调用链路,从而帮助开发人员快速诊断和解决问题。Spring Cloud Sleuth还提供了一些可视化工具,如Zipkin,可以帮助开发人员更好地理解和分析跟踪数据。 ### 回答2: SpringCloud Sleuth是一个基于日志的分布式跟踪方案,它可以用于解决微服务架构下的分布式系统的链路追踪问题。在分布式系统中,一个请求经常会穿越多个服务,从而会形成一条复杂的链路,如果有一台或多台机器对此进行记录,那么将能够轻松地查看和理解一个请求的完整路径。这些信息能够帮助我们更快地定位问题所在,提高系统可靠性和稳定性。 Sleuth使用了Zipkin的架构和数据模型,通过在每个请求的Header中添加Trace Id和Span Id来实现链路追踪。Trace Id表示整个请求链路,Span Id表示每一个服务的一个简单步骤。使用这两个 Id,我们就可以将整个链路追踪下来,使得对请求的监测、记录和分析变得更加容易。 Sleuth结合了Spring Cloud日志管理和Zipkin的功能,能够自动收集各个微服务的请求跟踪信息,并将其发送到Zipkin服务器进行聚合分析,视图展现。通过Sleuth的ChainInvoker,可以实现对所有链路的统一管理。当一条请求跨越多个服务时,Sleuth会为每个服务实例生成唯一的spanId,并将这个spanId沿用到下一个服务实例,从而使得整条链路保留了完整的信息。此外,Sleuth还支持基于日志的采样策略和数据比较高效的存储,保证了高性能的分布式链路追踪Sleuth的主要应用场景是微服务架构下的链路跟踪和性能监控。微服务架构中有大量的服务服务之间的关系错综复杂,因此链路追踪对于排查问题、优化性能非常重要。Sleuth能够方便地实现链路追踪和监测,并帮助我们快速定位问题所在,提高系统的可靠性和稳定性。 ### 回答3: Spring Cloud Sleuth 链路追踪Spring Cloud服务架构中的一项重要的功能模块。通过 Sleuth 链路追踪,我们可以跟踪整个分布式系统中的请求链路,从而了解每个操作所花费的时间、调用的服务以及调用顺序。在微服务架构中,服务调用会涉及到多个服务之间的协作,使用 Sleuth 链路追踪可以帮助我们很好地理解系统在内部的调用过程。 Sleuth 链路追踪的原理是在每个服务的请求中添加唯一的追踪 ID,通过这个追踪 ID,Sleuth 可以实现将每个请求相关的服务调用串联起来,形成完整的请求链路。追踪 ID 通常被称为 Trace ID,它作为请求的一部分,从前端发起请求的服务开始一直传递到最后一个服务。 通过 Sleuth 链路追踪,我们可以了解每个调用的服务名和 IP 地址,以及请求的耗时情况,在调试分布式系统时非常实用。此外,Sleuth 还支持将链路追踪信息集成到日志系统中,从而更好地协助开发人员进行故障排查。 Sleuth 链路追踪还提供了 Zipkin 集成,Zipkin 是一个开源的分布式链路追踪系统,可以将链路数据可视化显示,并提供了一些分析工具,帮助开发人员更好地理解系统的调用情况。 总之,Spring Cloud Sleuth 链路追踪是一个非常实用的工具,可以帮助我们更好地理解分布式系统中服务调用的情况,有效地解决微服务架构中的复杂度和故障排查的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gblfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值