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&#