Actuator是Spring Boot提供的對應用系統的自省和監控的集成功能,可以對應用系統進行配置查看、相關功能統計等。
使用Actuator
引入依賴即可
Maven :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
Gradle :
compile('org.springframework.boot:spring-boot-starter-actuator')Endpoints
列舉一些主要的endpoints
配置文件屬性介紹
地址和端口的配置
management.port :指定訪問這些監控方法的端口,與邏輯接口端口分離。如果不想將這些暴露在http中,可以設置management.port = -1management.address :指定地址,比如只能通過本機監控,可以設置management.address = 127.0.0.1敏感信息訪問限制
根據上面表格,鑑權為false的,表示不敏感,可以隨意訪問,否則就是做了一些保護,不能隨意訪問。
endpoints.mappings.sensitive=false
這樣需要對每一個都設置,比較麻煩。敏感方法默認是需要用戶擁有ACTUATOR角色,因此,也可以設置關閉安全限制:
management.security.enabled=false
或者配合Spring Security做細粒度控制。
自定義系統信息
可以通過訪問/info獲取信息,需要在配置文件設置
info: aaa: name: xxx email: [email protected] bbb: age: 25 hobbies: running build: artifact: "@project.artifactId@" name: "@project.name@" version: "@project.version@"
此時訪問localhost:8080/info 返回一下信息
如果使用maven ,可以訪問pom.xml文件的信息,用法如下:
// 獲取pom.xml中project節點下artifactId屬性artifact: "@project.artifactId@"
其他
/shutdown這個需要post方式,通過請求來關閉應用。
這個操作比較敏感,要想真正生效,需要以下配置:
endpoints.shutdown.enabled: true
我們可以通過實現HealthIndicator接口,編寫自己的/health方法邏輯。也可以增加自定義監控方法。
查看詳細介紹,請移步官方文檔
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。