如何在SpringBoot中監控執行引擎(actuator)?
/metrics眾所周知,springboot幫助我們自動配置了很多引數,包含bean的自動化裝配,資料庫方言的自動匹配,環境變數的自動獲取和配置,埠等引數的預設配置等等,但是有的時候,我們在使用SpringBoot的時候,我們想知道其內部自動化配置後,到底配置成了什麼模樣,從而能夠幫助我們進一步的理解springboot已經有的時候還能幫助我們進行錯誤定位,那麼應該如何做呢?值得欣喜的是,springboot已經為大家考慮著點了,提供了一個執行引擎(actuator)來幫助我們監控,窺探和檢視SpringBoot內部的祕密。那應該如何做呢?請參考下面的方法。
1 新增actuator的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
新增上面的依賴後,啟動程式。假設啟動的埠為8080
2 檢視beans的情況
在瀏覽器中輸入:http://127.0.0.1:8080/beans
就能看到下面的資料
不太好檢視,咱們把其拷貝出來,找一個線上的json的格式化網站,比如:http://www.sojson.com/
[
{
"context": "application",
"parent": null,
"beans": [
{
"bean": "application",
"scope": "singleton",
"type": "tr.com.example.meeting.Application$$EnhancerBySpringCGLIB$$3d19621a" ,
"resource": "null",
"dependencies": []
},
{
"bean": "org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory",
"scope": "singleton",
"type": "org.springframework.core.type.classreading.CachingMetadataReaderFactory",
"resource": "null",
"dependencies": []
},
{
"bean": "hibernateConfiguration",
"scope": "singleton",
"type": "tr.com.example.meeting.configuration.HibernateConfiguration$$EnhancerBySpringCGLIB$$d67d9aee",
"resource": "file [F:/test/test/spring-react-restful-example-master/meeting-server/target/classes/tr/com/example/meeting/configuration/HibernateConfiguration.class]",
"dependencies": [
"environment"
]
},
{
"bean": "webConfiguration",
"scope": "singleton",
"type": "tr.com.example.meeting.configuration.WebConfiguration$$EnhancerBySpringCGLIB$$8a162dec",
"resource": "file [F:/test/test/spring-react-restful-example-master/meeting-server/target/classes/tr/com/example/meeting/configuration/WebConfiguration.class]",
"dependencies": []
},
{
"bean": "departmentController",
"scope": "singleton",
"type": "tr.com.example.meeting.controller.DepartmentController",
"resource": "file [F:/test/test/spring-react-restful-example-master/meeting-server/target/classes/tr/com/example/meeting/controller/DepartmentController.class]",
"dependencies": [
"departmentService"
]
},
{
"bean": "employeeController",
"scope": "singleton",
"type": "tr.com.example.meeting.controller.EmployeeController",
"resource": "file [F:/test/test/spring-react-restful-example-master/meeting-server/target/classes/tr/com/example/meeting/controller/EmployeeController.class]",
"dependencies": [
"employeeService"
]
},
3 檢視自動配置的情況(autoconfig)
http://127.0.0.1:8080/autoconfig
不在貼圖,自己補腦子
4 檢視自動配置的屬性情況(configprops)
http://127.0.0.1:8080/configprops
不在貼圖,自己補腦子
5 獲取執行緒活動快照(dump)
http://127.0.0.1:8080/dump
不在貼圖,自己補腦子
6 獲取環境變數
http://127.0.0.1:8080/env
不在貼圖,自己補腦子
另獲取特定名稱的環境變數的值得URL為:
http://127.0.0.1:8080/env/{name}
7 獲取健康狀況
http://127.0.0.1:8080/health
比如本地測試環境的資訊如下:
{"status":"DOWN","diskSpace":{"status":"UP","total":208928301056,"free":154188779520,"threshold":10485760},"db":{"status":"DOWN","error":"org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure\n\nThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."}}
8 獲取應用程式定製資訊
9 獲取程式度量資訊
{
"mem": 362841,
"mem.free": 153605,
"processors": 8,
"instance.uptime": 1735765,
"uptime": 1741061,
"systemload.average": -1,
"heap.committed": 296960,
"heap.init": 196608,
"heap.used": 143354,
"heap": 2768896,
"nonheap.committed": 68800,
"nonheap.init": 2496,
"nonheap.used": 65883,
"nonheap": 0,
"threads.peak": 26,
"threads.daemon": 24,
"threads.totalStarted": 30,
"threads": 26,
"classes": 8639,
"classes.loaded": 8639,
"classes.unloaded": 0,
"gc.ps_scavenge.count": 8,
"gc.ps_scavenge.time": 57,
"gc.ps_marksweep.count": 2,
"gc.ps_marksweep.time": 127,
"httpsessions.max": -1,
"httpsessions.active": 0,
"gauge.response.beans": 80,
"gauge.response.mappings": 6,
"gauge.response.env": 8,
"gauge.response.autoconfig": 38,
"gauge.response.dump": 66,
"gauge.response.health": 1028,
"gauge.response.configprops": 90,
"gauge.response.unmapped": 6,
"gauge.response.info": 18,
"counter.status.200.mappings": 1,
"counter.status.200.beans": 1,
"counter.status.200.info": 1,
"counter.status.200.configprops": 1,
"counter.status.200.autoconfig": 1,
"counter.status.200.dump": 1,
"counter.status.503.health": 1,
"counter.status.404.unmapped": 1,
"counter.status.200.env": 1
}
10 獲取全部的URI路徑已經他們的對映關係
http://127.0.0.1:8080/mappings
這個方法太酷了,其還包含了執行引擎(actuator)本身的URL,好了,既然這個地址已經幫我們把所有的執行引擎(actuator)能用的URL都已經列出來,後面還準備列出但是沒有列出的(比如 /shutdown, /trace ),我就不在贅述. 該睡覺了,大家晚安。
"{[/error]}": {
"bean": "requestMappingHandlerMapping",
"method": "public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)"
},
"{[/error],produces=[text/html]}": {
"bean": "requestMappingHandlerMapping",
"method": "public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)"
},
"{[/env/{name:.*}],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)"
},
"{[/env || /env.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/mappings || /mappings.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/health || /health.json],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)"
},
"{[/beans || /beans.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/info || /info.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/metrics/{name:.*}],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)"
},
"{[/metrics || /metrics.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/trace || /trace.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}": {
"bean": "endpointHandlerMapping",
"method": "public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException"
},
"{[/configprops || /configprops.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/dump || /dump.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
},
"{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/json]}": {
"bean": "endpointHandlerMapping",
"method": "public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()"
}