首页 文章

spring boot Actuator endpoints 映射根类

提问于
浏览
0

在 Spring 天,我们可以设计如下的休息网络服务 .

@RestController
public class HelloController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello");
        return "hello";
    }
}

当我们这样做时,@ RestController和@RequestMapping将在内部管理请求映射部分 . 因此,当我打到url即http://localhost:8080/hello时,它将指向printWelcome方法 .

我正在研究 spring 启动器 Actuator 源代码 . 如果我们将在我们的应用程序中使用spring boot Actuator ,它将为我们提供一些 endpoints ,这些 endpoints 已作为 Health ,指标,信息等其他API公开 . 所以在我的应用程序中,如果我使用 spring 启动 Actuator ,当我将按“localhost:8080 / health”这样的网址时,我会得到回应 .

所以现在我的问题是在 Spring 季启动 Actuator 源代码中,这些URL被映射 . 我已经调试了spring boot actuator的源代码,但是无法找到 endpoints 映射的根类 .

有人可以帮忙吗?

2 回答

  • 1

    here它是,在AbstractEndpoint它说

    /**
         * Endpoint identifier. With HTTP monitoring the identifier of the endpoint is mapped
         * to a URL (e.g. 'foo' is mapped to '/foo').
         */
    

    如果你看到HealthEndPoint它会扩展AbstractEndpoint并执行 super("health", false); ,那就是它映射到"localhost:8080/health"的地方 .

  • 1

    所有spring-boot-actuator endpoints 都扩展AbstractEndpoint(在 Health endpoints 情况下例如: class HealthEndpoint extends AbstractEndpoint<Health> ),其中construcor具有 endpoints 的id .

    /**
     * Endpoint identifier. With HTTP monitoring the identifier of the endpoint is mapped
     * to a URL (e.g. 'foo' is mapped to '/foo').
     */
    private String id;
    

    否则,它有一个invoke方法(从接口Endpoint)通过它调用 endpoints .

    /**
     * Called to invoke the endpoint.
     * @return the results of the invocation
     */
    T invoke();
    

    最后,此 endpoints 在类 EndpointAutoConfiguration 中配置为 Bean

    @Bean
    @ConditionalOnMissingBean
    public HealthEndpoint healthEndpoint() {
        return new HealthEndpoint(this.healthAggregator, this.healthIndicators);
    }
    

    看一下这篇文章,其中解释了如何自定义 endpoints :

相关问题