SpringBoot监控

JAVA 2023-07-05 17:29:38
48阅读

actuator主要是做监控

  一个英文实例教程

EndPoint管理方法

   如果你是用IDEA商业源码,你将能够见到SpringBoot显现出的EndPoint的Http详细地址。详尽不做详细介绍了,总之便是能够根据Http曝露监控信息。可是那样做的情景并不是很多。

Metrics

  Java常见的Metrics库 dropwizard/metrics ,Flink Samza等数据融合架构中所应用的Metrics库。 micrometer-metrics, SpringBoot2中所应用的Metrics库,仅有插口沒有完成,类例如Sl4j架构。

docker 自然环境

  1. docker安裝 influxDb

  docker run -d --name influx -p 8086:8086 influxdb

  1. docker安裝Grafana

  docker run -d --name=grafana -p 3000:3000 grafana/grafana

  1. docker 安裝Prometheus

  docker run --name prometheus -d -p 9090:9090 prom/prometheus

SpringBoot 拆箱既用的 InfluxDb(完全免费,特性高,沒有分布式,push方式)和Prometheus(作用多,可分布式,pull实体模型)。DataDog,New Relic是必须收费的。Graphite太丑了。WaveFront没找到材料

禁止使用SpringBoot Actuator中Http监控,JMX监控。

  management: #JMX endpoints: jmx: exposure: exclude: '*' #Http server: port: -1

InfluxDb Grafana Metrics 监控

  pom

  <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-influx</artifactId> <scope>runtime</scope> </dependency>

  yml

  management: metrics: export: influx: auto-create-db: true db: test uri: http://192.168.15.102:8086 connect-timeout: 1s read-timeout: 10s num-threads: 1 consistency: one compressed: true batch-size: enabled: true step: 1米 prometheus: enabled: false

  grafana 简易流程 搞个数据库 -> 搞个数据图表 -> 写好相匹配Sql —> 結果3D渲染

grafana 报警

  TODO

Prometheus Grafana Metrics监控SpringBoot

  pom

  <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <scope>runtime</scope> </dependency>

  yml

  server: port: 8085management: endpoints: web: exposure: include: '*' endpoint: health: show-details: always metrics: export: influx: enabled: false tags: application: ${spring.application.name}spring: application: name: springboot-promethues

  promethues.yml配备,提议应用安装文件安裝promethues

  scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'spring' metrics_path: '/actuator/prometheus' static_configs: - targets: [';]

  起动promethues, promethues.exe --config.file=promethues.yml, 默认设置端口号为9090,在grafana导进他人写好的模版, https://hboards/4701,留意在Grafana中导进时选择自己的promethues数据库。最终結果。

  

Timed注释

  TODO

自定Metrics,并在Grafana中监控

  第一种:完成 MeterBinder 插口,Spring可能自动组装

  /** * @author tongjian * @date 2/5/29 15:27 */@Componentpublic class RegistryBind implements MeterBinder { @Override public void bindTo(MeterRegistry meterRegistry) { // 根据 meterRigistry申请注册Metrics }}

  假如你不愿加上@Component的注释,请应用@Bean注释代管到IOC器皿。

  第二种:根据构造方法,注册中心关联。 应用@Component 或 @Service 等IOC器皿注释,SpringBoot可能自动组装,一样,假如你无需@Component注释,请使用@Bean注释。

  /** * @author maodun * @date 2/5/29 15:27 */@Componentpublic class RegistryBind { private final List<String> words = new CopyOnWriteArrayList<>(); public RegistryBind(MeterRegistry registry) { registry.gaugeCollectionSize("dictionary.size", Tags.empty(), this.words); }}

  第三种: 立即应用全局性注册中心

  @Componentpublic class MyCounter { static final Counter Counter1 = Metrics.counter("a.test.counter", "bind", "demo"); public void counterAdd() { Counter1.inc rement(10d); }}

单元测试

  首先我们看下SpringBoot已有的 Metrics Meter (JVM,Http,Tomcat), 代码在UDFMetrcis目录下。

  /** * @author tongjian * @date 2/5/29 15:49 */@SpringBootTest(classes = InfluxApp.class, args = "--spring.profiles.active=influx")@Slf4jpublic class BaseMetrics { private static final AtomicInteger registrySize = new AtomicInteger(0); private static final AtomicInteger meterSize = new AtomicInteger(0); @Test void allMetrics() { for (MeterRegistry registry : Metrics.globalRegistry.getRegistries()) { registrySize.incrementAndGet(); log.info("Registry Name is: {}", registry.config().toString()); for (Meter meter : registry.getMeters()) { meterSize.incrementAndGet(); log.info("Meter名字为: {} ", meter.getId().getName()); } } log.info("注册中心数量为: {},Metrics 仪表数为: {}", registrySize.get(), meterSize.get()); }}

  

  
自定义的Metrics都注册入了注册中心,并且输出到了InfluxDb。 下面我来学习如果使用一个自定义的Metrics Counter。

  @Componentpublic class MyCounterV2 { public static final AtomicInteger atomicInteger = new AtomicInteger(0); public static final FunctionCounter testunit = FunctionCounter.builder("a.test.counter2", atomicInteger, AtomicInteger::get) .baseUnit("testunit") .register(Metrics.globalRegistry); public void add() { atomicInteger.incrementAndGet(); } public double get() { return testunit.count(); } public double get2() { return testunit.measure().iterator().next().getValue(); } public double get3() { return atomicInteger.get(); }}// 在Main App中调用 while (true) { myCounterV2.add(); Thread.sleep(500); System.out.println(myCounterV2.get()); }

  最后写入Influx的数据, 我发现,只会写入你步长范围内的增量,也就是一分钟内,计数器的增量。而且你的非函数Counter,写入Influxdb后,计数会归于零。

参考

  SpringBoot监控对比
SpringBoot整合InfluxDb
Spring Actuator
SpringBoot中的Metrics框架
Http接口的暴露
SpringBoot Prometheus
JMX 的介绍
Grafana DashBords仓库
超大规模Metrcis监控
自定义Redis Cache命中率到 InfluxDb
自定义Metrics 自定义Metrics

HA

  Prometheus HA 长期存储
M3 存储

the end
免责声明:本文不代表本站的观点和立场,如有侵权请联系本站删除!本站仅提供信息存储空间服务。