Spring Boot Actuator的關鍵特性是在應用程序裏提供衆多Web端點,通過它們瞭解應用程序運行時的內部狀況。
有了Actuator,你可以知道Bean在Spring應用程序上下文裏是如何組裝在一起的,掌握應用程序可以獲取的環境屬性信息,獲取運行時度量信息的快照。
1. 簡單使用
1. Maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 修改配置文件application.yml
management:
endpoints:
web:
exposure:
include: "*"
3. 獲取接口apis
http://localhost:8080/actuator/
2. /beans端點產生的報告能告訴你Spring應用程序上下文裏都有哪些Bean
{
"myTimerTask":{
"aliases":[
],
"scope":"singleton",
"type":"com.test.spring.timer.controller.MyTimerTask",
"resource":"file [/Users/didi/mytemps/timerDemo/target/classes/com/test/spring/timer/controller/MyTimerTask.class]",
"dependencies":[
"user"
]
}
}
3. /conditions端點能告訴你爲什麼會有這個Bean,或者爲什麼沒有這個Bean。
{
"positiveMatches":{
"AuditAutoConfiguration#auditListener":[
{
"condition":"OnBeanCondition",
"message":"@ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.listener.AbstractAuditListener; SearchStrategy: all) did not find any beans"
}
]
},
"negativeMatches":{
"RabbitHealthIndicatorAutoConfiguration":{
"notMatched":[
{
"condition":"OnClassCondition",
"message":"@ConditionalOnClass did not find required class 'org.springframework.amqp.rabbit.core.RabbitTemplate'"
}
]
}
}
}
4. /mappings羅列出應用程序發佈的全部端點
{
"handler":"public java.lang.String com.test.spring.timer.controller.MyTimerTask.timerTask()",
"predicate":"{ /outer/timerTask}",
"details":{
"handlerMethod":{
"className":"com.test.spring.timer.controller.MyTimerTask",
"name":"timerTask",
"descriptor":"()Ljava/lang/String;"
},
"requestMappingConditions":{
"consumes":[
],
"headers":[
],
"methods":[
],
"params":[
],
"patterns":[
"/outer/timerTask"
],
"produces":[
]
}
}
}
5. Actuator提供了一系列端點,讓你能在運行時快速檢查應用程序
http://localhost:8080/actuator/metrics/jvm.memory.max
{
"name":"jvm.memory.max",
"description":"The maximum amount of memory in bytes that can be used for memory management",
"baseUnit":"bytes",
"measurements":[
{
"statistic":"VALUE",
"value":3457679359
}
],
"availableTags":[
{
"tag":"area",
"values":[
"heap",
"nonheap"
]
},
{
"tag":"id",
"values":[
"Compressed Class Space",
"PS Survivor Space",
"PS Old Gen",
"Metaspace",
"PS Eden Space",
"Code Cache"
]
}
]
}
6. /trace端點能報告所有Web請求的詳細信息,包括請求方法、路徑、時間戳以及請求和響應的頭信息
{
"timestamp":"2018-12-22T09:10:33.925Z",
"principal":null,
"session":null,
"request":{
"method":"GET",
"uri":"http://localhost:8080/actuator",
"headers":{
"accept-language":[
"zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
],
"postman-token":[
"9293704b-285a-061a-c0c3-92fe1f6291da"
],
"host":[
"localhost:8080"
],
"connection":[
"keep-alive"
],
"cache-control":[
"no-cache"
],
"accept-encoding":[
"gzip, deflate, br"
],
"accept":[
"*/*"
],
"user-agent":[
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
]
},
"remoteAddress":null
},
"response":{
"status":200,
"headers":{
"Transfer-Encoding":[
"chunked"
],
"Date":[
"Sat, 22 Dec 2018 09:10:33 GMT"
],
"Content-Type":[
"application/vnd.spring-boot.actuator.v2+json;charset=UTF-8"
]
}
},
"timeTaken":1
}