Sleuth学习

Sleuth学习

为什么需要链路监控?

​ 在微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的服务节点调用来协同产生最后的请求结果,每一个前段请求都会形成一条复杂的分布式服务调用链路,链路中的任何一环出现高延时或错误都会引起整个请求最后的失败!

SpringCloud Sleuth是什么?

SpringCloud Sleuth提供了一套完整的服务跟踪解决方案,在分布式系统中提供追踪追踪解决方案并且兼容支持了zipkin!

SpringCloud Sleuth负责收集微服务之间调用的链路信息,zipkin负责以网页的形式展现这些信息!

SpringCloud Sleuth搭建实操

zipkin安装

下载

https://repo1.maven.org/maven2/io/zipkin/zipkin-server/

启动

java -jar zipkin-server-2.23.2-exec.jar

访问管理页面

http://localhost:9411/zipkin

微服务中集成Sleuth

依赖添加

<!--添加Sleuth和zipkin依赖,以下为cloud2020版本添加,之前版本自行百度-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

yaml文件添加监控

spring:
 #zipkin服务所在地址
  zipkin:
    base-url: http://localhost:9411/
  #配置采样百分比,开发环境可以设置为1,表示全部,生产就用默认(0.1),取值范围0.1-1
  sleuth:
    sampler:
      probability: 1

服务提供者编写简单Controller

@RequestMapping("/helloZipkin")
public AjaxResult helloZipkin(){
    return AjaxResult.success("调用成功---helloZipkin");
}

服务调用者编写简单调用==》服务提供者提供的服务

@RequestMapping("/helloZipkinByOrder")
public AjaxResult helloZipkin(){
    return openFeignService.helloZipkin();
}

注意:服务提供者和服务调用者都需要加入Sleuth依赖和yaml配置

测试请求

http://localhost/order/helloZipkinByOrder

访问zipkin页面分析调用链路监控

http://localhost:9411/zipkin

参考项目

服务调用者:openFeign-order-80

服务提供者:provider-payment001

posted @ 2021-08-09 23:27  幸运刘  阅读(46)  评论(0)    收藏  举报