spring boot actuator介绍
-
Spring Boot包含许多其他功能,可帮助您在将应用程序推送到生产环境时监视和管理应用程序。
-
您可以选择使用HTTP端点或JMX来管理和监视应用程序。
-
审核,运行状况和指标收集也可以自动应用于您的应用程序。
总之Spring Boot Actuator就是一款可以帮助你监控系统数据的框架,其可以监控很多很多的系统数据,它有对应用系统的自省和监控的集成功能,可以查看应用配置的详细信息,如:
显示应用程序员的Health健康信息显示Info应用信息
显示HTTP Request跟踪信息
显示当前应用程序的“Metrics”信息
显示所有的@RequestMapping的路径信息
显示应用程序的各种配置信息
显示你的程序请求的次数 时间 等各种信息
引入Actuactor三角坐标依赖
<!-- 端点监控的配置-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>org.springframework.hateoas</groupId><artifactId>spring-hateoas</artifactId>
</dependency>
访问: http://localhost:9999/actuator/ 响应信息如下所示
{
"_links": {
"self": {
"href": "http://localhost:9999/actuator",
"templated": false
},
"beans": {
"href": "http://localhost:9999/actuator/beans",
"templated": false
},
"caches-cache": {
"href": "http://localhost:9999/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://localhost:9999/actuator/caches",
"templated": false
},
"health": {
"href": "http://localhost:9999/actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:9999/actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://localhost:9999/actuator/info",
"templated": false
},
"conditions": {
"href": "http://localhost:9999/actuator/conditions",
"templated": false
},
"configprops": {
"href": "http://localhost:9999/actuator/configprops",
"templated": false
},
"configprops-prefix": {
"href": "http://localhost:9999/actuator/configprops/{prefix}",
"templated": true
},
"env": {
"href": "http://localhost:9999/actuator/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:9999/actuator/env/{toMatch}",
"templated": true
},
"loggers": {
"href": "http://localhost:9999/actuator/loggers",
"templated": false
},
"loggers-name": {
"href": "http://localhost:9999/actuator/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://localhost:9999/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:9999/actuator/threaddump",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:9999/actuator/metrics/{requiredMetricName}",
"templated": true
},
"metrics": {
"href": "http://localhost:9999/actuator/metrics",
"templated": false
},
"scheduledtasks": {
"href": "http://localhost:9999/actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://localhost:9999/actuator/mappings",
"templated": false
}
}
}
访问health端点: http://localhost:9999/actuator/health 响应信息如下:
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 1333606182912,
"free": 1269725843456,
"threshold": 10485760,
"exists": true
}
},
"elasticsearch": {
"status": "UP",
"details": {
"cluster_name": "elasticsearch",
"status": "green",
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,
"active_primary_shards": 0,
"active_shards": 0,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 0,
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 0,
"active_shards_percent_as_number": 100.0
}
},
"ping": {
"status": "UP"
},
"r2dbc": {
"status": "UP",
"details": {
"database": "Jasync-MySQL",
"validationQuery": "validate(REMOTE)"
}
}
}
}
开启 Info端点: yml文件中配置:
# 显示任意的应用信息,默认关闭 springBoot版本:2.7.5 CURRENT GA如果是更低一些的版本默认是开启的
# 在spring boot 2.0以后,actuator默认只开启了info和health两个端点,要想使用其他的端点,需要在application.yml中打开 management:endpoint:health:show-details: always # 配置health端点显示详细信息info:env:enabled: true # 显示任意的应用信息,默认关闭 springBoot版本:2.7.5 CURRENT GA如果是更低一些的版本默认是开启的endpoints:web:exposure:include: "*"cors:allowed-headers: "*"info:app:encoding: @project.build.sourceEncoding@java:source: @java.version@target: @java.version@
访问info端点: http://localhost:9999/actuator/info 响应信息如下:
{
"app": {
"encoding": "UTF-8",
"java": {
"source": "1.8.0_221",
"target": "1.8.0_221"
}
}
}
拓展info端点:
package org.jd.websocket.auth.data.reactor.help;import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.stereotype.Component;import java.util.Collections; import java.util.Date; import java.util.Map; import java.util.concurrent.ConcurrentHashMap;@Component public class CustomBuildInfoContributor implements InfoContributor {@Overridepublic void contribute(Info.Builder builder) {Map<String,Object> details= new ConcurrentHashMap<>();details.put("build",Collections.singletonMap("timestamp",new Date()));details.put("author","YangGe");builder.withDetails(details);} }
再次访问info端点: http://localhost:9999/actuator/info 响应信息如下:
{
"app": {
"encoding": "UTF-8",
"java": {
"source": "1.8.0_221",
"target": "1.8.0_221"
}
},
"build": {
"timestamp": "2023-08-07T15:14:28.463+00:00"
},
"author": "YangGe"
}
management:
endpoints:
web:
base-path: /actuator #配置端点访问前缀
exposure:
include: info,health #只暴露info,health两个端点; “*” 表示暴露所有端点
exclude: health #可以将以暴露的端点排除(不暴露)
其他更细节的配置可以看官网
spring boot 热部署导入devtool依赖idea窗口钝化
yml yet anothor markup language
actuator是spring boot 提供的对应系统的自省和监控的基础功能,当出现问题时可以及时的定位问题。
拓展Metrics端点:
http://localhost:9999/actuator/metrics 访问该端点,响应信息如下
{
"names":[
"application.ready.time",
"application.started.time",
"disk.free",
"disk.total",
"executor.active",
"executor.completed",
"executor.pool.core",
"executor.pool.max",
"executor.pool.size",
"executor.queue.remaining",
"executor.queued",
"http.server.requests",
"jvm.buffer.count",
"jvm.buffer.memory.used",
"jvm.buffer.total.capacity",
"jvm.classes.loaded",
"jvm.classes.unloaded",
"jvm.gc.live.data.size",
"jvm.gc.max.data.size",
"jvm.gc.memory.allocated",
"jvm.gc.memory.promoted",
"jvm.gc.overhead",
"jvm.gc.pause",
"jvm.memory.committed",
"jvm.memory.max",
"jvm.memory.usage.after.gc",
"jvm.memory.used",
"jvm.threads.daemon",
"jvm.threads.live",
"jvm.threads.peak",
"jvm.threads.states",
"logback.events",
"process.cpu.usage",
"process.start.time",
"process.uptime",
"system.cpu.count",
"system.cpu.usage"
]
}对应这些端点信息,用于实现生产级的度量工具. 这些指标包括内存总量,空闲内存数据,处理器数量,系统正常运行时间,堆信息等,如果想了解某项指标的信息,
http://localhost:9999/actuator/metrics
端点后加上上述指标的名称即可。如当前内存使用情况可以通过:
http://localhost:9999/actuator/metrics/jvm.memory.used 就会得到如下响应信息:
{
"name":"jvm.memory.used",
"description":"The amount of used memory",
"baseUnit":"bytes",
"measurements":[
{
"statistic":"VALUE",
"value":479145136
}
],
"availableTags":[
{
"tag":"area",
"values":[
"heap",
"nonheap"
]
},
{
"tag":"id",
"values":[
"Compressed Class Space",
"PS Survivor Space",
"PS Old Gen",
"Metaspace",
"PS Eden Space",
"Code Cache"
]
}
]
}
http://localhost:9999/actuator/metrics/jvm.memory.usage.after.gc gc回收内存的情况
{
"name":"jvm.memory.usage.after.gc",
"description":"The percentage of long-lived heap pool used after the last GC event, in the range [0..1]",
"baseUnit":"percent",
"measurements":[
{
"statistic":"VALUE",
"value":0.0014572154044711605
}
],
"availableTags":[
{
"tag":"area",
"values":[
"heap"
]
},
{
"tag":"pool",
"values":[
"long-lived"
]
}
]
}