Spring Cloud Edgware新特性之九:Sleuth使用MQ方式整合Zipkin

时间:2022-05-06
本文章向大家介绍Spring Cloud Edgware新特性之九:Sleuth使用MQ方式整合Zipkin,主要内容包括Dalston及更低版本、微服务端、Edgware及更高版本、微服务端、对比及分析、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

众所周知,Spring Cloud Sleuth有两种方式整合Zipkin:

  • HTTP直连Zipkin方式
  • MQ方式,架构如下图:

Spring Cloud Edgware及更高版本中,Sleuth使用MQ方式整合Zipkin的玩法发生了巨大改变。本文将贴出新旧版本中Sleuth如何整合Zipkin的具体操作。MQ使用的是RabbitMQ(读者也可使用Kafka)。

Dalston及更低版本

如果您使用Spring Cloud Dalston或更低版本,那么整合步骤如下:

服务器端

一、依赖:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
  <groupId>io.zipkin.java</groupId>
  <artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>

二、启动类上添加注解 @EnableZipkinStreamServer

三、配置:

server:
  port: 9411
spring:  
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

微服务端

一、加依赖:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

二、配置:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

Edgware及更高版本

对于Edgware,以上方式也可使用,但目前已标注为 废弃 ,未来将被删除!

从Edgware开始,应如下整合Zipkin——

服务器端

一、依赖:

<dependency>
  <groupId>io.zipkin.java</groupId>
  <artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
  <groupId>io.zipkin.java</groupId>
  <artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
  <groupId>io.zipkin.java</groupId>
  <artifactId>zipkin-autoconfigure-collector-rabbitmq</artifactId>
  <version>2.3.1</version>
</dependency>

二、启动类上添加 @EnableZipkinServer

三、配置:

server:
  port: 9411
zipkin:
  collector:
    rabbitmq:
      addresses: localhost:5672
      password: guest
      username: guest
      queue: zipkin

微服务端

一、依赖:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.amqp</groupId>
  <artifactId>spring-rabbit</artifactId>
</dependency>

二、配置:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  zipkin:
    rabbitmq:
      queue: zipkin

对比及分析

对比后不难发现,从Edgware开始,Sleuth基于MQ整合Zipkin更加的简化!不过,改动还是比较大的。官方为什么要做这个改进呢?答案如下——

  1. Spring Cloud Edgware之前的版本使用 Zipkin1.x ,要想MQ方式收集数据,需整合 spring-cloud-sleuth-stream 。而在Edgware及更高版本中,使用 Zipkin2.xZipkin2.x 本身已支持基于MQ的数据收集方式,故而 spring-cloud-sleuth-stream 将被废弃!
  2. 两种使用方式不兼容,请读者务必注意!