---
title: "使用Spring Cloud Sleuth、Zipkin、Kafka、Elasticsearch实现分布式追踪"
author: "wiselyman"
date: "2018-03-02T11:24:28Z"
tags: []
categories: ["企业架构"]
slug: "spring-cloud-sleuth-zipkin-kafka-elasticsearch"
draft: false
lastmod: "2018-03-02T11:24:28Z"
id: 2412005
url: "https://wiselyman.iteye.com/blog/2412005"
---

本例使用`Spring Cloud Sleuth`来实现分布式追踪，利用`Kafka`实现异步的分布式追踪，使用`Elasticsearch`存储追踪数据，使用`Zipkin`查看追踪内容。

## [](https://github.com/wiselyman/distributed-tracing#1%E5%89%8D%E7%BD%AE%E6%9D%A1%E4%BB%B6)1.前置条件

使用`docker compose`安装`Kafka`和`Elasticsearch`：

### [](https://github.com/wiselyman/distributed-tracing#kafka%E5%AE%89%E8%A3%85)Kafka安装

```
version: '2'

services:
  zk:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"


  kafka:
    image: wurstmeister/kafka:0.10.1.1
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ZOOKEEPER_CONNECT: zk:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
```

### [](https://github.com/wiselyman/distributed-tracing#elasticsearch%E5%AE%89%E8%A3%85)Elasticsearch安装

```
version: '2'

services:
  elasticsearch:
    image: registry.cn-hangzhou.aliyuncs.com/wiselyman/es553:5.5.3
    restart: on-failure
    ports:
      - '9200:9200'
      - '9300:9300'
```

> `registry.cn-hangzhou.aliyuncs.com/wiselyman/es553:5.5.3`是`docker.elastic.co/elasticsearch/elasticsearch:5.5.3`阿里云加速镜像

## [](https://github.com/wiselyman/distributed-tracing#2%E6%90%AD%E5%BB%BAzipkin-server)2.搭建Zipkin Server

### [](https://github.com/wiselyman/distributed-tracing#%E4%BE%9D%E8%B5%96)依赖

```
<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-kafka</artifactId>
</dependency>

<dependency>
	<groupId>io.zipkin.java</groupId>
	<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>

<dependency>
	<groupId>io.zipkin.java</groupId>
	<artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
	<version>2.4.1</version>
</dependency>
```

### [](https://github.com/wiselyman/distributed-tracing#3%E5%BC%80%E5%90%AFzipkin%E6%94%AF%E6%8C%81)3.开启Zipkin支持

```
@SpringBootApplication
@EnableZipkinStreamServer
public class ZipkinServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ZipkinServerApplication.class, args);
	}
}
```

### [](https://github.com/wiselyman/distributed-tracing#%E9%85%8D%E7%BD%AEkafka)配置kafka

```
spring.cloud.stream.kafka.binder.zkNodes: 192.168.1.222
spring.cloud.stream.kafka.binder.brokers: 192.168.1.222
```

### [](https://github.com/wiselyman/distributed-tracing#%E9%85%8D%E7%BD%AEes%E5%AD%98%E5%82%A8)配置ES存储

```
zipkin:
  storage:
    type: elasticsearch
    elasticsearch:
      hosts: http://192.168.1.222:9200
      username: elastic
      password: changeme
      cluster: elasticsearch
      index: zipkin
      index-shards: 1
      index-replicas: 1
```

## [](https://github.com/wiselyman/distributed-tracing#4%E7%A4%BA%E4%BE%8B%E8%B0%83%E7%94%A8%E6%9C%8D%E5%8A%A1from-service)4.示例调用服务`from-service`

### [](https://github.com/wiselyman/distributed-tracing#%E4%BE%9D%E8%B5%96-1)依赖

```
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
```

### [](https://github.com/wiselyman/distributed-tracing#%E9%85%8D%E7%BD%AE)配置

```
spring.application.name: from-service
server.port: 8081

spring.sleuth.sampler.percentage: 1.0

spring.cloud.stream.kafka.binder.zkNodes: 192.168.1.222
spring.cloud.stream.kafka.binder.brokers: 192.168.1.222
```

### [](https://github.com/wiselyman/distributed-tracing#%E8%B0%83%E7%94%A8%E6%8E%A7%E5%88%B6%E5%99%A8)调用控制器

```
@RestController
public class HelloController {
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/sync")
    public String sync(@RequestParam String param){
        return restTemplate.getForObject("http://localhost:8082/toSync?param=" + param,String.class);
    }
}
```

## [](https://github.com/wiselyman/distributed-tracing#5%E7%A4%BA%E4%BE%8B%E8%A2%AB%E8%B0%83%E7%94%A8%E6%9C%8D%E5%8A%A1to-service)5.示例被调用服务`to-service`

### [](https://github.com/wiselyman/distributed-tracing#%E4%BE%9D%E8%B5%96-2)依赖

```
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
```

### [](https://github.com/wiselyman/distributed-tracing#%E9%85%8D%E7%BD%AE-1)配置

```
spring.application.name: to-service
server.port: 8082

spring.sleuth.sampler.percentage: 1.0

spring.cloud.stream.kafka.binder.zkNodes: 192.168.1.222
spring.cloud.stream.kafka.binder.brokers: 192.168.1.222
```

## [](https://github.com/wiselyman/distributed-tracing#%E6%BC%94%E7%A4%BA)演示

- 启动`zipkin-server`，访问zipkin页面`http://localhost:8080/zipkin/`:

<!--THE END-->

[![](https://github.com/wiselyman/distributed-tracing/raw/master/images/tracing1.png)](https://github.com/wiselyman/distributed-tracing/blob/master/images/tracing1.png)

- 启动`from-service`,`to-service`,访问`http://localhost:8081/sync?param=sss`，这时查看zipkin页面:

[![](https://github.com/wiselyman/distributed-tracing/raw/master/images/tracing2.png)](https://github.com/wiselyman/distributed-tracing/blob/master/images/tracing2.png)[![](https://github.com/wiselyman/distributed-tracing/raw/master/images/tracing3.png)](https://github.com/wiselyman/distributed-tracing/blob/master/images/tracing3.png)[![](https://github.com/wiselyman/distributed-tracing/raw/master/images/tracing4.png)](https://github.com/wiselyman/distributed-tracing/blob/master/images/tracing4.png)

源码地址:[http://www.wisely.top/2018/03/02/spring\_cloud\_sleuth-zipkin-kafka-es/](http://www.wisely.top/2018/03/02/spring_cloud_sleuth-zipkin-kafka-es/)