前言
該模塊增加了很多監控點,使用
http://{ip}:{port}/{endpoint}
來訪問這些斷點,瞭解程序的運行情況。這是一個完全暴露自身信息的模塊,主要作用就是作爲監控和管理的。
引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
注意這裏也需要添加starter-web
模塊。
查看日誌
啓動程序後,通過查看日誌,我們很清晰的看到這裏的actuator地址只有這三個。分別是/actuator/health
、/actuator/info
、/actuator
2018-12-09 17:54:36.904 INFO 14364 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-12-09 17:54:36.905 INFO 14364 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-12-09 17:54:36.906 INFO 14364 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
如果我們需要查看其他的監控信息,則需要在配置中將其打開;在這裏提示,springBoot2.x之後配置方式和之前有所改變。
/actuator 所有監控地址
我們在地址欄中訪問這個地址,詳細的看到所有監點。正如上面日誌中所提到的只開放了這三個監控點。其他的監控信息,我們配置之後就可以看到。
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
}
}
}
/actuator/health 服務是否啓動
{
"status": "UP"
}
Up
表示正常運行,除此之外還有DOWN
、OUT_OF_SERVICE
、UNKNOWN
等狀態。
/info
info屬性來自定義info端點公開的數據。
配置開啓所有監控端點
management:
endpoints:
web:
exposure:
include: "*"
開啓所有監控端點,運行程序,可以在日誌當中看到映射mapping地址;同樣也可以在程序正常啓動後通過訪問http://localhost:8080/actuator
後的json數據中顯示所監控的信息。
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"auditevents": {
"href": "http://localhost:8080/actuator/auditevents",
"templated": false
},
"beans": {
"href": "http://localhost:8080/actuator/beans",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"conditions": {
"href": "http://localhost:8080/actuator/conditions",
"templated": false
},
"configprops": {
"href": "http://localhost:8080/actuator/configprops",
"templated": false
},
"env": {
"href": "http://localhost:8080/actuator/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8080/actuator/env/{toMatch}",
"templated": true
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
},
"loggers": {
"href": "http://localhost:8080/actuator/loggers",
"templated": false
},
"loggers-name": {
"href": "http://localhost:8080/actuator/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://localhost:8080/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8080/actuator/threaddump",
"templated": false
},
"metrics": {
"href": "http://localhost:8080/actuator/metrics",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
"templated": true
},
"scheduledtasks": {
"href": "http://localhost:8080/actuator/scheduledtasks",
"templated": false
},
"httptrace": {
"href": "http://localhost:8080/actuator/httptrace",
"templated": false
},
"mappings": {
"href": "http://localhost:8080/actuator/mappings",
"templated": false
}
}
}
management.endpoints.web.exposure.include:
默認情況只是開啓了/health 和 /info端點,想要暴露所有的端點只需要設置成*
management:
endpoints:
web:
exposure:
include: "*"
management.server.servlet.context-path:
設置管理端點的上下文路徑,默認是"",此時設置了,地址欄訪問:http://localhost:9000/au/actuator
management:
server:
servlet:
context-path: /au
management.server.port:
設置管理服務的端口,默認和應用端口保持一致。
management:
server:
port: 9000
關於這些值配置,在spring-boot-actuator-autoconfigure-{xxx}.jar
這個jar包中有一個spring-configuration-metadata.json
包含了
management:
server:
servlet:
context-path: "/management"
port: 8088
endpoints:
web:
exposure:
include: "*"
base-path: "/monitor"
endpoint:
shutdown:
enabled: true
info:
app:
name: "@project.artifactId@"
encoding: '@project.build.sourceEncoding@'
java:
source: '@java.version@'
target: '@java.version@'
現將配置修改如上,/actuator 在 management.endpoints.web.base-path 的根目錄中有一個映射,它提供了到所有暴露端點的鏈接。現將其修改成了/monitor
,默認爲/actuator
。info
如上所說自定義屬性系列信息,在此打印服務名稱和服務編碼和基礎環境等信息。
聯繫
聰聰的獨立博客 ,一個喜歡技術,喜歡鑽研的95後。如果你看到這篇文章,千里之外,我在等你聯繫。