SpringCloud+Sleuth+Zipkin分布式服务跟踪

Spring Cloud Sleuth对Zipkin整合进行了自动化配置的封装,可以很轻松引入和使用。

  • Java 8及以上版本
  • Spring Cloud Finchley
  • Maven 3.6.3 及以上版本
第一步:搭建Zipkin Server
  • 创建一个基础的Spring Boot应用,命名为zipkin-server,并在pom.xml中引入Zipkin Server的相关依赖,具体如下:
 <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <groupId>org.example</groupId>
          <artifactId>zipkin-server</artifactId>
          <version>1.0-SNAPSHOT</version>
          <packaging>jar</packaging>
      
          <name>zipkin-server</name>
          <url>http://maven.apache.org</url>
      
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.0.4.RELEASE</version>
          </parent>
      
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-dependencies</artifactId>
                      <version>Finchley.RELEASE</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
      
              </dependencies>
          </dependencyManagement>
      
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <maven.compiler.source>1.8</maven.compiler.source>
              <maven.compiler.target>1.8</maven.compiler.target>
          </properties>
      
          <dependencies>
      
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
              </dependency>
      
              <dependency>
                  <groupId>io.zipkin.java</groupId>
                  <artifactId>zipkin-server</artifactId>
                  <version>2.11.8</version>
              </dependency>
      
              <dependency>
                  <groupId>io.zipkin.java</groupId>
                  <artifactId>zipkin-autoconfigure-ui</artifactId>
                  <version>2.11.8</version>
              </dependency> 
          </dependencies>
          
      </project>
  • 创建应用主类,使用@EnableZipkinServer注解来启动Zipkin Server,具体如下:
    @SpringBootApplication
    @EnableEurekaClient
    @EnableZipkinServer
    public class ZipkinApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ZipkinApplication.class, args);
        }
    }
  • 在application.properties中做一些简单配置,比如设置服务端口号为9411
    server:
      port: 9411
    spring:
      application:
        name: zipkin-server
    eureka:
      client:
        service-url:
          #注册地址
          defaultZone: http://root:root@localhost:8082/eureka/

创建完上述工程之后,将其启动,并访问:http://127.0.0.1:9411/zipkin/

第二部:为应用引入和配置Zipkin服务
  • 在hello-service和ribbon-consumer的application.yml中增加ZipkinServer的配置,具体如下:

hello-service的application.yml

server:
  port: 8085
spring:
  application:
    name: hello-service
  zipkin:
    base-url: http://localhost:9411/
eureka:
  client:
    service-url:
      #注册地址
      defaultZone: http://root:root@localhost:8082/eureka/

ribbon-consumer 的application.yml

server:
  port: 9000
spring:
  application:
    name: ribbon-consumer
  zipkin:
    base-url: http://localhost:9411/
eureka:
  client:
    service-url:
      #注册地址
      defaultZone: http://root:root@localhost:8082/eureka/

测试与分析

依次eureka-server、hello-service、ribbon-consumer启动起来,然后做一些测试。

向hello-servie的接口发送几个请求(http://localhost:9000/hello)。日志跟踪信息最后一个值为true的时候,说明该跟踪信息会输出给Zipkin Server,在Zipkin Server的管理界面(http://127.0.0.1:9411/zipkin/)中选择合适的查询条件,单击Find Traces按钮,查询出日志中出现的跟踪信息,页面如下:
在这里插入图片描述

单击下方hello-service端点的跟踪信息,可以看到sleuth跟踪到的详细信息,包含请求事件消耗等。
在这里插入图片描述

sleuth跟踪到的详细信息:
在这里插入图片描述

至此,分布式服务跟踪搭建完毕。如有不足,还望大家多多指教。

源码参考
zipkin官方网站
参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值