Spring Boot集成zipkin快速入门Demo

1.什么zipkin

Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来,由 Twitter 公司开发贡献。其主要功能是聚集来自各个异构系统的实时监控数据。Zipkin默认支持Http协议,除此之外,它还支持kafka,rabbitmq以及scribe协议:

2.搭建zipkin环境

1.获取镜像

bash 复制代码
docker pull openzipkin/zipkin

2.启动

css 复制代码
docker run --name zipkin -d -p 9411:9411 openzipkin/zipkin

3.web端查看

浏览器打开 http://127.0.0.1:9411

3.代码项目

**

实验目的:实现监控数据上报到zipkin,并分析每个方法的执行时间

**

pom.xml

xml 复制代码
<?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">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>zipkin</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
    </dependencies>
</project>

application.properties

ini 复制代码
#zipkin
spring.zipkin.base-url=http://127.0.0.1:9411/
spring.zipkin.enabled=true
spring.sleuth.sampler.probability=1
##
server.port=8088

controller

java 复制代码
package com.et.zipkin.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorldController {
    private static Logger log = LoggerFactory.getLogger(HelloWorldController.class);


    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("ping")
    public Object ping() {
        log.info("进入ping");
        return "pong study";
    }

    @RequestMapping("log")
    public Object log() {
        log.info("this is info log");
        log.error("this is error log");
        log.debug("this is debug log");
        log.warn("this is warn log");
        log.trace("this is trace log");
        return "123";
    }

    @RequestMapping("http")
    public Object httpQuery() {

        String studyUrl = "http://localhost:8088/ping";
        URI studyUri = URI.create(studyUrl);
        String study = restTemplate.getForObject(studyUri, String.class);
        log.info("study:{}", study);

        String floorUrl = "http://localhost:8088/log";
        URI floorUri = URI.create(floorUrl);
        String floor = restTemplate.getForObject(floorUri, String.class);
        log.info("floor:{}", floor);

        String roomUrl = "http://localhost:8088/ping";
        URI roomUri = URI.create(roomUrl);
        String room = restTemplate.getForObject(roomUri, String.class);
        log.info("room:{}", room);
        return study + "-------" + floor + "-------" + room + "-------";
    }


}

config

java 复制代码
package com.et.zipkin.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * RestTemplate  config
 */
@Configuration
public class RestTemplateConfig {
 
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }
 
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//unit is ms
        factory.setConnectTimeout(5000);//unit is ms
        return factory;
    }
}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

4.测试

启动Spring Boot应用

访问controller

浏览器输入http://127.0.0.1:8088/http,可以看到控制台生成traceID 和spanid

yaml 复制代码
2024-04-19 10:54:18.353 INFO [,37bf669cd6027ce8,86712cf932e61e5b,true] 31404 --- [nio-8088-exec-2] c.e.z.controller.HelloWorldController : 进入ping
2024-04-19 10:54:18.370 INFO [,37bf669cd6027ce8,37bf669cd6027ce8,true] 31404 --- [nio-8088-exec-1] c.e.z.controller.HelloWorldController : study:pong study
2024-04-19 10:54:18.373 INFO [,37bf669cd6027ce8,eb552e780a6de261,true] 31404 --- [nio-8088-exec-3] c.e.z.controller.HelloWorldController : this is info log
2024-04-19 10:54:18.373 ERROR [,37bf669cd6027ce8,eb552e780a6de261,true] 31404 --- [nio-8088-exec-3] c.e.z.controller.HelloWorldController : this is error log
2024-04-19 10:54:18.374 WARN [,37bf669cd6027ce8,eb552e780a6de261,true] 31404 --- [nio-8088-exec-3] c.e.z.controller.HelloWorldController : this is warn log
2024-04-19 10:54:18.375 INFO [,37bf669cd6027ce8,37bf669cd6027ce8,true] 31404 --- [nio-8088-exec-1] c.e.z.controller.HelloWorldController : floor:123
2024-04-19 10:54:18.377 INFO [,37bf669cd6027ce8,3b1df4558b01739f,true] 31404 --- [nio-8088-exec-4] c.e.z.controller.HelloWorldController : 进入ping
2024-04-19 10:54:18.379 INFO [,37bf669cd6027ce8,37bf669cd6027ce8,true] 31404 --- [nio-8088-exec-1] c.e.z.controller.HelloWorldController : room:pong study

zipkin查看调用链

可以看到刚刚访问trace id,以及调用的3个方法耗时,以及具体的代码位置信息

5.参考

相关推荐
xiaolyuh1235 分钟前
Arthas修改类(如加日志)的实现原理
java
栗子叶9 分钟前
Java对象创建的过程
java·开发语言·jvm
勇哥java实战分享14 分钟前
短信平台 Pro 版本 ,比开源版本更强大
后端
学历真的很重要18 分钟前
LangChain V1.0 Context Engineering(上下文工程)详细指南
人工智能·后端·学习·语言模型·面试·职场和发展·langchain
有一个好名字19 分钟前
力扣-从字符串中移除星号
java·算法·leetcode
计算机毕设VX:Fegn089521 分钟前
计算机毕业设计|基于springboot + vue二手家电管理系统(源码+数据库+文档)
vue.js·spring boot·后端·课程设计
zfj32128 分钟前
CyclicBarrier、CountDownLatch、Semaphore 各自的作用和用法区别
java·开发语言·countdownlatch·semaphore·cyclicbarrier
2501_9167665434 分钟前
【JVM】类的加载机制
java·jvm
Sag_ever35 分钟前
Java数组详解
java
张np35 分钟前
java基础-ConcurrentHashMap
java·开发语言