深入講解SpringBoot Actuator是什么
《Spring Boot Actuator詳解與深入應(yīng)用》預(yù)計(jì)包括三篇,第一篇重點(diǎn)講Spring Boot Actuator 1.x的應(yīng)用與定制端點(diǎn);第二篇將會(huì)對(duì)比Spring Boot Actuator 2.x 與1.x的區(qū)別,以及應(yīng)用和定制2.x的端點(diǎn);第三篇將會(huì)介紹Actuator metric指標(biāo)與Prometheus和Grafana的使用結(jié)合。這部分內(nèi)容很常用,且較為入門,歡迎大家的關(guān)注。
Actuator是什么
Spring Boot Actuator提供了生產(chǎn)上經(jīng)常用到的功能(如健康檢查,審計(jì),指標(biāo)收集,HTTP跟蹤等),幫助我們監(jiān)控和管理Spring Boot應(yīng)用程序。這些功能都可以通過JMX或HTTP端點(diǎn)訪問。
通過引入相關(guān)的依賴,即可監(jiān)控我們的應(yīng)用程序,收集指標(biāo)、了解流量或數(shù)據(jù)庫的狀態(tài)變得很簡單。該庫的主要好處是我們可以獲得生產(chǎn)級(jí)工具,而無需自己實(shí)際實(shí)現(xiàn)這些功能。與大多數(shù)Spring模塊一樣,我們可以通過多種方式輕松配置或擴(kuò)展它。
Actuator還可以與外部應(yīng)用監(jiān)控系統(tǒng)集成,如Prometheus,Graphite,DataDog,Influx,Wavefront,New Relic等等。 這些系統(tǒng)為您提供出色的儀表板,圖形,分析和警報(bào),以幫助我們?cè)谝粋€(gè)統(tǒng)一界面監(jiān)控和管理應(yīng)用服務(wù)。
本文將會(huì)介紹Spring Boot Actuator 1.x 包括其中的端點(diǎn)(HTTP端點(diǎn))、配置管理以及擴(kuò)展和自定義端點(diǎn)。
快速開始
引入如下的依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
Spring Boot Actuator 1.x
在1.x中,Actuator遵循讀寫模型,這意味著我們可以從中讀取信息或?qū)懭胄畔?。我們可以檢索指標(biāo)或我們的應(yīng)用程序的健康狀況,當(dāng)然我們也可以優(yōu)雅地終止我們的應(yīng)用程序或更改我們的日志配置。Actuator通過Spring MVC暴露其HTTP端點(diǎn)。
端點(diǎn)
當(dāng)引入的Actuator的版本為1.x時(shí),啟動(dòng)應(yīng)用服務(wù),可以控制臺(tái)輸出如下的端點(diǎn)信息:
我們介紹一下常用的endpoints:
- /health:顯示應(yīng)用程序運(yùn)行狀況信息(通過未經(jīng)身份驗(yàn)證的連接訪問時(shí)的簡單“狀態(tài)”或經(jīng)過身份驗(yàn)證時(shí)的完整消息詳細(xì)信息),它默認(rèn)不敏感
- /info:顯示應(yīng)用程序信息,默認(rèn)情況下不敏感
- /metrics:顯示當(dāng)前應(yīng)用程序的“指標(biāo)”信息,它默認(rèn)也很敏感
- /trace:顯示跟蹤信息(默認(rèn)情況下是最后幾個(gè)HTTP請(qǐng)求)
有些端點(diǎn)默認(rèn)并不會(huì)被開啟,如/shutdown。
配置端點(diǎn)
我們可以自定義每個(gè)端點(diǎn)的屬性,按照如下的格式:
endpoints.[endpoint name].[property to customize]
可以自定義的屬性有如下三個(gè):
- id,暴露的http端點(diǎn)地址
- enabled,是否開啟
- sensitive,當(dāng)為true時(shí),需要認(rèn)證之后才會(huì)通過http獲取到敏感信息
我們?cè)谂渲梦募性黾尤缦碌呐渲?,將?huì)定制/beans端點(diǎn)。
endpoints.beans.id=springbeans
endpoints.beans.sensitive=false
endpoints.beans.enabled=true
/health端點(diǎn)
/health端點(diǎn)用于監(jiān)控運(yùn)行的服務(wù)實(shí)例狀態(tài),當(dāng)服務(wù)實(shí)例下線或者因?yàn)槠渌脑蜃兊卯惓#ㄈ鏒B連接不上,磁盤缺少空間)時(shí),將會(huì)及時(shí)通知運(yùn)維人員。
在默認(rèn)未授權(quán)的情況下,通過HTTP的方式僅會(huì)返回如下的簡單信息:
{
"status": "UP"
}
獲取詳細(xì)的health信息
我們進(jìn)行如下的配置:
endpoints:health:id: chealthsensitive: false
management.security.enabled: false
如上一小節(jié)所述,我們更改了/health端點(diǎn)的訪問路徑為/chealth,并將安全授權(quán)關(guān)閉。訪問http://localhost:8005/chealth
將會(huì)得到如下的結(jié)果。
{
"status": "UP",
"healthCheck": {
"status": "UP"
},
"diskSpace": {
"status": "UP",
"total": 999995129856,
"free": 762513104896,
"threshold": 10485760
}
}
自定義health的信息
我們還可以定制實(shí)現(xiàn)health指示器。它可以收集特定于應(yīng)用程序的任何類型的自定義運(yùn)行狀況數(shù)據(jù),并通過/health端點(diǎn)訪問到定義的信息。
@Component public class HealthCheck implements HealthIndicator {@Overridepublic Health health() {int errorCode = check(); // perform some specific health checkif (errorCode != 0) {return Health.down().withDetail("Error Code", errorCode).build();}return Health.up().build();}public int check() {// Our logic to check healthreturn 0;} }
實(shí)現(xiàn)HealthIndicator
接口,并覆寫其中的health()
方法即可自定義我們的/health端點(diǎn)。
/info端點(diǎn)
通過/info端點(diǎn),我們可以為應(yīng)用服務(wù)定義一些基本信息:
info.app.name=Spring Sample Application
info.app.description=This is my first spring boot application
info.app.version=1.0.0
我們?cè)谌缟系呐渲弥卸x了服務(wù)名、描述和服務(wù)的版本號(hào)。
/metrics端點(diǎn)
/metrics端點(diǎn)展示了OS、JVM和應(yīng)用級(jí)別的指標(biāo)信息。當(dāng)開啟之后,我們可以獲取內(nèi)存、堆、線程、線程池、類加載和HTTP等信息。
{
"mem": 417304,
"mem.free": 231678,
"processors": 4,
"instance.uptime": 248325,
"uptime": 250921,
"systemload.average": 1.9541015625,
"heap.committed": 375296,
"heap.init": 393216,
"heap.used": 143617,
"heap": 5592576,
"nonheap.committed": 43104,
"nonheap.init": 2496,
"nonheap.used": 42010,
"nonheap": 0,
"threads.peak": 30,
"threads.daemon": 18,
"threads.totalStarted": 46,
"threads": 20,
"classes": 6020,
"classes.loaded": 6020,
"classes.unloaded": 0,
"gc.ps_scavenge.count": 3,
"gc.ps_scavenge.time": 35,
"gc.ps_marksweep.count": 1,
"gc.ps_marksweep.time": 29,
"httpsessions.max": -1,
"httpsessions.active": 0,
"gauge.response.info": 38.0,
"counter.status.200.info": 1
}
定制metrics端點(diǎn)
為了收集自定義的metrics,Actuator支持單數(shù)值記錄的功能,簡單的增加/減少計(jì)數(shù)功能。如下的實(shí)現(xiàn),我們將登錄成功和失敗的次數(shù)作為自定義指標(biāo)記錄下來。
@Service public class LoginServiceImpl implements LoginService {private final CounterService counterService;@Autowiredpublic LoginServiceImpl(CounterService counterService) {this.counterService = counterService;}@Overridepublic Boolean login(String userName, char[] password) {boolean success;if (userName.equals("admin") && "secret".toCharArray().equals(password)) {counterService.increment("counter.login.success");success = true;} else {counterService.increment("counter.login.failure");success = false;}return success;} }
再次訪問/metrics,發(fā)現(xiàn)多了如下的指標(biāo)信息。登錄嘗試和其他安全相關(guān)事件在Actuator中可用作審計(jì)事件。
{
"gauge.response.metrics": 2.0,
"gauge.response.test": 3.0,
"gauge.response.star-star.favicon.ico": 1.0,
"counter.status.200.star-star.favicon.ico": 10,
"counter.status.200.test": 6,
"counter.login.failure": 6,
"counter.status.200.metrics": 4
}
自定義端點(diǎn)
除了使用Spring Boot Actuator提供的端點(diǎn),我們也可以定義一個(gè)全新的端點(diǎn)。
首先,我們需要實(shí)現(xiàn)Endpoint
接口:
@Component public class CustomEndpoint implements Endpoint<List<String>> {@Overridepublic String getId() {return "custom";}@Overridepublic boolean isEnabled() {return true;}@Overridepublic boolean isSensitive() {return false;}@Overridepublic List<String> invoke() {// Custom logic to build the outputList<String> messages = new ArrayList<String>();messages.add("This is message 1");messages.add("This is message 2");return messages;} }
getId()
方法用于匹配訪問這個(gè)端點(diǎn),當(dāng)我們?cè)L問/custom時(shí),將會(huì)調(diào)用invoke()
我們自定義的邏輯。 另外兩個(gè)方法,用于設(shè)置是否開啟和是否為敏感的端點(diǎn)。
[ "This is message 1", "This is message 2" ]
進(jìn)一步定制
出于安全考慮,我們可能選擇通過非標(biāo)準(zhǔn)端口 暴露Actuator端點(diǎn)。通過management.port屬性來配置它。
另外,正如我們已經(jīng)提到的那樣,在1.x. Actuator基于Spring Security配置自己的安全模型,但獨(dú)立于應(yīng)用程序的其余部分。
因此,我們可以更改management.address屬性以限制可以通過網(wǎng)絡(luò)訪問端點(diǎn)的位置:
#port used to expose actuator
management.port=8081
#CIDR allowed to hit actuator
management.address=127.0.0.1
此外,除了/info端點(diǎn),其他所有的端點(diǎn)默認(rèn)都是敏感的,如果引入了Spring Security,我們通過在配置文件中定義這些安全的屬性(username, password, role)來確保內(nèi)置端點(diǎn)的安全。
到此這篇關(guān)于深入講解SpringBoot Actuator是什么的文章就介紹到這了,更多相關(guān)SpringBoot Actuator內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)字符串和日期類型相互轉(zhuǎn)換的方法
這篇文章主要介紹了java實(shí)現(xiàn)字符串和日期類型相互轉(zhuǎn)換的方法,涉及java針對(duì)日期與字符串的轉(zhuǎn)換與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-02-02IDEA maven compile報(bào)錯(cuò)OutOfMemoryError(內(nèi)存溢出)解決及jvm分析
遇到Maven編譯時(shí)報(bào)OutOfMemoryError錯(cuò)誤通常因?yàn)槟J(rèn)的堆內(nèi)存大小不足,本文就來介紹一下OutOfMemoryError(內(nèi)存溢出)解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-10-10SpringCloud OpenFeign自定義結(jié)果解碼器方式
這篇文章主要介紹了SpringCloud OpenFeign自定義結(jié)果解碼器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示功能(詳細(xì)步驟)
這篇文章主要介紹了SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示,通過數(shù)據(jù)庫設(shè)計(jì)、實(shí)現(xiàn)數(shù)據(jù)訪問層、業(yè)務(wù)邏輯層和控制層的代碼編寫,以及前端頁面的開發(fā),本文詳細(xì)地介紹了SpringBoot整合Echarts的實(shí)現(xiàn)步驟和代碼,需要的朋友可以參考下2023-05-05Spring容器的創(chuàng)建過程之如何注冊(cè)BeanPostProcessor詳解
關(guān)于BeanPostProcessor 各位一定不陌生,今天整理的這篇文章總結(jié)了如何注冊(cè)BeanPostProcessor,文中有非常詳細(xì)的圖文示例,需要的朋友可以參考下2021-06-06