您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# SpringBoot2怎么自定义端点
## 前言
在Spring Boot应用中,Actuator模块提供了丰富的生产级监控端点(如`/health`、`/metrics`)。但有时我们需要根据业务需求自定义端点。本文将详细介绍在Spring Boot 2.x中如何实现自定义端点。
---
## 一、基础概念
### 1.1 Actuator端点类型
Spring Boot Actuator端点分为两类:
- **原生端点**:框架内置(如`/info`)
- **自定义端点**:开发者扩展实现
### 1.2 端点暴露方式
在`application.properties`中配置:
```properties
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env
@Endpoint
注解<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
import org.springframework.boot.actuate.endpoint.annotation.*;
import java.util.Map;
import java.util.HashMap;
@Endpoint(id = "custom")
public class CustomEndpoint {
@ReadOperation
public Map<String, Object> getInfo() {
Map<String, Object> details = new HashMap<>();
details.put("status", "UP");
details.put("timestamp", System.currentTimeMillis());
return details;
}
@WriteOperation
public void triggerAction(@Selector String name) {
// 执行自定义操作
}
}
@Endpoint(id = "custom")
:定义端点ID,访问路径为/actuator/custom
@ReadOperation
:对应HTTP GET请求@WriteOperation
:对应HTTP POST请求@Selector
:路径参数捕获@WebEndpoint
扩展适用于需要完全控制HTTP行为的场景:
@WebEndpoint(id = "webendpoint")
public class WebCustomEndpoint {
@ReadOperation
public WebEndpointResponse<Map<String, Object>> webOperation() {
Map<String, Object> result = new HashMap<>();
result.put("message", "Web Endpoint Response");
return new WebEndpointResponse<>(result, 200);
}
}
@ControllerEndpoint
整合MVC与Spring MVC深度集成:
@ControllerEndpoint(id = "controllerend")
public class ControllerCustomEndpoint {
@GetMapping("/")
@ResponseBody
public String handleGet() {
return "Controller Endpoint Response";
}
}
@Configuration
public class EndpointSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests()
.anyRequest().hasRole("ADMIN");
}
}
management.endpoints.web.path-mapping.custom=/api/mycustom
@ReadOperation
public CachedResponse getCachedData() {
return new CachedResponse()
.withCache(CacheControl.maxAge(10, TimeUnit.MINUTES));
}
@Endpoint(id = "sysinfo")
public class SystemInfoEndpoint {
@ReadOperation
public SystemInfo systemInfo() {
Runtime runtime = Runtime.getRuntime();
return new SystemInfo(
Runtime.version().toString(),
runtime.availableProcessors(),
runtime.maxMemory() / 1024 / 1024 + "MB"
);
}
@Data
@AllArgsConstructor
private static class SystemInfo {
private String jvmVersion;
private int processors;
private String maxMemory;
}
}
GET /actuator/sysinfo
Response:
{
"jvmVersion": "17.0.1",
"processors": 8,
"maxMemory": "4096MB"
}
user-stats
)通过本文我们学习了三种自定义端点的方式,并探讨了安全、缓存等高级特性。合理使用自定义端点可以极大增强应用的可观测性。建议结合Prometheus和Grafana等工具构建完整的监控体系。
官方参考文档:
Spring Boot Actuator Web API Documentation “`
文章共计约1100字,包含代码示例、配置说明和实战案例,采用标准的Markdown格式。可根据需要调整代码示例中的细节或补充更多配置场景。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。