Spring Cloud Zipkin
Zipkim是一个可以采集并且跟踪分布式系统中请求数据的组件,让开发者可以更加
直观的监控到请求在各个微服务所耗费的时间等,Zipkin:Zipkin Server、Zipkin Client,
创建Zipkin server
创建一个maven工程,pom.xml

<dependencies>
    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-server</artifactId>
        <version>2.9.4</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-autoconfigure-ui</artifactId>
        <version>2.9.4</version>
    </dependency>
</dependencies>

创建配置文件 application.yml

server:
  port: 9090

创建启动类

package com.shuang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.internal.EnableZipkinServer;

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

注解说明:
@EnableZipkinServer:声明启动Zipkin Server
创建 Zipkin Client
创建maven工程,pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zipkin</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

创建配置文件,application.yml

server:
  port: 8090
spring:
  application:
    name: zipkinclient
  sleuth:
    web:
      client:
        enabled: true
    sampler:
      probability: 1.0
  zipkin:
    base-url: http://localhost:9090/
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

属性说明:
spring.sleuth.web.client.enabled:设置开启请求跟踪
spring.sleuth.sampler.probability:设置采样比例,默认值是1.0。
spring.zipkin.base-url:Zipkin Server地址。
创建启动类

package com.shuang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Handler

package com.shuang.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/zipkin")
public class ZipkinHandler {
    
    @Value("${server.port}")
    public String port;
    
    @GetMapping("/index")
    public String index(){
        return this.port;
    }
}

依次启动:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐