SpringBoot自帶監(jiān)控功能Actuator,可以幫助實現對程序內部運行情況監(jiān)控,比如監(jiān)控狀況、Bean加載情況、環(huán)境變量、日志信息、線程信息等
pom文件中添加
<!-- actuator start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${springboot.version}</version>
</dependency>
<!-- actuator end-->
yaml文件
監(jiān)控端口必須單獨配置,否則請求不到
server:
port: 8091
#必須配置,否則端點默認禁用
management:
endpoints:
web:
# 路徑配置
# base-path: /manage 修改默認的actuator/*
exposure:
include: "*"
# “*”號代表啟用所有的監(jiān)控端點,可以單獨啟用,例如,health,info,metrics等
# exclude: beans #關閉部分監(jiān)控點
server:
port: 8091
endpoint:
health:
show-details: always
啟動項目,請求接口:http:/localhost:8092/actuator 返回的是可以查看的所有接口
health接口
如果項目中同步引用了oracle和redis,請求接口/health 時會帶出狀態(tài),但信息不是很詳細
{
"status":"UP",
"components":{
"db":{
"status":"UP",
"details":{
"database":"Oracle",
"validationQuery":"isValid()"
}
},
"diskSpace":{
"status":"UP",
"details":{
"total":427390136320,
"free":412959125504,
"threshold":10485760,
"exists":true
}
},
"ping":{
"status":"UP"
},
"redis":{
"status":"UP",
"details":{
"version":"5.0.5"
}
}
}
}
如果想關閉特定的檢查指標
management:
health:
redise:
enabled: false
info接口
就是在配置文件中以info開頭的配置信息比如
info:
app:
name:
spring-boot-actuator
version: 1.0.0
test: test
調用接口展示如下
{
"app": {
"name": "spring-boot-actuator",
"version": "1.0.0",
"test":"test"
}
}
beans
展示了 bean 的別名、類型、是否單例、類的地址、依賴等信息。
conditions
可以在應用運行時查看代碼了某個配置在什么條件下生效,或者某個自動配置為什么沒有生效。
heapdump
訪問actuator/heapdump 會自動生成一個jvm的堆文件heapdump 可以用jdk自帶的jvm監(jiān)控工具visualVM打開查看
shutdown
接口關閉springboot服務,需要配置文件開啟
management:
endpoint:
shutdown:
enabled: true
只支持post接口
curl -X POST "http://localhost:8080/actuator/shutdown"
{
"message": "stop"
}
mappings
展示全部的uri路徑,和控制器的關系
在項目統(tǒng)計的時候還是蠻有用的
{
"contexts":{
"application":{
"mappings":{
"dispatcherServlets":{
"dispatcherServlet":[
{
"handler":"example.controller.TestController#get(String)",
"predicate":"{GET /test/get}",
"details":{
"handlerMethod":{
"className":"example.controller.TestController",
"name":"get",
"descriptor":"(Ljava/lang/String;)Ljava/lang/Object;"
},
"requestMappingConditions":{
"consumes":[
],
"headers":[
],
"methods":[
"GET"
],
"params":[
],
"patterns":[
"/test/get"
],
"produces":[
]
}
}
},
{
"handler":"ResourceHttpRequestHandler [\"classpath:/META-INF/resources/webjars/\"]",
"predicate":"/webjars/**",
"details":null
},
{
"handler":"ResourceHttpRequestHandler [\"classpath:/META-INF/resources/\", \"classpath:/resources/\", \"classpath:/static/\", \"classpath:/public/\", \"/\"]",
"predicate":"/**",
"details":null
}
]
},
"servletFilters":[
{
"servletNameMappings":[
],
"urlPatternMappings":[
"/*"
],
"name":"webMvcMetricsFilter",
"className":"org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter"
}
],
"servlets":[
{
"mappings":[
],
"name":"default",
"className":"org.apache.catalina.servlets.DefaultServlet"
}
]
},
"parentId":null
}
}
}
threaddump接口
會產生當前線程活動的快照,主要展示線程名稱、線程id、線程狀態(tài)等
示例如下
{
"threads":[
{
"threadName":"boundedElastic-evictor-1",
"threadId":109,
"blockedTime":-1,
"blockedCount":0,
"waitedTime":-1,
"waitedCount":24,
"lockName":"java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@20ab793d",
"lockOwnerId":-1,
"lockOwnerName":null,
"inNative":false,
"suspended":false,
"threadState":"TIMED_WAITING",
"stackTrace":[
{
"methodName":"take",
"fileName":"ScheduledThreadPoolExecutor.java",
"lineNumber":809,
"className":"java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue",
"nativeMethod":false
}
]
}
]
}
loggers接口
可以查看當前應用的日志級別等信息
metrics接口
http://localhost:8092/actuator/metrics后返回的指標
再任意訪問一個參數可以獲得對應的指標
http://localhost:8092/actuator/metrics/jvm.buffer.memory.used
{
"name":"jvm.buffer.memory.used",
"description":"An estimate of the memory that the Java virtual machine is using for this buffer pool",
"baseUnit":"bytes",
"measurements":[
{
"statistic":"VALUE",
"value":65537
}
],
"availableTags":[
{
"tag":"id",
"values":[
"direct",
"mapped"
]
}
]
}
序號 參數 參數說明 是否監(jiān)控 監(jiān)控手段 重要度
JVM
1 jvm.memory.max JVM 最大內存
2 jvm.memory.committed JVM 可用內存 是 展示并監(jiān)控堆內存和 Metaspace 重要
3 jvm.memory.used JVM 已用內存 是 展示并監(jiān)控堆內存和 Metaspace 重要
4 jvm.buffer.memory.used JVM 緩沖區(qū)已用內存
5 jvm.buffer.count 當前緩沖區(qū)數
6 jvm.threads.daemon JVM 守護線程數 是 顯示在監(jiān)控頁面
7 jvm.threads.live JVM 當前活躍線程數 是 顯示在監(jiān)控頁面;監(jiān)控達到閾值時報警 重要
8 jvm.threads.peak JVM 峰值線程數 是 顯示在監(jiān)控頁面
9 jvm.classes.loaded 加載 classes 數
10 jvm.classes.unloaded 未加載的 classes 數
11 jvm.gc.memory.allocated GC 時,年輕代分配的內存空間
12 jvm.gc.memory.promoted GC 時,老年代分配的內存空間
13 jvm.gc.max.data.size GC 時,老年代的最大內存空間
14 jvm.gc.live.data.size FullGC 時,老年代的內存空間
15 jvm.gc.pause GC 耗時 是 顯示在監(jiān)控頁面
TOMCAT
16 tomcat.sessions.created tomcat 已創(chuàng)建 session 數
17 tomcat.sessions.expired tomcat 已過期 session 數
18 tomcat.sessions.active.current tomcat 活躍 session 數
19 tomcat.sessions.active.max tomcat 最多活躍 session 數 是 顯示在監(jiān)控頁面,超過閾值可報警或者進行動態(tài)擴容 重要
20 tomcat.sessions.alive.max.second tomcat 最多活躍 session 數持續(xù)時間
21 tomcat.sessions.rejected 超過 session 最大配置后,拒絕的 session 個數 是 顯示在監(jiān)控頁面,方便分析問題
22 tomcat.global.error 錯誤總數 是 顯示在監(jiān)控頁面,方便分析問題
23 tomcat.global.sent 發(fā)送的字節(jié)數
24 tomcat.global.request.max request 最長時間
25 tomcat.global.request 全局 request 次數和時間
26 tomcat.global.received 全局 received 次數和時間
27 tomcat.servlet.request servlet 的請求次數和時間
28 tomcat.servlet.error servlet 發(fā)生錯誤總數
29 tomcat.servlet.request.max servlet 請求最長時間
30 tomcat.threads.busy tomcat 繁忙線程 是 顯示在監(jiān)控頁面,據此檢查是否有線程夯住
31 tomcat.threads.current tomcat 當前線程數(包括守護線程) 是 顯示在監(jiān)控頁面 重要
32 tomcat.threads.config.max tomcat 配置的線程最大數 是 顯示在監(jiān)控頁面 重要
33 tomcat.cache.access tomcat 讀取緩存次數
34 tomcat.cache.hit tomcat 緩存命中次數
CPU
35 system.cpu.count CPU 數量
36 system.load.average.1m load average 是 超過閾值報警 重要
37 system.cpu.usage 系統(tǒng) CPU 使用率
38 process.cpu.usage 當前進程 CPU 使用率 是 超過閾值報警
39 http.server.requests http 請求調用情況 是 顯示 10 個請求量最大,耗時最長的 URL;統(tǒng)計非 200 的請求量 重要
40 process.uptime 應用已運行時間 是 顯示在監(jiān)控頁面
41 process.files.max 允許最大句柄數 是 配合當前打開句柄數使用
42 process.start.time 應用啟動時間點 是 顯示在監(jiān)控頁面
43 process.files.open 當前打開句柄數 是 監(jiān)控文件句柄使用率,超過閾值后報警 重要
監(jiān)控頁面
<dependency>
<groupId>cn.pomit</groupId>
<artifactId>spring-boot-monitor</artifactId>
<version>0.0.1</version>
</dependency>
啟動后訪問
http://localhost:8091/monitor
只有一個應用點進去文章來源:http://www.zghlxwxcb.cn/news/detail-809417.html
參考文章:
https://www.pomit.cn/SpringBootMonitor/#/
https://blog.csdn.net/yunfeather/article/details/122581536文章來源地址http://www.zghlxwxcb.cn/news/detail-809417.html
到了這里,關于springboot 整合 actuator監(jiān)控詳情的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!