sleuth结合zipkin实现链路追踪(一) 基于Http

本文详细介绍如何在基于Spring Cloud的微服务架构中,利用Zipkin和Sleuth实现服务间的链路追踪,包括配置Eureka注册中心、Zipkin-Server及服务集成Sleuth的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参照

sleuth-git

sleuth-guide

环境

Java 8 / Idea / Gradle4.10.2

Spring-boot (2.1.3.RELEASE) / SpringCloud(Greenwich.RELEASE) / Zipkin-Server (2.12.9) / Sleuth (2.1.0.RELEASE)

本文基于Eureka注册中心实现,spring-cloud-dependencies管理Spring-Cloud依赖版本

Eureka注册中心

gradle核心依赖:

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'

Application入口类

/**
 */
@EnableEurekaServer
@SpringBootApplication
public class Registry01Application {
    public static void main(String[] args) {
        SpringApplication.run(Registry01Application.class, args);
    }
}

yml配置文件:

server:
  port: 8010
spring:
  application:
    name: registry01

#注册中心配置,不注册到注册中心
eureka:
  server:
    #关闭自我保护
    enableSelfPreservation: false
  client:
    registerWithEureka: false
    fetchRegistry: false

运行main方法正常启动即可。

Zipkin-Server分布式追踪

gradle依赖:

    //作为eureka client依赖
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

    //zipkin链路信息请求接收服务器,2.12.5开始使用armeria server
    implementation ("io.zipkin.java:zipkin-server:${zipkinVersion}") {
        //排除自带log与spring boot冲突造成stackOverflowError
        exclude group: "org.springframework.boot", module: "spring-boot-starter-log4j2"
    }
    implementation "io.zipkin.java:zipkin-autoconfigure-ui:${zipkinVersion}"

Application:


/**
 * zipkin server 链路追踪分析服务,twitter推出。
 * 访问:http://localhost:port/zipkin/
 */
@EnableZipkinServer
@EnableDiscoveryClient
@SpringBootApplication
public class ZipkinServer01Application {

    /**
     * 该方式启动ZipkinServer,使用zipkin-server-shared.yml配置文件
     * 位于zipkin-server.jar内
     * {@link ZipkinServer}
     * @param args
     */
    public static void main(String[] args) {
        //zipkin-server-shared.yml
        new SpringApplicationBuilder(ZipkinServer.class)
                .listeners(new RegisterZipkinHealthIndicators())
                //指定配置文件名称为zipkin-server,默认是application
                //zipkin-server.yml中指定profile=shared
                .properties("spring.config.name=zipkin-server").run(args);
    }

yml配置文件

#eureka-client注册中心配置,注册到注册中心
eureka:
  client:
    #默认true
#    enabled: false
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8010/eureka/

spring:
  application:
    name: zipkin-server01

服务集成Sleuth

gradle依赖:

  //作为eureka client依赖
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    //web模块
    implementation 'org.springframework.boot:spring-boot-starter-web'
    //feign是对hystrix+ribbon的一层封装
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    //sleuth 链路
    //implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'

Application:

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Consumer02SleuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(Consumer02SleuthApplication.class, args);
    }
}

Controller:

@RestController
@RequestMapping("hello")
public class HelloController {

    @GetMapping
    public String local() {
        return "hello";
    }
}

yml配置文件:

#eureka-client注册中心配置,注册到注册中心
eureka:
  client:
    #默认true
#    enabled: false
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8010/eureka/

server:
  port: 8032

spring:
  application:
    name: consumer02-sleuth
  #ZipkinProperties
  zipkin:
    #ZipkinSenderProperties
    sender:
      type: web
    # zipkin-server01是zipkin-server注册到注册中心的名称
    baseUrl: http://zipkin-server01/
    #使用服务发现
    #discoveryClientEnabled: true
    #当前服务名称,不写默认取spring.application.name
    #service:
      #name: consumer02-sleuth
  #SamplerProperties 采样配置, 1=100%的请求进行采样
  sleuth:
    sampler:
      probability: 1

logging:
  level:
    root: info
    web: debug

访问接口控制台日志模样大概如此(开启web日志):

2019-05-09 19:46:32.311 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] o.s.web.servlet.DispatcherServlet        :GET "/hello", parameters={}
2019-05-09 19:46:32.323 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping :Mapped to public java.lang.String tom.spring.cloud.consumer.controller.HelloController.local()
2019-05-09 19:46:32.351 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] m.m.a.RequestResponseBodyMethodProcessor :Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-05-09 19:46:32.352 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] m.m.a.RequestResponseBodyMethodProcessor :Writing ["hello"]
2019-05-09 19:46:32.364 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] o.s.web.servlet.DispatcherServlet        :Completed 200 OK

解释一下DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true]代表的东西,第一个是代表服务名称就是注册到注册中心的名字配置spring.application.name;第二个是TraceId当次记录的标识,多服务间调用时该值是不变的;第三个是SpanId当前业务请求处理快的标识;第四个字段是代表是否收集,因为probability=1所以一直是true。

 

 

 

最后,启动注册中心,zipkin-server和服务程序,访问服务程序hello接口,观察控制台日志。访问zipkin-server主页可以看到访问链路信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值