使用Spring Cloud Sleuth、Zipkin、Kafka、Elasticsearch实现分布式追踪

使用Spring Cloud Sleuth、Zipkin、Kafka、Elasticsearch实现分布式追踪

环境说明

  • jdk1.8 server 64位
  • intellij IDEA 2018
  • springboot 2.0.4.RELEASE
  • Springcloud Finchley.RELEASE
  • kafka 2.11-1.1.0
  • zookeeper 3.4.6
  • elasticsearch 5.0.2

启动环境

  1. 启动zookeeper3.4.6详见:
    kafka1.1.0版本集群搭建和常用命令
  2. 启动kafka1.1.0,详见:
    kafka1.1.0版本集群搭建和常用命令
  3. 启动elasticsearch5.0.2,详见:
    es开发搭建

启动zipkinserver

curl -sSL https://zipkin.io/quickstart.sh | bash -s
java -jar zipkin.jar

使用kafka传输,es存储,启动命令如下

KAFKA_BOOTSTRAP_SERVERS=192.168.97.211:9192,192.168.97.212:9192,192.168.97.213:9192 STORAGE_TYPE=elasticsearch ES_HOSTS=http://192.168.97.57:9200 java -jar zipkin.jar

参考:

  • zipkin quickstart
  • zipkin-server

启动注册中心

参考注册中心

创建服务sa-sleuth-http,采用http方式

  1. 编写pom依赖
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
    
  1. 配置服务发现和负载均衡
@SpringBootApplication
@EnableDiscoveryClient
public class SaApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(SaApplication.class, args);
    }
}
  1. 配置文件配置
spring:
  application:
    name: sa
  zipkin:
    base-url: http://192.168.97.217:9411
  sleuth:
    sampler:
      probability: 1
server:
  port: 5003

eureka:
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://192.168.97.209:8761/eureka/
  1. 启动服务

创建服务sb-sleuth-kafka,采用kafka方式

  1. 编写pom依赖
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
        
            org.springframework.kafka
            spring-kafka
        
    
  1. 配置服务发现和负载均衡
@SpringBootApplication
@EnableDiscoveryClient
public class SaApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(SaApplication.class, args);
    }
}
  1. 配置文件配置
spring:
  application:
    name: sb
  zipkin:
    sender:
      type: kafka
  sleuth:
    sampler:
      probability: 1
  kafka:
    bootstrap-servers: 192.168.97.211:9192,192.168.97.212:9192,192.168.97.213:9192
server:
  port: 5004
eureka:
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://192.168.97.209:8761/eureka/
  1. 启动服务

zipkin ui

分别请求http://192.168.97.120:5003/test1、http://192.168.97.120:5004/test1,查看zipkin ui,可以看到界面上的请求链路

源码地址

源码地址

你可能感兴趣的:(Sping,Cloud)