一、引入Actuator
当我们在项目中引入spring-boot-starter-actuator的时候,我们可以通过如下方式调用,查看服务的信息:
localhost:8006/actuator/info
默认actuator只开启了info和health,如果想要使用其他功能,需要在配置中类似如下方式添加:
management.endpoints.web.exposure.include = *
management.endpoints.web.exposure.exclude = env,beans
二、源码分析
1、handlerMapping
在调用actuator接口的时候,如果使用webflux,Spring使用的是WebFuxEndPointHandlerMapping。
2、Handler
使用的Handler是AbstractWebFuxEndPointHandlerMapping中的ReadOperationHandler,其handle方法如下:
@ResponseBody
public Publisher<ResponseEntity<Object>> handle(ServerWebExchange exchange) {
return this.operation.handle(exchange, null);
}
3、Invoker
上面的this.operation中包含了invoker,使用的是ElasticSchedulerInvoker,在ElasticSchedulerInvoker类中有一个invoker参数,使用的是OperationInvoker invoker = operation::invoke; 而此invoke方法最后是调用到了AbstractDiscoveredOperation的invoke方法:
@Override
public Object invoke(InvocationContext context) {
return this.invoker.invoke(context);
}
其中this.invoker是ReactiveOperationInvoker,其invoke方法如下:
@Override
public Object invoke(InvocationContext context) {
validateRequiredParameters(context);
Method method = this.operationMethod.getMethod();
Object[] resolvedArguments = resolveArguments(context);
ReflectionUtils.makeAccessible(method);
return ReflectionUtils.invokeMethod(method, this.target, resolvedArguments);
}
最后是调用到了InfoEndpoint的Info方法。
三、方法总览
在spring-boot-actuator包中org.springframework.boot.actuate目录下,包含了所有的actuator,如下
当调用的时候,其里面的类中都有对应id,比如,我们调用/actuator/info,因为其类上有的EndPoint的id为info
@Endpoint(id = "info")
public class InfoEndpoint