1. 概述

老话说的好:安全不能带来财富,但盲目的冒险也是不可取的,大胆筹划,小心实施才是上策。

言归正传,微服务的特点就是服务多,服务间的互相调用也很复杂,就像一张关系网,因此为了更好的定位故障和优化性能,我们需要有工具帮我们很快的梳理出服务间上下游的调用关系。Sleuth 就可以很好的帮我们解决这个问题。

但 Sleuth 只是做了后台工作,没有展示页面,不方便我们对调用链进行分析。因此需要 Zipkin 去把数据展示出来,方便我们进行分析。

闲话不多说,直接上代码。

2. Zipkin 服务的搭建

2.1 Zipkin 服务的搭建方式

我们打开Zipkin的官网地址:https://zipkin.io/pages/quickstart.html

如图所示,Zipkin Server 支持 Docker方式启动,Java 运行 Jar 包方式启动,以及编译源码的方式启动。

2.2 Java 运行 jar 包方式启动

大家可以根据自己的喜好选择启动方式,这里我们选择 Java 运行 jar 包的方式启动。

1)下载 zipkin.jar

# curl -sSL https://zipkin.io/quickstart.sh | bash -s

2)启动 Zipkin Server

# java -jar zipkin.jar

2.3 访问 Zipkin Server

http://IP:9411/zipkin/

3. Demo 服务的搭建

3.1 概述

这里我们搭建两个有调用关系的服务作为 Demo。

两个服务都依赖 Sleuth 和 Zipkin。

3.2 my-sleuth-A 的搭建

3.2.1 主要依赖

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>

3.2.2 主要配置

server:
port: 46000
spring:
application:
name: my-sleuth-A
sleuth:
sampler:
probability: 1 # 采样率100%
zipkin:
base-url: http://192.168.1.22:9411  # Zipkin Server 地址 eureka:
client:
service-url:
defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/ # Eureka Server的地址

3.2.3 启动类

@SpringBootApplication
@EnableDiscoveryClient
public class MySleuthAApplication { @LoadBalanced
@Bean
public RestTemplate lb() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(MySleuthAApplication.class, args);
}
}

3.2.4 Demo Controller

@RestController
@Slf4j
public class Controller { @Autowired
private RestTemplate restTemplate; @GetMapping("/traceA")
public String traceA() {
log.info("-------- traceA -------");
return restTemplate.getForEntity
("http://my-sleuth-B/traceB", String.class).getBody();
}
}

3.3 my-sleuth-B 的搭建

3.3.1 主要依赖

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>

3.3.2 主要配置

server:
port: 46001
spring:
application:
name: my-sleuth-B
sleuth:
sampler:
probability: 1 # 采样率100%
zipkin:
base-url: http://192.168.1.22:9411 # Zipkin Server 地址 eureka:
client:
service-url:
defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/ # Eureka Server的地址

3.3.3 启动类

@SpringBootApplication
@EnableDiscoveryClient
public class MySleuthBApplication { @LoadBalanced
@Bean
public RestTemplate lb() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(MySleuthBApplication.class, args);
}
}

3.3.4 Demo Controller

@RestController
@Slf4j
public class Controller { @Autowired
private RestTemplate restTemplate; @GetMapping("/traceB")
public String traceB() {
log.info("-------- traceB -------");
return "traceB";
}
}

4. Zipkin 的简单使用

4.1 调用 Demo 接口

调用两个Demo工程的接口

GET http://localhost:46001/traceB

GET http://localhost:46000/traceA

4.2 打开 zipkin 查看

4.3 依赖关系图查看 

5. 综述

今天聊了一下 调用链追踪 Sleuth + Zipkin ,希望可以对大家的工作有所帮助。

欢迎帮忙点赞、评论、转发、加关注 :)

关注追风人聊Java,每天更新Java干货。

6. 个人公众号

追风人聊Java,欢迎大家关注

SpringCloud 2020.0.4 系列之 Sleuth + Zipkin的更多相关文章

  1. SpringCloud 2020.0.4 系列之 Feign

    1. 概述 老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解. 言归正传,之前我们聊了 SpringCloud 的服务治理组件 Eureka,今天我 ...

  2. SpringCloud 2020.0.4 系列之 Stream 消息广播 与 消息分组 的实现

    1. 概述 老话说的好:事情太多,做不过来,就先把事情记在本子上,然后理清思路.排好优先级,一件一件的去完成. 言归正传,今天我们来聊一下 SpringCloud 的 Stream 组件,Spring ...

  3. SpringCloud 2020.0.4 系列之 Stream 延迟消息 的实现

    1. 概述 老话说的好:对待工作要有责任心,不仅要完成自己的部分,还要定期了解整体的进展. 言归正传,我们在开发产品时,常常会遇到一段时间后检查状态的场景,例如:用户下单场景,如果订单生成30分钟后, ...

  4. SpringCloud 2020.0.4 系列之 Stream 消息出错重试 与 死信队列 的实现

    1. 概述 老话说的好:出错不怕,怕的是出了错,却不去改正.如果屡次出错,无法改对,就先记下了,然后找援军解决. 言归正传,今天来聊一下 Stream 组件的 出错重试 和 死信队列. RabbitM ...

  5. SpringCloud 2020.0.4 系列之Eureka

    1. 概述 老话说的好:遇见困难,首先要做的是积极的想解决办法,而不是先去泄气.抱怨或生气. 言归正传,微服务是当今非常流行的一种架构方式,其中 SpringCloud 是我们常用的一种微服务框架. ...

  6. SpringCloud 2020.0.4 系列之服务降级

    1. 概述 老话说的好:做人要正直,做事要正派,胸怀坦荡.光明磊落,才会赢得他人的信赖与尊敬. 言归正传,之前聊了服务间通信的组件 Feign,今天我们来聊聊服务降级. 服务降级简单的理解就是给一个备 ...

  7. SpringCloud 2020.0.4 系列之 Bus

    1. 概述 老话说的好:会休息的人才更会工作,身体是革命的本钱,身体垮了,就无法再工作了. 言归正传,之前我们聊了 SpringCloud 的 分布式配置中心 Config,文章里我们聊了config ...

  8. SpringCloud 2020.0.4 系列之 Gateway入门

    1. 概述 老话说的好:做人要有幽默感,懂得幽默的人才会活的更开心. 言归正传,今天我们来聊聊 SpringCloud 的网关组件 Gateway,之前我们去访问 SpringCloud 不同服务的接 ...

  9. SpringCloud 2020.0.4 系列之 JWT用户鉴权

    1. 概述 老话说的好:善待他人就是善待自己,虽然可能有所付出,但也能得到应有的收获. 言归正传,之前我们聊了 Gateway 组件,今天来聊一下如何使用 JWT 技术给用户授权,以及如果在 Gate ...

随机推荐

  1. Faster RCNN 改进论文及资料

    1,面向小目标的多尺度Faster RCNN检测算法 黄继鹏等 对高分辨率图像进行下采样和上采样,使得网上获取的数据与实际测试数据分布接近. 下采样:最大池化和平均池化 上采样:线性插值,区域插值,最 ...

  2. git 切换分支

      # 查看git源 git  remote -v git remote set-url origin http://mingzhanghui@xx.xx.xx.xx:8090/r/ENSO/weba ...

  3. webpack基本用法及原理(10000+)

    1 webpack是什么 所有工具的出现,都是为了解决特定的问题,那么前端熟悉的webpack是为了解决什么问题呢? 1.1 为什么会出现webpack js模块化: 浏览器认识的语言是HTML,CS ...

  4. 《手把手教你》系列技巧篇(三十)-java+ selenium自动化测试- Actions的相关操作下篇(详解教程)

    1.简介 本文主要介绍两个在测试过程中可能会用到的功能:Actions类中的拖拽操作和Actions类中的划取字段操作.例如:需要在一堆log字符中随机划取一段文字,然后右键选择摘取功能. 2.拖拽操 ...

  5. 学习笔记——不带修序列莫队 (luogu2079)小B的询问

    莫队是一种对于询问的离线算法 时间复杂度:O(\(n \sqrt n\)) 大致思想就是 首先将询问离线,然后对原序列分块,使得每一个\(l和r\)都在一个块里 然后按照左节点排序,若所在的块相等,就 ...

  6. JSP(java server pages)安装开发和执行环境

    JSP是一种动态网页技术标准. 它是在传统的网页HTML文件中插入Java程序段(Scriptlet)和JSP标记(tag)的.jsp文件: java程序段:操纵数据库,重新定向网页,发送email等 ...

  7. 【UE4】 补丁Patch 与 DLC

    概述 UE4 中主要使用 Project Launcher 来进行补丁和DLC的制作 补丁与 DLC 都需要基于某个版本而制作 补丁 与 DLC 最后以 Pak 形式表现, 补丁的 pak 可以重命名 ...

  8. 改善深层神经网络-week3编程题(Tensorflow 实现手势识别 )

    TensorFlow Tutorial Initialize variables Start your own session Train algorithms Implement a Neural ...

  9. time_formatter攻防世界学习

    time_formatter 前言:这题说实话分析量蛮大的,首先是程序内壁比较绕,而且调用了之前许多没有见到的函数---如snprintf_che,以及strsup(好像打错了),getegid(), ...

  10. Noip模拟12 2021.7.12

    T1 interval 亏得昨天晚上改掉了T3并且理解了单调栈,今天一扫这题目就知道要用啥了. 先预处理出以a[i]为最大值的最大左右区间.然后再将a[i]取%!!!是的,要不然会影响单调栈的使用.. ...