actuator是spring boot提供的對應用系統的自省和監控的集成功能,可以對應用系統進行配置查看、相關功能統計等。
使用actuator
引入依賴即可
maven
:
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> |
gradle
:
1
|
compile( 'org.springframework.boot:spring-boot-starter-actuator' ) |
endpoints
列舉一些主要的endpoints
配置文件屬性介紹
地址和端口的配置
-
management.port
:指定訪問這些監控方法的端口,與邏輯接口端口分離。如果不想將這些暴露在http中,可以設置 management.port = -1 -
management.address
:指定地址,比如只能通過本機監控,可以設置 management.address = 127.0.0.1
敏感信息訪問限制
根據上面表格,鑒權為 false
的,表示不敏感,可以隨意訪問,否則就是做了一些保護,不能隨意訪問。
1
|
endpoints.mappings.sensitive= false |
這樣需要對每一個都設置,比較麻煩。敏感方法默認是需要用戶擁有 actuator
角色,因此,也可以設置關閉安全限制:
1
|
management.security.enabled= false |
或者配合 spring security
做細粒度控制。
自定義系統信息
可以通過訪問 /info
獲取信息,需要在配置文件設置
1
2
3
4
5
6
7
8
9
10
11
|
info: aaa: name: xxx email: xxx @qq .com 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方式,通過請求來關閉應用。
這個操作比較敏感,要想真正生效,需要以下配置:
1
|
endpoints.shutdown.enabled: true |
我們可以通過實現healthindicator接口,編寫自己的/health方法邏輯。也可以增加自定義監控方法。
查看詳細介紹,請移步官方文檔
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://juejin.im/post/5afbe1856fb9a07acc11e5a4