Spring Boot 注解方式自定義Endpoint詳解
概述
在使用Spring Boot的時(shí)候我們經(jīng)常使用actuator,健康檢查,bus中使用/refresh等。這里記錄如何使用注解的方式自定義Endpoint??捎糜跐M足一些服務(wù)狀態(tài)監(jiān)控,或者優(yōu)雅停機(jī)等。
準(zhǔn)備
Spring Boot項(xiàng)目,pom中加入:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
編寫自定義Endpoint
@Configuration @Endpoint(id = "my-endpoint") public class MyEndpoint { @ReadOperation public Map<String, Object> endpoint() { Map<String, Object> map = new HashMap<>(16); map.put("message", "this is my endpoint"); return map; } }
配置
management.endpoints.web.exposure.include=my-endpoint
啟動(dòng)&測(cè)試
啟動(dòng)后可以看到日志:
Mapped "{[/actuator/my-endpoint],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>)
注意
- @EndPoint中的id不能使用駝峰法,需要以-分割
- @Spring Boot會(huì)去掃描@EndPoint注解下的@ReadOperation, @WriteOperation, @DeleteOperation注解,分別對(duì)應(yīng)生成Get/Post/Delete的Mapping。注解中有個(gè)produces參數(shù),可以指定media type, 如:application/json等。
Spring Boot 常用endpoint的使用
Actuator
目的
- 監(jiān)控并管理應(yīng)用程序
監(jiān)控:讓我們?nèi)グl(fā)現(xiàn)和了解程序的運(yùn)行狀況各種指標(biāo)
管理:比如說(shuō)通過(guò)Actuator去做一個(gè)shutdown功能,通過(guò)訪問(wèn)一個(gè)特定的url去操作,默認(rèn)是不開(kāi)啟的,另外 還可以在運(yùn)行的過(guò)程中 對(duì)日志進(jìn)行調(diào)整
訪問(wèn)方式
- HTTP
- JMX
默認(rèn) 會(huì)把更多的 Actuator 暴露在JMX上面
依賴
- spring-boot-starter-actuator
一些常用 Endpoint
只有health和info 默認(rèn)是可以通過(guò)http 進(jìn)行訪問(wèn)的
shutdown
是默認(rèn)不開(kāi)啟的一個(gè)threaddump
去看線程情況Prometheus
雖然是默認(rèn)開(kāi)啟 但是 必須在pom文件上加入相關(guān)依賴 才能提供支持
如何訪問(wèn) Actuator Endpoint
HTTP 訪問(wèn)
- /actuator/<id >
端口與路徑
- management.server.address=
Actuator Endpoint 發(fā)布的地址
- management.server.port=
Actuator Endpoint 發(fā)布的端口號(hào)
- management.endpoints.web.base-path=/actuator
自定義端口
- management.endpoints.web.path-mapping.<id>=路徑
修改端點(diǎn)的訪問(wèn)路徑(映射),端點(diǎn)默認(rèn)使用的是它的 值
開(kāi)啟 Endpoint
- management.endpoint.<id>.enabled=true
開(kāi)啟名為id的 Endpoint
- management.endpoints.enabled-by-default=false
端點(diǎn)啟用是選擇性加入而不是選擇性排除。表示禁用了所有端點(diǎn)
暴露 Endpoint
- management.endpoints.jmx.exposure.exclude=
jmx方式排除需要公開(kāi)的端點(diǎn)
- management.endpoints.jmx.exposure.include=*
jmx方式包含需要公開(kāi)的端點(diǎn)
- management.endpoints.web.exposure.exclude=
http方式排除需要公開(kāi)的端點(diǎn)
- management.endpoints.web.exposure.include=info, health
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決springcloud中Feign導(dǎo)入依賴為unknow的情況
這篇文章主要介紹了解決springcloud中Feign導(dǎo)入依賴為unknow的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot配置使Mybatis打印SQL執(zhí)行時(shí)的實(shí)際參數(shù)值操作
這篇文章主要介紹了SpringBoot配置使Mybatis打印SQL執(zhí)行時(shí)的實(shí)際參數(shù)值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12深入了解Java中的過(guò)濾器Filter和監(jiān)聽(tīng)器Listener
這篇文章主要為大家詳細(xì)介紹了Java中的過(guò)濾器Filter和監(jiān)聽(tīng)器Listener的使用以及二者的區(qū)別,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06Java實(shí)現(xiàn)LeetCode(報(bào)數(shù))
這篇文章主要介紹了Java實(shí)現(xiàn)LeetCode(報(bào)數(shù)),本文通過(guò)使用java實(shí)現(xiàn)leetcode的報(bào)數(shù)題目和實(shí)現(xiàn)思路分析,需要的朋友可以參考下2021-06-06JDBC連接數(shù)據(jù)庫(kù)步驟及基本操作示例詳解
這篇文章主要為大家介紹了JDBC連接數(shù)據(jù)庫(kù)步驟及基本操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11