Spring Boot 监控数据可视化图文教程

本文详述如何利用Spring Boot Actuator的Micrometer对接Prometheus进行监控,然后通过Grafana实现数据可视化。内容涵盖依赖配置、Prometheus安装与配置、Grafana安装及创建监控Dashboard,最后分享了几款实用的预设Dashboard。
摘要由CSDN通过智能技术生成

TIPS

本文基于Spring Boot 2.1.4,理论支持Spring Boot 2.x所有版本

众所周知,Spring Boot有个子项目Spring Boot Actuator,它为应用提供了强大的监控能力。从Spring Boot 2.0开始,Actuator将底层改为Micrometer,提供了更强、更灵活的监控能力。Micrometer是一个监控门面,可以类比成监控界的 Slf4j 。

借助Micrometer,应用能够对接各种监控系统,例如:

•AppOptics[1]•Atlas[2]•Datadog[3]•Dynatrace[4]•Elastic[5]•Ganglia[6]•Graphite[7]•Humio[8]•Influx[9]•JMX[10]•KairosDB[11]•New Relic[12]•Prometheus[13]•SignalFx[14]•Simple (in-memory)[15]•StatsD[16]•Wavefront[17]

下面演示如何对接 Prometheus ,并使用 Grafana 实现数据的可视化。

TIPS

童鞋们对Prometheus或Grafana不熟悉也没关系,本文是手把手文章,按步骤操作即可。

编码

编写代码

1 加依赖


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

这里,我们为应用引入了 micrometer-registry-prometheus ,事实上,你想对接上文列表中的哪款监控系统,就写啥。例如想对接 Influx ,则需添加依赖 micrometer-registry-influx 。

2 写配置


server:
  port: 8080
spring:
  application:
    name: prometheus-test
management:
  endpoints:
    web:
      exposure:
        include: 'prometheus'
  metrics:
    tags:
      application: ${spring.application.name}

如配置所示,指定应用名为 prometheus-test ,并将 Actuator 的 /actuator/prometheus 端点暴露出来;management.metrics.tags.application=prometheus-test 作用是为指标设置一个名为application="prometheus-test" 的Tag,Tag是Prometheus提供的一种能力,从而实现更加灵活的筛选。

测试

1 启动应用

2 访问 http://localhost:8080/actuator/prometheus 可获得类似如下的结果:


# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{application="prometheus-test",area="heap",id="PS Old Gen",} 2.1193976E7
jvm_memory_used_bytes{application="prometheus-test",area="nonheap",id="Metaspace",} 3.8791688E7
jvm_memory_used_bytes{application="prometheus-test",area="heap",id="PS Survivor Space",} 0.0
jvm_memory_used_bytes{application="prometheus-test",area="nonheap",id="Compressed Class Space",} 5303976.0
jvm_memory_used_bytes{application="prometheus-test",area="heap",id="PS Eden Space",} 8.2574816E7
jvm_memory_used_bytes{application="prometheus-test",area="nonheap",id="Code Cache",} 8693824.0
# HELP tomcat_global_received_bytes_total  
# TYPE tomcat_global_received_bytes_total counter
tomcat_global_received_bytes_total{application="prometheus-test",name="http-nio-8080",} 0.0
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="prometheus-test",} 20.0
# HELP tomcat_sessions_alive_max_seconds  
# TYPE tomcat_sessions_alive_max_seconds gauge
tomcat_sessions_alive_max_seconds{application="prometheus-test",} 0.0
# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
# TYPE jvm_buffer_memory_used_bytes gauge
jvm_buffer_memory_used_bytes{application="prometheus-test",id="mapped",} 0.0
jvm_buffer_memory_used_bytes{application="prometheus-test",id="direct",} 90112.0
# HELP jvm_threads_states_threads The current number of threads having NEW state
# TYPE jvm_threads_states_threads gauge
jvm_threads_states_threads{application="prometheus-test",state="runnable",} 9.0
jvm_threads_states_threads{application="prometheus-test",state="new",} 0.0
jvm_threads_states_threads{application="prometheus-test",state="terminated",} 0.0
jvm_threads_states_threads{application="prometheus-test",state="blocked",} 0.0
jvm_threads_states_threads{application="prometheus-test",state="waiting",} 12.0
jvm_threads_states_threads{application="prometheus-test",state="timed-waiting",} 3.0
# HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
# TYPE process_cpu_usage gauge
process_cpu_usage{application="prometheus-test",} 0.0030590633504868434
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{application="prometheus-test",level="info",} 7.0
logback_events_total{application="prometheus-test",level="warn",} 0.0
logback_events_total{application="prometheus-test",level="trace",} 0.0
logback_events_total{application="prometheus-test",level="debug",} 0.0
logback_events_total{application="prometheus-test&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值