【问题标题】:spring boot actuator endpoint mapping root classspring boot 执行器端点映射根类
【发布时间】:2017-02-07 13:21:10
【问题描述】:

在春天我们可以设计如下的REST Web服务。

@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 boot 执行器源代码。如果我们将在我们的应用程序中使用 Spring Boot 执行器,它将为我们提供一些端点,这些端点已作为其他 API 公开,例如健康、指标、信息。所以在我的应用程序中,如果我使用弹簧启动执行器,当我点击“localhost:8080/health”之类的 url 时,我会得到响应。

所以现在我的问题是在映射此 URL 的 Spring Boot 执行器源代码中。我调试了spring boot actuator的源代码,但是找不到端点映射的根类。

有人可以帮忙吗?

【问题讨论】:

    标签: java spring spring-boot spring-boot-actuator


    【解决方案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”的地方。

    【讨论】:

      【解决方案2】:

      所有 spring-boot-actuator 端点都扩展了 AbstractEndpoint(在 Health 端点情况下,例如:class HealthEndpoint extends AbstractEndpoint<Health>),其 contrucor 具有 Endpoint 的 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;
      

      否则,它有一个调用方法(来自接口端点),通过它被调用端点。

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

      最后,这个端点在EndpointAutoConfiguration 类中配置为Bean

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

      看看这篇解释如何自定义端点的帖子:

      【讨论】:

        猜你喜欢
        • 2018-12-06
        • 1970-01-01
        • 1970-01-01
        • 2018-03-03
        • 2016-08-26
        • 1970-01-01
        • 2020-01-10
        • 2015-09-28
        • 1970-01-01
        相关资源
        最近更新 更多