SpringBoot快速接入Prometheus监控方法

步骤一:引入Spring Boot Actuator依赖,进行初始配置

1
2
3
4
5
6
7
8
9
10
<!-- spring-boot-actuator依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- prometheus依赖 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

步骤二:在bootstrap-环境标示.yml中添加相关配置暴露监测数据端口。例如,端口为19098。

提示:监控端口根据自身服务设置,例如:微服务端口是:9098,监控端口设置为:19098,方便识别管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#prometheus监控平台配置
management:
server:
port: 19098 # 直接访问这个端口即可显示相关监控接口
endpoints:
jmx:
exposure:
include: "*"
web:
exposure:
include: "*"
exclude: configprops
endpoint:
health:
show-details: ALWAYS
metrics:
tags:
application: ${spring.application.name}

步骤三:启动服务,访问 http://localhost:19098/metrics

接口返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# HELP hikaricp_connections_timeout_total Connection timeout total count
# TYPE hikaricp_connections_timeout_total counter
hikaricp_connections_timeout_total{application="picgen",pool="UserHikariCP",} 0.0
# HELP system_load_average_1m The sum of the number of runnable entities queued to available processors and the number of runnable entities running on the available processors averaged over a period of time
# TYPE system_load_average_1m gauge
system_load_average_1m{application="picgen",} 10.0771484375
# HELP executor_pool_core_threads The core number of threads for the pool
# TYPE executor_pool_core_threads gauge
executor_pool_core_threads{application="picgen",name="applicationTaskExecutor",} 8.0
# HELP application_started_time_seconds Time taken (ms) to start the application
# TYPE application_started_time_seconds gauge
application_started_time_seconds{application="picgen",main_application_class="io.soundsright.picgen.SoundsrightPicgenApplication",} 11.188
# HELP tomcat_sessions_alive_max_seconds
# TYPE tomcat_sessions_alive_max_seconds gauge
tomcat_sessions_alive_max_seconds{application="picgen",} 0.0
# HELP hikaricp_connections Total connections
# TYPE hikaricp_connections gauge
hikaricp_connections{application="picgen",pool="UserHikariCP",} 5.0
# HELP app_requests_method_count_total
# TYPE app_requests_method_count_total counter
app_requests_method_count_total{application="picgen",method="PrometheusController.core",} 0.0
app_requests_method_count_total{application="picgen",method="PrometheusController.index",} 0.0
# HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded_classes gauge
jvm_classes_loaded_classes{application="picgen",} 16322.0
# HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool
# TYPE jvm_buffer_total_capacity_bytes gauge
jvm_buffer_total_capacity_bytes{application="picgen",id="mapped",} 0.0
jvm_buffer_total_capacity_bytes{application="picgen",id="direct",} 2263623.0
# HELP hikaricp_connections_creation_seconds_max Connection creation time
# TYPE hikaricp_connections_creation_seconds_max gauge
hikaricp_connections_creation_seconds_max{application="picgen",pool="UserHikariCP",} 0.0
# HELP hikaricp_connections_creation_seconds Connection creation time
# TYPE hikaricp_connections_creation_seconds summary
hikaricp_connections_creation_seconds_count{application="picgen",pool="UserHikariCP",} 13.0
hikaricp_connections_creation_seconds_sum{application="picgen",pool="UserHikariCP",} 10.01
# HELP hikaricp_connections_usage_seconds Connection usage time
# TYPE hikaricp_connections_usage_seconds summary
hikaricp_connections_usage_seconds_count{application="picgen",pool="UserHikariCP",} 298.0
hikaricp_connections_usage_seconds_sum{application="picgen",pool="UserHikariCP",} 45.876
# HELP hikaricp_connections_usage_seconds_max Connection usage time
# TYPE hikaricp_connections_usage_seconds_max gauge


下面省略一千多行……

参考: