SpringBoot Actuator端点的实现原理【概括整理】
SpringBoot Actuator端点的实现原理【概括整理】
版本:spring-boot-actuator-2.2.6.RELEASE
概括起来,SpringBoot Actuator的实现原理如下
1,Endpoint Bean
@Endpoint标注的class是一个bean,是SpringBoot Actuator功能的具体的实现。 比如/actuator/beans端点对应的实现类是BeansEndpoint.class
2,注入Endpoint Bean到IOC容器。
如何把BeansEndpoint.class注入IOC容器呢,是通过配置类EndpointAutoConfiguration,比如BeansEndpoint.class对应的自动配置类是BeansEndpointAutoConfiguration
3,根据Endpoint Bean创建HandlerMapping,并注入HandlerMapping集合。
OK,功能实现了,如何暴露端口给第三方呢?注入HandlerMapping即可。 如何把EndpointHandlerMapping注入SpringBoot的HandlerMapping呢,
具体的处理类是AbstractWebMvcEndpointHandlerMapping,initHandlerMethods方法会把所有标注有@Endpoint注解的class,封装成符合规范的结构,注入到HandlerMapping集合。
4,处理端点请求
处理请求的具体实现AbstractWebMvcEndpointHandlerMapping.OperationHandler。
最后要注意的是,SpringBoot Actuator不存在controller,而是通过@Endpoint注解的class来处理Endpoint端点请求的。 @Endpoint注解的class相当于我们的controller
SpringBoot Actuator端点请求的具体处理类:OperationHandler
private final class OperationHandler {
private final AbstractWebMvcEndpointHandlerMapping.ServletWebOperation operation;
OperationHandler(AbstractWebMvcEndpointHandlerMapping.ServletWebOperation operation) {
this.operation = operation;
}
@ResponseBody
Object handle(HttpServletRequest request, @RequestBody(required = false) Map<String, String> body) {
return this.operation.handle(request, body);
}
public String toString() {
return this.operation.toString();
}
}
SpringBoot Actuator端点请求封装的HandlerMapping的json数据结构如下
{
"handler": "Actuator web endpoint 'env'",
"predicate": "{GET /actuator/env, produces [application/vnd.spring-boot.actuator.v3+json || application/vnd.spring-boot.actuator.v2+json || application/json]}",
"details": {
"handlerMethod": {
"className": "org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping.OperationHandler",
"name": "handle",
"descriptor": "(Ljavax/servlet/http/HttpServletRequest;Ljava/util/Map;)Ljava/lang/Object;"
},
"requestMappingConditions": {
"consumes": [],
"headers": [],
"methods": [
"GET"
],
"params": [],
"patterns": [
"/actuator/env"
],
"produces": [
{
"mediaType": "application/vnd.spring-boot.actuator.v3+json",
"negated": false
},
{
"mediaType": "application/vnd.spring-boot.actuator.v2+json",
"negated": false
},
{
"mediaType": "application/json",
"negated": false
}
]
}
}
}
我们正常的controller接口封装的HandlerMapping的结构是这样的,可以对比一下异同
{
"handler": "com.cr949.auto.docs.controller.CommonController#commonResponseSimpleObjectTest(JavaBaisicTypeDto)",
"predicate": "{GET /cr949/interface/common/commonResponseSimpleObjectTest}",
"details": {
"handlerMethod": {
"className": "com.cr949.auto.docs.controller.CommonController",
"name": "commonResponseSimpleObjectTest",
"descriptor": "(Lcom/cr949/auto/docs/dto/swagger2/JavaBaisicTypeDto;)Lcom/cr949/auto/docs/dto/CommonResponse;"
},
"requestMappingConditions": {
"consumes": [],
"headers": [],
"methods": [
"GET"
],
"params": [],
"patterns": [
"/cr949/interface/common/commonResponseSimpleObjectTest"
],
"produces": []
}
}
}
本文地址:https://blog.csdn.net/reggergdsg/article/details/110534264
上一篇: Redis:发布订阅模式的使用
下一篇: 三层神经网络