Spring Boot集成zipkin快速入门Demo

ops/2024/9/23 8:13:48/

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

f85734bff89436b8b524c8787dfe0921.png

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

#zipkin
spring.zipkin.base-url=http://127.0.0.1:9411/
spring.zipkin.enabled=true
spring.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;
@RestController
public class HelloWorldController {private static Logger log = LoggerFactory.getLogger(HelloWorldController.class);@Autowiredprivate 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 {@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory){return new RestTemplate(factory);}@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory(){SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setReadTimeout(5000);//unit is msfactory.setConnectTimeout(5000);//unit is msreturn 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 : 进入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个方法耗时,以及具体的代码位置信息5cd72e71a183ee90a9456dcfefc1bdd1.png

5.参考

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

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


http://www.ppmy.cn/ops/14505.html

相关文章

全国各省市建设工程类专业职称评审要求总结(欢迎补充完善、沟通交流)

全国各省市建设工程类专业职称评审要求汇总统计如下&#xff0c;总体来说北京最难&#xff0c;经济欠发达、偏远地区评审要求相对简单&#xff0c;每个地方的要求存在一定的相似性&#xff0c;但又都各具特色&#xff0c;基本上来说论文是评审的必备条件&#xff0c;但是各个地…

网络初识

网络 局域网 一个区域的网 广域网 相对概念&#xff0c;没有绝对的界限&#xff0c;全世界现在最大的广域网&#xff0c;就教做TheInternet&#xff0c;万维网 路由器 交换机和路由器&#xff0c;都是用来组建网络的重要设备 交换机 上网的设备&#xff08;电脑/手…

Okapi Framework

文章目录 关于 OkapiRainbowCheckMateRatelTikalFilters Plugin for OmegaTLonghorn 关于 Okapi 官网&#xff1a;http://okapiframework.org源码&#xff1a;https://bitbucket.org/okapiframework/okapi/src文档&#xff1a;http://okapiframework.org/wiki/index.php?titl…

Python重点数据结构基本用法

Python重点数据结构用法 运算符描述[] [:]下标&#xff0c;切片**指数~ -按位取反, 正负号* / % //乘&#xff0c;除&#xff0c;模&#xff0c;整除 -加&#xff0c;减>> <<右移&#xff0c;左移&按位与^ < < > >小于等于&#xff0c;小于&#…

人工智能数据防泄露解决方案

需求背景 人工智能三大核心要素&#xff1a;算法、算力、数据。除了算法、算力外&#xff0c;最重要核心因素是数据。实现人工智能有两个阶段&#xff0c;即准备数据与训练模型。数据准备工作量占比达 70% 以上&#xff0c;但更重要的数据背后的人工&#xff0c;即数据预处理、…

倍思、南卡、漫步者开放式耳机好不好用? 硬核测评年度最强产品

​在开放式耳机市场的竞争日益加剧的今天&#xff0c;各品牌在硬件配置、功能及性价比上都在进行着激烈的角逐。不论是业界的领军品牌还是新兴的挑战者&#xff0c;他们无一例外地致力于推出更高水准的耳机&#xff0c;以迎合消费者的需求。作为一位经验丰富的耳机评测师&#…

MySql 查询优化

MySQL查询优化涉及多个方面&#xff0c;包括索引优化、查询优化、服务器配置优化等。以下是一些基本的查询优化技巧&#xff1a; 1.使用索引 确保你的查询利用了适当的索引。 SELECT * FROM table_name WHERE column_name value; 2.避免SELECT * 只选择需要的列&#xff…

Day1--什么是网络安全?网络安全常用术语

目录 1. 什么是网络安全&#xff1f; 信息系统&#xff08;Information System&#xff09; 信息系统安全三要素&#xff08;CIA&#xff09; 网络空间安全管理流程 网络安全管理 2. 网络安全的常用术语 3. 网络安全形势 4. 中国网络安全产业现状 1. 什么是网络安全&am…