/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()"
}