赞
踩
Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来,由 Twitter 公司开发贡献。其主要功能是聚集来自各个异构系统的实时监控数据。Zipkin默认支持Http协议,除此之外,它还支持kafka,rabbitmq以及scribe协议:
docker pull openzipkin/zipkin
docker run --name zipkin -d -p 9411:9411 openzipkin/zipkin
浏览器打开 http://127.0.0.1:9411
实验目的:实现监控数据上报到zipkin,并分析每个方法的执行时间
- <?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>
- #zipkin
- spring.zipkin.base-url=http://127.0.0.1:9411/
- spring.zipkin.enabled=true
- spring.sleuth.sampler.probability=1
- ##
- server.port=8088
- 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 + "-------";
- }
-
-
-
-
- }
- 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;
- }
- }
以上只是一些关键代码,所有代码请参见下面代码仓库
https://github.com/Harries/springboot-demo
启动Spring Boot应用
浏览器输入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
可以看到刚刚访问trace id,以及调用的3个方法耗时,以及具体的代码位置信息
http://www.liuhaihua.cn/archives/710448.html
https://spring.academy/guides/spring-spring-zipkin
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。