actuator是spring boot提供的對應(yīng)用系統(tǒng)的自省和監(jiān)控的集成功能,可以對應(yīng)用系統(tǒng)進(jìn)行配置查看、相關(guān)功能統(tǒng)計等。
使用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
:指定訪問這些監(jiān)控方法的端口,與邏輯接口端口分離。如果不想將這些暴露在http中,可以設(shè)置 management.port = -1 -
management.address
:指定地址,比如只能通過本機(jī)監(jiān)控,可以設(shè)置 management.address = 127.0.0.1
敏感信息訪問限制
根據(jù)上面表格,鑒權(quán)為 false
的,表示不敏感,可以隨意訪問,否則就是做了一些保護(hù),不能隨意訪問。
1
|
endpoints.mappings.sensitive= false |
這樣需要對每一個都設(shè)置,比較麻煩。敏感方法默認(rèn)是需要用戶擁有 actuator
角色,因此,也可以設(shè)置關(guān)閉安全限制:
1
|
management.security.enabled= false |
或者配合 spring security
做細(xì)粒度控制。
自定義系統(tǒng)信息
可以通過訪問 /info
獲取信息,需要在配置文件設(shè)置
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節(jié)點下artifactid屬性 artifact: "@project.artifactid@"
其他
/shutdown這個需要post方式,通過請求來關(guān)閉應(yīng)用。
這個操作比較敏感,要想真正生效,需要以下配置:
1
|
endpoints.shutdown.enabled: true |
我們可以通過實現(xiàn)healthindicator接口,編寫自己的/health方法邏輯。也可以增加自定義監(jiān)控方法。
查看詳細(xì)介紹,請移步官方文檔
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://juejin.im/post/5afbe1856fb9a07acc11e5a4