使用Spring Cloud Sleuth、Zipkin、Kafka、Elasticsearch实现分布式追踪
本例使用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/
相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29