【问题标题】:Custom nested actuator endpoint自定义嵌套执行器端点
【发布时间】:2014-10-23 19:07:18
【问题描述】:

使用 Spring Boot 1.0,我能够自定义执行器端点,如下所示...

endpoints.beans.id=foo/springbeans

这将在 /foo/springbeans 处公开 spring beans 端点。但是,在最新的 Spring Boot 中这是不可能的,因为 AbstractEndpoint...

中有以下代码
@NotNull
@Pattern(regexp = "\\w+", message = "ID must only contains letters, numbers and '_'")
private String id;

我尝试使用下划线,但这只会暴露 /foo_springbeans 处的端点。这导致我尝试添加一个视图控制器,这样我至少可以重定向或转发到默认端点,但我也找不到一个简单的方法来做到这一点。如何配置端点或重定向?

【问题讨论】:

  • management.context-path 属性有用吗?在这种情况下,所有端点都将暴露在不同的路径下。
  • 另一个想法,BeanPostProcessor 听起来像一个解决方案吗?我检查了一下,它可以完成工作。但我不确定这是否是理想的方式。在 BeansEndpoint 设置的 bean 的 postProcessAfterInitialization 中,将 id 设置为“foo/springbeans”解决了这个问题。
  • 感谢您的回复,但也不是我想要的。我有移动其中一个执行器的业务需求,如果我移动整个管理上下文,则该位置对其他执行器没有意义。 BeanPostProcessor 不是我正在寻找的答案。我希望其中一位 Spring 开发人员能够回答为什么要删除此功能。看来我可能需要提交错误报告。

标签: spring spring-mvc spring-boot


【解决方案1】:

在打开 Spring Boot 问题并被告知按照 Rafal 的建议简单地移动整个管理上下文后,我能够实现我想要的,尽管代码比我想要的要多。我创建了一个自定义 MvcEndpoint 如下...

@Named
public class MyCustomHealthCheck extends EndpointMvcAdapter {
    private HealthEndpoint delegate;

    @Inject
    public MyCustomHealthCheck(HealthEndpoint delegate) {
        super(delegate);
        this.delegate = delegate;
    }

    @ResponseBody
    @RequestMapping(value = "/springbeans", method = GET)
    public Health foo() {
        return delegate.invoke();
    }
}

上面的代码在 HealthEndpoint 映射到的任何路径下创建 /springbeans 路径,这对于我的用例来说已经足够了。如果我希望它映射到一个完全独立的路径,我需要创建一个虚拟端点并将这个 MvcEndpoint 粘贴在它下面。

【讨论】:

    【解决方案2】:

    对于 Spring 1.x 以下属性应该可以帮助您:

    endpoints.beans.path: /foo/springbeans
    

    您可以将它与任何标准端点一起使用,如果您想将它与扩展 AbstractEndpoint 的自定义端点一起使用,那么您需要额外的注释:

    @ConfigurationProperties(prefix = "endpoints.customEndpoint")
    

    然后使用属性:

    endpoints.customEndpoint.path: /custom/endpoint

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-10
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多