對java工程實時監控方式很多,本文主要講在springboot框架中的監控。
springboot框架,自帶了actuator監控,在pom中引入jar包即可,如下
1.引入jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在2.0版本之后改動很大,我這里是用的2.0.2
2.啟動項目后即可訪問
http://localhost:8081/webproject/actuator/health
http://localhost:8081/webproject/actuator/info
如果想讓url個性化一點,在 application.propertie
配置文件中 加入
management.endpoints.web.base-path=/jiankong
那么就可以訪問
http://localhost:8081/webproject/jiankong/health
3.actuator 提供了很多api(稱為:節點)
默認只開放了 health、info兩個節點
如果需要公開所有 則在配置文件中繼續加入
management.endpoints.web.exposure.include=*
其中 health方法 是不顯示具體的內容的,如需要
則繼續加入配置
management.endpoint.health.show-details=always
具體方法和返回內容 可以參考API
GET /autoconfig 查看自動配置的使用情況 true
GET /configprops 查看配置屬性,包括默認配置 true
GET /beans 查看bean及其關系列表 true
GET /dump 打印線程棧 true
GET /env 查看所有環境變量 true
GET /env/{name} 查看具體變量值 true
GET /health 查看應用健康指標 false
GET /info 查看應用信息 false
GET /mappings 查看所有url映射 true
GET /metrics 查看應用基本指標 true
GET /metrics/{name} 查看具體指標 true
POST /shutdown 關閉應用 true
GET /trace 查看基本追蹤信息 true
4. 常用的查看具體指標的方法 /metrics/{name}
http://localhost:8081/webproject/actuator/metrics/http.server.requests
http://localhost:8081/webproject/actuator/metrics/jvm.memory.used
http://localhost:8081/webproject/actuator/metrics/jvm.threads.peak
等等,具體name 可以通過/metrics 獲得,
其中 http.server.requests 是對所有請求的url的監控,次數,時間 等。
那么如果我們需要針對個別重點controller分別監控呢~~
5.分別監控,需要在controller上做些手腳
在RequestMapping 上面 加上注解 @Timed(value = "list.base", longTask = true)
value可以就用mapping 這樣。
這個@Timed 注解 是 micrometer-core-1.0.3-sources.jar的 springboot監控本身用到的。
當訪問過這個controller之后 就可以調用~ 注意還沒有收到過請求的話是請求不到這個list.base的
http://localhost:8081/webproject/actuator/metrics/list.base
{
"availableTags":[
{
"tag":"exception",
"values":["None"]
},
{
"tag":"method",
"values":["POST"]
},
{
"tag":"uri",
"values":["/ListDataBase","root"]
},
{
"tag":"status",
"values":["200"]
}
],
"measurements":[
{
"statistic":"ACTIVE_TASKS",
"value":0
},
{
"statistic":"DURATION",
"value":0
},
{
"statistic":"COUNT",
"value":1
},
{
"statistic":"TOTAL_TIME",
"value":0.306317164
},
{
"statistic":"MAX",
"value":0.306317164
}
],
"name":"list.base"
}
如果有興趣研究更多。。。官網api
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready-endpoints