【问题标题】:SpringBoot 2.2.4 Actuator - path for custom management endpointsSpring Boot 2.2.4 Actuator - 自定义管理端点的路径
【发布时间】:2020-06-01 10:12:00
【问题描述】:

从 spring-boot v1.3 迁移到最新的 spring-boot v2.2.4 后,我们失去了在管理端口下拥有自定义端点的能力。

在我们将自定义端点声明为:

@Component
public class CacheEndpoint implements MvcEndpoint {
 ...
   @Override
  public String getPath() {
    return "/v1/cache";
  }
  ...
  // mappings goes here

由于 MvcEndpoint 已从 spring-boot 执行器中删除,现在我们需要做下一步:

@Component
@RestControllerEndpoint(id = "cache")
public class CacheEndpoint {
    ...
    // mappings goes here

不幸的是,我们失去了为自定义管理端点提供自定义根路径的选项(之前是 /v1/

为了向后兼容,我们仍然希望在/ 基本路径下有默认的执行器端点,例如healthmetricsenv..。例如host:<management_port>/health,但同时我们仍然希望在 /v1/ 路径下支持我们的自定义端点,例如host:<management_port>/v1/cache

我尝试了很多东西,googled 甚至更多,但还没有成功。 有没有办法做到这一点?

【问题讨论】:

    标签: spring-boot endpoint spring-boot-actuator


    【解决方案1】:

    这是我用于 Spring Boot 2 的:

    application.yml:

    
    management:
      endpoints:
        enabled-by-default: true
        web:
          exposure:
            include: "*"
          base-path: "/management" # <-- note, here is the context path
    

    总而言之,考虑为从 spring boot 1.x 到 2.x 的执行器读取 migration guide

    【讨论】:

    • 这可行,但适用于所有端点,包括执行器的默认值。例如。 /health, /metrics 变成 /manage/health, /manage/metrics 等等。但是我们需要为默认执行器的端点保存 base-path=/ 并为自定义端点应用另一个基本路径。
    • 我不记得我在 spring boot 1.x 中需要这样的东西,但你为什么不放一个路径 /actuator/custom/path/my-custom-endpoint 呢?总而言之,我相信执行器的所有端点都应该在同一个根路径下
    • 这是一个向后兼容的原因。当然,我们可以将所有东西都移到一条路径下,但是因此需要重新配置很多东西。在边缘情况下,我们会这样做,但如果有办法在应用程序级别进行配置 - 最好不要重新配置许多第三方。
    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 2015-02-22
    • 2023-03-10
    • 2018-01-08
    • 1970-01-01
    • 2021-08-10
    • 2018-03-26
    • 2016-06-01
    相关资源
    最近更新 更多