一、引入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