Spring Boot 注解方式自定義Endpoint詳解
概述
在使用Spring Boot的時(shí)候我們經(jīng)常使用actuator,健康檢查,bus中使用/refresh等。這里記錄如何使用注解的方式自定義Endpoint。可用于滿足一些服務(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
啟動&測試
啟動后可以看到日志:
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會去掃描@EndPoint注解下的@ReadOperation, @WriteOperation, @DeleteOperation注解,分別對應(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)
管理:比如說通過Actuator去做一個(gè)shutdown功能,通過訪問一個(gè)特定的url去操作,默認(rèn)是不開啟的,另外 還可以在運(yùn)行的過程中 對日志進(jìn)行調(diào)整
訪問方式
- HTTP
- JMX
默認(rèn) 會把更多的 Actuator 暴露在JMX上面
依賴
- spring-boot-starter-actuator
一些常用 Endpoint

只有health和info 默認(rèn)是可以通過http 進(jìn)行訪問的

shutdown是默認(rèn)不開啟的一個(gè)threaddump去看線程情況Prometheus雖然是默認(rèn)開啟 但是 必須在pom文件上加入相關(guān)依賴 才能提供支持
如何訪問 Actuator Endpoint
HTTP 訪問
- /actuator/<id >
端口與路徑
- management.server.address=
Actuator Endpoint 發(fā)布的地址
- management.server.port=
Actuator Endpoint 發(fā)布的端口號
- management.endpoints.web.base-path=/actuator
自定義端口
- management.endpoints.web.path-mapping.<id>=路徑
修改端點(diǎn)的訪問路徑(映射),端點(diǎn)默認(rèn)使用的是它的 值
開啟 Endpoint
- management.endpoint.<id>.enabled=true
開啟名為id的 Endpoint
- management.endpoints.enabled-by-default=false
端點(diǎn)啟用是選擇性加入而不是選擇性排除。表示禁用了所有端點(diǎn)
暴露 Endpoint
- management.endpoints.jmx.exposure.exclude=
jmx方式排除需要公開的端點(diǎn)
- management.endpoints.jmx.exposure.include=*
jmx方式包含需要公開的端點(diǎn)
- management.endpoints.web.exposure.exclude=
http方式排除需要公開的端點(diǎn)
- management.endpoints.web.exposure.include=info, health
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在Java項(xiàng)目中實(shí)現(xiàn)日志輸出的技巧分享
日志是開發(fā)過程中不可或缺的一部分,它可以幫助我們追蹤代碼的執(zhí)行過程、排查問題以及監(jiān)控系統(tǒng)運(yùn)行狀況,然而,大多數(shù)開發(fā)人員在編寫日志時(shí)往往只關(guān)注于輸出必要的信息,而忽略了日志的可讀性和美觀性,本文將介紹如何在Java項(xiàng)目中實(shí)現(xiàn)漂亮的日志輸出2023-10-10
SpringBoot簡單實(shí)現(xiàn)定時(shí)器過程
這篇文章主要介紹了SpringBoot簡單實(shí)現(xiàn)定時(shí)器過程,對于Java后端來說肯定實(shí)現(xiàn)定時(shí)功能肯定是使用到Spring封裝好的定時(shí)調(diào)度Scheduled2023-04-04
JAVA WEB中Servlet和Servlet容器的區(qū)別
這篇文章主要介紹了JAVA WEB中Servlet和Servlet容器的區(qū)別,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
有關(guān)ServletConfig與ServletContext的訪問
下面小編就為大家?guī)硪黄嘘P(guān)ServletConfig與ServletContext的訪問。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
SpringBoot集成Aviator實(shí)現(xiàn)參數(shù)校驗(yàn)的代碼工程
Aviator是一個(gè)高性能、輕量級的java語言實(shí)現(xiàn)的表達(dá)式求值引擎,主要用于各種表達(dá)式的動態(tài)求值,本文給大家詳細(xì)介紹了SpringBoot集成Aviator實(shí)現(xiàn)參數(shù)校驗(yàn)的方法,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-11-11

