Spring Cloud Sleuth对Zipkin整合进行了自动化配置的封装,可以很轻松引入和使用。
- Java 8及以上版本
- Spring Cloud Finchley
- Maven 3.6.3 及以上版本
第一步:搭建Zipkin Server
- 创建一个基础的Spring Boot应用,命名为zipkin-server,并在pom.xml中引入Zipkin Server的相关依赖,具体如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>zipkin-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zipkin-server</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.11.8</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.11.8</version>
</dependency>
</dependencies>
</project>
- 创建应用主类,使用@EnableZipkinServer注解来启动Zipkin Server,具体如下:
@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class ZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinApplication.class, args);
}
}
- 在application.properties中做一些简单配置,比如设置服务端口号为9411
server:
port: 9411
spring:
application:
name: zipkin-server
eureka:
client:
service-url:
#注册地址
defaultZone: http://root:root@localhost:8082/eureka/
创建完上述工程之后,将其启动,并访问:http://127.0.0.1:9411/zipkin/
第二部:为应用引入和配置Zipkin服务
- 在hello-service和ribbon-consumer的application.yml中增加ZipkinServer的配置,具体如下:
hello-service的application.yml
server:
port: 8085
spring:
application:
name: hello-service
zipkin:
base-url: http://localhost:9411/
eureka:
client:
service-url:
#注册地址
defaultZone: http://root:root@localhost:8082/eureka/
ribbon-consumer 的application.yml
server:
port: 9000
spring:
application:
name: ribbon-consumer
zipkin:
base-url: http://localhost:9411/
eureka:
client:
service-url:
#注册地址
defaultZone: http://root:root@localhost:8082/eureka/
测试与分析
依次eureka-server、hello-service、ribbon-consumer启动起来,然后做一些测试。
向hello-servie的接口发送几个请求(http://localhost:9000/hello)。日志跟踪信息最后一个值为true的时候,说明该跟踪信息会输出给Zipkin Server,在Zipkin Server的管理界面(http://127.0.0.1:9411/zipkin/)中选择合适的查询条件,单击Find Traces按钮,查询出日志中出现的跟踪信息,页面如下:
单击下方hello-service端点的跟踪信息,可以看到sleuth跟踪到的详细信息,包含请求事件消耗等。
sleuth跟踪到的详细信息: