Spring Boot集成zipkin快速入门Demo


1.什么zipkin

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

2.搭建zipkin环境

1.获取镜像

docker pull openzipkin/zipkin

2.启动

docker run --name zipkin -d -p 9411:9411 openzipkin/zipkin

3.web端查看

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

3.代码项目

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

pom.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

#zipkinspring.zipkin.base-url=http://127.0.0.1:9411/spring.zipkin.enabled=truespring.sleuth.sampler.probability=1##server.port=8088

controller

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;@RestControllerpublic 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

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 */@Configurationpublic 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; }}

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


代码仓库

  • https://github.com/Harries/springboot-demo

4.测试

启动Spring Boot应用

访问controller

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

2024-04-19 10:54:18.353 INFO [,37bf669cd6027ce8,86712cf932e61e5b,true] 31404 --- [nio-8088-exec-2] c.e.z.controller.HelloWorldController : 进入ping2024-04-19 10:54:18.370 INFO [,37bf669cd6027ce8,37bf669cd6027ce8,true] 31404 --- [nio-8088-exec-1] c.e.z.controller.HelloWorldController : study:pong study2024-04-19 10:54:18.373 INFO [,37bf669cd6027ce8,eb552e780a6de261,true] 31404 --- [nio-8088-exec-3] c.e.z.controller.HelloWorldController : this is info log2024-04-19 10:54:18.373 ERROR [,37bf669cd6027ce8,eb552e780a6de261,true] 31404 --- [nio-8088-exec-3] c.e.z.controller.HelloWorldController : this is error log2024-04-19 10:54:18.374 WARN [,37bf669cd6027ce8,eb552e780a6de261,true] 31404 --- [nio-8088-exec-3] c.e.z.controller.HelloWorldController : this is warn log2024-04-19 10:54:18.375 INFO [,37bf669cd6027ce8,37bf669cd6027ce8,true] 31404 --- [nio-8088-exec-1] c.e.z.controller.HelloWorldController : floor:1232024-04-19 10:54:18.377 INFO [,37bf669cd6027ce8,3b1df4558b01739f,true] 31404 --- [nio-8088-exec-4] c.e.z.controller.HelloWorldController : 进入ping2024-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.参考

  • http://www.liuhaihua.cn/archives/710448.html

  • https://spring.academy/guides/spring-spring-zipkin

相关推荐

  • Rust 修复了 std 库中 Windows 10/11 的关键命令注入缺陷
  • 架构师如何不错过大模型技术革命?一线实践都在这了|ArchSummit深圳 2024 议程上线
  • 李彦宏称开源落后于闭源,圈内大佬力挺;雷军否认“爽文人生” ;特斯拉裁员遣散费“过低”,马斯克:“得加钱”! | Q资讯
  • 预测前端市场未来五年趋势,做好准备~
  • 人工智能周刊#17:Llama3、微调 LLMs 的 PyTorch 原生库、李彦宏内部讲话、900 个最受欢迎的AI 工具
  • 小即是大?HuggingFace CEO预测小模型元年将至,将成为AI的下一个“大事件”
  • GPT 5发布前夜:美国大选成最大变数!
  • React 案例:sidebar(侧边栏)和 modal(弹出层)实例
  • 阿里P9的50个思考
  • Redis 如何做消息队列?
  • C++ 11 接受度最广!2024 年 C++ 开发者年度调查报告最新出炉
  • 2024全球机器学习技术大会-上海站日程发布,附参会指南!
  • 突破!颜宁,发国产神刊!
  • Llama 3突然来袭!开源社区再次沸腾:GPT-4级别模型可以自由访问的时代到来
  • [开源]MIT开源协议,一款免费的运行时界面创建API接口的项目
  • 最强开源大模型Llama 3来了!4000亿参数狙击GPT-4,训练数据达Llama 2七倍
  • 2024中国生成式AI企业TOP50揭晓!AI新质生产力创新先锋
  • 54位大咖演讲精华!中国生成式AI大会圆满收官,TOP50企业榜单揭晓
  • 这两个BI可视化软件如何选择?亲测推荐
  • React案例:一个常见的响应式网站导航