衆所周知,Spring Cloud Sleuth有兩種方式整合Zipkin:
- HTTP直連Zipkin方式
- MQ方式,架構圖如下:
image.png
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> <!-- 使用消息的方式收集資料(使用rabbitmq) --> <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更加的簡化!不過,改動還是比較大的。官方為什麼要做這個改進呢?答案如下——
- Spring Cloud Edgware之前的版本使用
,要想MQ方式收集資料,需整合Zipkin 1.x
。而在Edgware及更高版本中,使用spring-cloud-sleuth-stream
Zipkin 2.x
本身已支援基于MQ的資料收集方式,故而Zipkin 2.x
将被廢棄!spring-cloud-sleuth-stream
- 兩種使用方式不相容,請讀者務必注意!