使用Spring Cloud Sleuth、Zipkin、Kafka、Elasticsearch实现分布式追踪
程序员文章站
2022-03-01 13:47:16
...
本例使用Spring Cloud Sleuth
来实现分布式追踪,利用Kafka
实现异步的分布式追踪,使用Elasticsearch
存储追踪数据,使用Zipkin
查看追踪内容。
1.前置条件
使用docker compose
安装Kafka
和Elasticsearch
:
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
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
阿里云加速镜像
2.搭建Zipkin Server
依赖
<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>
3.开启Zipkin支持
@SpringBootApplication
@EnableZipkinStreamServer
public class ZipkinServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinServerApplication.class, args);
}
}
配置kafka
spring.cloud.stream.kafka.binder.zkNodes: 192.168.1.222
spring.cloud.stream.kafka.binder.brokers: 192.168.1.222
配置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
4.示例调用服务from-service
依赖
<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>
配置
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
调用控制器
@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);
}
}
5.示例被调用服务to-service
依赖
<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>
配置
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
演示
- 启动
zipkin-server
,访问zipkin页面http://localhost:8080/zipkin/
:
- 启动
from-service
,to-service
,访问http://localhost:8081/sync?param=sss
,这时查看zipkin页面:
源码地址:http://www.wisely.top/2018/03/02/spring_cloud_sleuth-zipkin-kafka-es/
上一篇: kubectl连接多个集群
推荐阅读
-
Spring Cloud 分布式链路跟踪 Sleuth + Zipkin + Elasticsearch【Finchley 版】
-
spring cloud 入门系列八:使用spring cloud sleuth整合zipkin进行服务链路追踪
-
Spring Cloud Sleuth + Zipkin 分布式链路追踪
-
Spring Cloud(十)Sleuth+ZipKin 实现服务追踪(续)
-
跟我学SpringCloud | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
-
Spring Cloud 分布式链路跟踪 Sleuth + Zipkin + Elasticsearch【Finchley 版】
-
spring cloud 入门系列八:使用spring cloud sleuth整合zipkin进行服务链路追踪
-
spring-cloud-sleuth+zipkin追踪服务实现(二)
-
spring-cloud-sleuth+zipkin追踪服务实现(二)
-
跟我学SpringCloud | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪