在微服务架构中可以使用Zipkin来追踪服务调用链路,可以知道各个服务的调用依赖关系。在Spring Cloud中,也提供了Spring Cloud Sleuth来方便集成Zipkin实现。

本文使用一个Zipkin Server,用户微服务,电影微服务来实现。完整实现的代码放在github中:

Zipkin Server

Zipkin可以不配置数据库,但跟踪的数据只存在内在中,不能长久保存,因此这里使用mysql存储跟踪数据。项目中还使用了rabbitMQ作为消息中间件进行数据收集,实现Zipkin与微服务的解耦。

添加依赖

新建一个Spring Boot项目,添加以下依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.zipkin.java/zipkin-autoconfigure-storage-mysql -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<!--引入SpringCloud 依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

添加注解

在启动类上添加注解:

1
@EnableZipkinStreamServer

修改配置

配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring:
application:
name: microservice-trace-zipkin-server-stream-mysql
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
datasource:
schema: classpath:/mysql.sql
url: jdbc:mysql://localhost:3306/zipkin
username: root
password: 123456
zipkin:
storage:
type: mysql

server:
port: 9411

mysql.sql文件可以在上面项目地址中找到,首先需要在Mysql数据库中新建zipkin数据库,在项目启动时会自动执行mysql.sql

RabbitMQ的安装与配置可以参考:RabbitMQ学习系列:一、RabbitMQ 的安装

微服务整合Zipkin

用户微服务与电影微服务作一样的修改。

##添加依赖

主要需要添加以下依赖:

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

修改配置

在配置文件中添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
application:
name: microservice-simple-consumer-movie-trace-zipkin
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
percentage: 1.0
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest

启动测试

完成两个微服务的整合修改后,首先启动rabbitmq,保证mysql可以连通。分别启动Zipkin Server,用户微服务,电影微服务。

为了能看到跟踪数据,我们先访问服务让其产生数据:http://localhost:8011/user/1

再方便Zipink Server地址查看:http://localhost:9411/zipkin/

Zipkin

选择我们需要查看的时间点,点击 Find Traces我们就能看到跟踪数据了。

点击导航栏上的Dependencies可以查看服务依赖

Dependencies

这里我们可以看到服务的调用方向。

打开Mysql数据库也可以看到跟踪数据已经被存储了:

mysql存储

这样即使Zipkin被关闭,跟踪数据也不会丢失。