3. Spring Boot 提供 Prometheus 数据
构建项目
创建一个 spring boot 的 maven 项目,名为 prometheus-demo
修改 pom.xml
在 pom.xml 添加 web、actuator、prometheus 依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置 application.properties 文件
在 application.properties 文件中添加暴露 actuator 的端点
spring.application.name=prometheus-demo
server.port=8081
management.endpoints.web.exposure.include=*
*表示暴露所有 actuator 端点
查看 /actuator
在浏览器输入 http://localhost:8081/actuator
得到如下信息:
{
"_links": {
"self": {
"href": "[http://localhost:8081/actuator](http://localhost:8081/actuator)",
"templated": false
},
"beans": {
"href": "[http://localhost:8081/actuator/beans](http://localhost:8081/actuator/beans)",
"templated": false
},
"caches-cache": {
"href": "[http://localhost:8081/actuator/caches/{cache}](http://localhost:8081/actuator/caches/%7Bcache%7D)",
"templated": true
},
"caches": {
"href": "[http://localhost:8081/actuator/caches](http://localhost:8081/actuator/caches)",
"templated": false
},
"health-path": {
"href": "[http://localhost:8081/actuator/health/{*path}](http://localhost:8081/actuator/health/%7B*path%7D)",
"templated": true
},
"health": {
"href": "[http://localhost:8081/actuator/health](http://localhost:8081/actuator/health)",
"templated": false
},
"info": {
"href": "[http://localhost:8081/actuator/info](http://localhost:8081/actuator/info)",
"templated": false
},
"conditions": {
"href": "[http://localhost:8081/actuator/conditions](http://localhost:8081/actuator/conditions)",
"templated": false
},
"configprops": {
"href": "[http://localhost:8081/actuator/configprops](http://localhost:8081/actuator/configprops)",
"templated": false
},
"configprops-prefix": {
"href": "[http://localhost:8081/actuator/configprops/{prefix}](http://localhost:8081/actuator/configprops/%7Bprefix%7D)",
"templated": true
},
"env": {
"href": "[http://localhost:8081/actuator/env](http://localhost:8081/actuator/env)",
"templated": false
},
"env-toMatch": {
"href": "[http://localhost:8081/actuator/env/{toMatch}](http://localhost:8081/actuator/env/%7BtoMatch%7D)",
"templated": true
},
"loggers": {
"href": "[http://localhost:8081/actuator/loggers](http://localhost:8081/actuator/loggers)",
"templated": false
},
"loggers-name": {
"href": "[http://localhost:8081/actuator/loggers/{name}](http://localhost:8081/actuator/loggers/%7Bname%7D)",
"templated": true
},
"heapdump": {
"href": "[http://localhost:8081/actuator/heapdump](http://localhost:8081/actuator/heapdump)",
"templated": false
},
"threaddump": {
"href": "[http://localhost:8081/actuator/threaddump](http://localhost:8081/actuator/threaddump)",
"templated": false
},
"prometheus": {
"href": "[http://localhost:8081/actuator/prometheus](http://localhost:8081/actuator/prometheus)",
"templated": false
},
"metrics-requiredMetricName": {
"href": "[http://localhost:8081/actuator/metrics/{requiredMetricName}](http://localhost:8081/actuator/metrics/%7BrequiredMetricName%7D)",
"templated": true
},
"metrics": {
"href": "[http://localhost:8081/actuator/metrics](http://localhost:8081/actuator/metrics)",
"templated": false
},
"sbom": {
"href": "[http://localhost:8081/actuator/sbom](http://localhost:8081/actuator/sbom)",
"templated": false
},
"sbom-id": {
"href": "[http://localhost:8081/actuator/sbom/{id}](http://localhost:8081/actuator/sbom/%7Bid%7D)",
"templated": true
},
"scheduledtasks": {
"href": "[http://localhost:8081/actuator/scheduledtasks](http://localhost:8081/actuator/scheduledtasks)",
"templated": false
},
"mappings": {
"href": "[http://localhost:8081/actuator/mappings](http://localhost:8081/actuator/mappings)",
"templated": false
}
}
}
- 可以看到 actuator 提供了哪些可访问的端点,其中包括:
- self: 提供对 actuator 根路径的访问,可以查看所有可用端点的列表。
- beans: 显示应用程序中所有 Spring beans 的完整列表,以及它们如何相互关联。
- caches: 提供所有缓存的概览信息。
- caches-cache: 用于查看或管理特定缓存的详细信息。
- health: 用于检查应用程序的健康状态,返回应用的实时健康状况。
- health-path: 提供对特定健康检查路径的访问,可以查看子组件的健康状况。
- info: 显示应用程序的一些自定义信息,如版本号、描述等。通常由应用程序自行配置。
- conditions: 显示自动配置的条件信息,帮助了解哪些配置条件通过了或未通过。
- configprops: 显示所有配置属性及其值,方便进行配置审查。
- configprops-prefix: 提供特定前缀的配置属性信息。
- env: 显示当前环境变量和配置属性的详细信息。
- env-toMatch: 用于查找特定的环境属性或配置值。
- loggers: 提供应用中 logger 的配置信息,允许动态调整 logger 的级别。
- loggers-name: 用于查看和修改特定 logger 的日志级别。
- heapdump: 生成当前 JVM 的 heap dump 文件,供分析使用。
- threaddump: 显示当前 JVM 的线程活动信息(线程转储),有助于诊断线程相关的问题。
- prometheus: 提供格式化的指标数据,供 Prometheus 监控系统使用。
- metrics: 显示应用程序中的各种指标信息,帮助监控应用的性能。
- metrics-requiredMetricName: 提供特定指标的详细信息。
- sbom: 显示 Software Bill of Materials(SBOM),即应用程序中所有组件的清单。
- sbom-id: 用于查看特定组件的 SBOM 详细信息。
- scheduledtasks: 显示应用中所有计划任务的信息,包括已注册的定时任务。
- mappings: 显示所有 HTTP 请求路径的映射信息,帮助了解应用的 API 结构。
查看 /actuator/prometheus
/actuator/prometheus 端点是暴露给 prometheus ,供 prometheus 收集 spring-boot 数据。
在浏览器输入 http://localhost:8081/actuator/prometheus,便可查询到 prometheus 格式的度量数据
# HELP application_ready_time_seconds Time taken for the application to be ready to service requests
# TYPE application_ready_time_seconds gauge
application_ready_time_seconds{main_application_class="com.example.prometheus_demo.PrometheusDemoApplication"} 2.741
# HELP application_started_time_seconds Time taken to start the application
# TYPE application_started_time_seconds gauge
application_started_time_seconds{main_application_class="com.example.prometheus_demo.PrometheusDemoApplication"} 2.655
# HELP disk_free_bytes Usable space for path
# TYPE disk_free_bytes gauge
disk_free_bytes{path="D:\\workspace\\prometheus-demo\\."} 3.3373065216E10
# HELP disk_total_bytes Total space for path
# TYPE disk_total_bytes gauge
disk_total_bytes{path="D:\\workspace\\prometheus-demo\\."} 1.28033222656E11
# HELP executor_active_threads The approximate number of threads that are actively executing tasks
# TYPE executor_active_threads gauge
executor_active_threads{name="applicationTaskExecutor"} 0.0
# HELP executor_completed_tasks_total The approximate total number of tasks that have completed execution
# TYPE executor_completed_tasks_total counter
executor_completed_tasks_total{name="applicationTaskExecutor"} 0.0
# HELP executor_pool_core_threads The core number of threads for the pool
# TYPE executor_pool_core_threads gauge
executor_pool_core_threads{name="applicationTaskExecutor"} 8.0
# HELP executor_pool_max_threads The maximum allowed number of threads in the pool
# TYPE executor_pool_max_threads gauge
executor_pool_max_threads{name="applicationTaskExecutor"} 2.147483647E9
# HELP executor_pool_size_threads The current number of threads in the pool
# TYPE executor_pool_size_threads gauge
executor_pool_size_threads{name="applicationTaskExecutor"} 0.0
# HELP executor_queue_remaining_tasks The number of additional elements that this queue can ideally accept without blocking
# TYPE executor_queue_remaining_tasks gauge
executor_queue_remaining_tasks{name="applicationTaskExecutor"} 2.147483647E9
# HELP executor_queued_tasks The approximate number of tasks that are queued for execution
# TYPE executor_queued_tasks gauge
...

浙公网安备 33010602011771号