Spring Cloud 分布式应用跟踪
SpanID:阶段性ID,比如一次RPC有可能多Span
TraceID:一次RPC只有一个TraceID
整合Spring Cloud Sleuth
1.增加依赖
<!-- 整合 Spring Cloud Sleuth -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
Zipkin整合
新增Zipkin服务器
增加Maven依赖
<!-- Zipkin Server 整合 -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<!-- Zipkin Server UI 整合 -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
激活Zipkin服务器
HTTP方式采集
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.EnableZipkinServer;
/**
* Zipkin 服务器应用
*
*/
@SpringBootApplication
@EnableZipkinServer
public class ZipkinServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinServerApplication.class, args);
}
}
配置Zipkin服务器
## 应用元信息
## Zipkin 服务器应用名称
spring.application.name = zipkin-server
## Zipkin 服务器服务端口
server.port = 20000
## 管理端口安全失效
management.security.enabled = false
整合Zipkin客户端
改造user-service-client
HTTP方式上报
增加Maven依赖
<!-- 整合 Spring Cloud Zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
配置:连接zipkin服务器
## Zipkin 配置
### 配置 Zipkin 服务器
zipkin.server.host = localhost
zipkin.server.port = 20000
spring.zipkin.base-url = http://${zipkin.server.host}:${zipkin.server.port}
改造user-service-provider
增加Maven依赖
<!-- 整合 Spring Cloud Zipkin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
配置:连接zipkin服务器
## Zipkin 配置
### 配置 Zipkin 服务器
zipkin.server.host = localhost
zipkin.server.port = 20000
spring.zipkin.base-url = http://${zipkin.server.host}:${zipkin.server.port}
改造zipkin服务器:使用Stream方式订阅
增加Maven依赖
<!-- zipkin 整合 Spring Cloud Sleuth Stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<!-- 整合 Spring Cloud Stream Binder Rabbit MQ -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
替换激活注解:@EnableZipkinStreamServer
Stream方式采集
package com.segumentfault.spring.cloud.lesson15.zipkin.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer;
/**
* Zipkin 服务器应用
*
*/
@SpringBootApplication
//@EnableZipkinServer
@EnableZipkinStreamServer
public class ZipkinServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinServerApplication.class, args);
}
}
改造user-service-client
增加Maven依赖
stream方式上报
<!-- 整合 Spring Cloud Sleuth -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!-- 整合 Spring Cloud Sleuth Stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
<!-- 整合 Spring Cloud Stream Binder Rabbit MQ -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
注意要移除之前zipkin依赖
改造user-service-provider
增加Maven依赖
Stream方式上报
<!-- 整合 Spring Cloud Sleuth -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!-- 整合 Spring Cloud Sleuth Stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
<!-- 整合 Spring Cloud Stream Binder Rabbit MQ -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
注意要移除zipkin依赖