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. html网页乱码

    html乱码原因与网页乱码解决方法   html乱码原因与网页乱码解决方法,浏览器浏览网页内容出现乱码符合解决篇(html中文乱码) 造成html网页乱码原因主要是html源代码内中文字内容与html ...

  2. JavaScript --css样式

    1.JavaScript显示隐藏控制 隐藏:display:none; 显示:display:block; 参考链接:https://blog.csdn.net/sleepwalker_1992/ar ...

  3. Redis之品鉴之旅(五)

    Redis事务 原子性:就是最小的单位 一致性:好多命令,要么全部执行成功,要么全部执行失败 隔离性:一个会话和另一个会话之间是互相隔离的 持久性:执行了就执行了,数据保存在硬盘上 典型例子:银行转账 ...

  4. AngularJS的简单实用

    Angular Js 的初步认识和使用    一:        1.模块化            定义模块和控制器  ng-app="myapp"  controller=&qu ...

  5. 【vue】获取异步加载后的数据

    异步请求的数据,对它做一些处理,需要怎么做呢?? axios 异步请求数据,得到返回的数据, 赋值给变量 info .如果要对 info 的数据做一些处理后再赋值给 hobby ,直接在 axios ...

  6. FastAPI(59)- 详解使用 OAuth2PasswordBearer + JWT 认证

    JWT JSON Web Tokens 它是一个将 JSON 对象编码为密集且没有空格的长字符串的标准 使用 JWT token 和安全密码 hash 使应用程序真正安全 JWT 小栗子 eyJhbG ...

  7. Apache ShardingSphere 在京东白条场景的落地之旅

    京东白条使用 Apache ShardingSphere 解决了千亿数据存储和扩容的问题,为大促活动奠定了基础. 2014 年初,"京东白条"作为业内互联网信用支付产品,数据量爆发 ...

  8. Data Interoperability Tools

    这里的工具貌似没有对应函数~~~

  9. NOIP&CSP 考前 Dev-cpp 的选项问题和考试心态

    (进入考场后您将获得一个崭新的 \(Dev-cpp\),没有中文,没有编译选项,没有缺省源:我还将获得一个崭新的脑子,没有心态,没有智商,没有调试能力--) 中文 \[Step1 \] \[Step2 ...

  10. VS Code Remote SSH设置

    本文翻译自:5 Steps: Setup VS Code for Remote Development via SSH from Windows to Linux system 5个步骤:设置VS代码 ...