• 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

    f85734bff89436b8b524c8787dfe0921.png

    3.代码项目

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

    pom.xml

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>springboot-demoartifactId>
    7. <groupId>com.etgroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>zipkinartifactId>
    12. <properties>
    13. <maven.compiler.source>8maven.compiler.source>
    14. <maven.compiler.target>8maven.compiler.target>
    15. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.bootgroupId>
    19. <artifactId>spring-boot-starter-webartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframework.bootgroupId>
    23. <artifactId>spring-boot-autoconfigureartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.bootgroupId>
    27. <artifactId>spring-boot-starter-testartifactId>
    28. <scope>testscope>
    29. dependency>
    30. <dependency>
    31. <groupId>org.springframework.cloudgroupId>
    32. <artifactId>spring-cloud-starter-zipkinartifactId>
    33. <version>2.2.5.RELEASEversion>
    34. dependency>
    35. <dependency>
    36. <groupId>org.springframework.cloudgroupId>
    37. <artifactId>spring-cloud-starter-sleuthartifactId>
    38. <version>2.2.5.RELEASEversion>
    39. dependency>
    40. dependencies>
    41. project>

    application.properties

    1. #zipkin
    2. spring.zipkin.base-url=http://127.0.0.1:9411/
    3. spring.zipkin.enabled=true
    4. spring.sleuth.sampler.probability=1
    5. ##
    6. server.port=8088

    controller

    1. package com.et.zipkin.controller;
    2. import org.slf4j.Logger;
    3. import org.slf4j.LoggerFactory;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.ResponseBody;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import org.springframework.web.client.RestTemplate;
    10. import java.net.URI;
    11. import java.util.HashMap;
    12. import java.util.Map;
    13. @RestController
    14. public class HelloWorldController {
    15. private static Logger log = LoggerFactory.getLogger(HelloWorldController.class);
    16. @Autowired
    17. private RestTemplate restTemplate;
    18. @RequestMapping("ping")
    19. public Object ping() {
    20. log.info("进入ping");
    21. return "pong study";
    22. }
    23. @RequestMapping("log")
    24. public Object log() {
    25. log.info("this is info log");
    26. log.error("this is error log");
    27. log.debug("this is debug log");
    28. log.warn("this is warn log");
    29. log.trace("this is trace log");
    30. return "123";
    31. }
    32. @RequestMapping("http")
    33. public Object httpQuery() {
    34. String studyUrl = "http://localhost:8088/ping";
    35. URI studyUri = URI.create(studyUrl);
    36. String study = restTemplate.getForObject(studyUri, String.class);
    37. log.info("study:{}", study);
    38. String floorUrl = "http://localhost:8088/log";
    39. URI floorUri = URI.create(floorUrl);
    40. String floor = restTemplate.getForObject(floorUri, String.class);
    41. log.info("floor:{}", floor);
    42. String roomUrl = "http://localhost:8088/ping";
    43. URI roomUri = URI.create(roomUrl);
    44. String room = restTemplate.getForObject(roomUri, String.class);
    45. log.info("room:{}", room);
    46. return study + "-------" + floor + "-------" + room + "-------";
    47. }
    48. }

    config

    1. package com.et.zipkin.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.http.client.ClientHttpRequestFactory;
    5. import org.springframework.http.client.SimpleClientHttpRequestFactory;
    6. import org.springframework.web.client.RestTemplate;
    7. /**
    8. * RestTemplate config
    9. */
    10. @Configuration
    11. public class RestTemplateConfig {
    12. @Bean
    13. public RestTemplate restTemplate(ClientHttpRequestFactory factory){
    14. return new RestTemplate(factory);
    15. }
    16. @Bean
    17. public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
    18. SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    19. factory.setReadTimeout(5000);//unit is ms
    20. factory.setConnectTimeout(5000);//unit is ms
    21. return factory;
    22. }
    23. }

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

    代码仓库

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

    4.测试

    启动Spring Boot应用

    访问controller

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

    1. 2024-04-19 10:54:18.353 INFO [,37bf669cd6027ce8,86712cf932e61e5b,true] 31404 --- [nio-8088-exec-2] c.e.z.controller.HelloWorldController : 进入ping
    2. 2024-04-19 10:54:18.370 INFO [,37bf669cd6027ce8,37bf669cd6027ce8,true] 31404 --- [nio-8088-exec-1] c.e.z.controller.HelloWorldController : study:pong study
    3. 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
    4. 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
    5. 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
    6. 2024-04-19 10:54:18.375 INFO [,37bf669cd6027ce8,37bf669cd6027ce8,true] 31404 --- [nio-8088-exec-1] c.e.z.controller.HelloWorldController : floor:123
    7. 2024-04-19 10:54:18.377 INFO [,37bf669cd6027ce8,3b1df4558b01739f,true] 31404 --- [nio-8088-exec-4] c.e.z.controller.HelloWorldController : 进入ping
    8. 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

  • 相关阅读:
    网络安全:使用各类编码混淆攻击
    瑞芯微 RK1126 平台编译zlib gpac 使用GPAC将H264 H265保存为MP4文件 录像
    基于WEB的网上购物系统的设计与实现
    【C++学习笔记】1.2 C++输入与输出
    【Eureka】【源码+图解】【八】Eureka客户端的服务获取
    1.Linux的目录结构
    E4991B 阻抗分析仪,1 MHz 至 500 MHz/1 GHz/3 GHz
    【每日OJ —— 225.用队列实现栈(队列)】
    超级计算/先进计算的十大用途
    电路知识的回顾
  • 原文地址:https://blog.csdn.net/dot_life/article/details/138021348