【问题标题】:Not able to exclude /info and /health/{*path} when using Spring Actuator使用 Spring Actuator 时无法排除 /info 和 /health/{*path}
【发布时间】:2020-03-06 14:51:06
【问题描述】:

我正在使用 Spring Actuator(版本 2.2.4.RELEASE)在 /localhost:8080/my-app/actuator/health 处生成运行正常的健康检查端点。

这会生成 3 个端点,在访问 /actuator 时显示并在 Swagger(版本 2)中显示:

  1. /actuator/health
  2. /actuator/health/{*path}(在我的招摇页面中,这显示为/actuator/health/**)
  3. /actuator/info

由于 AWS 的原因,我在使用 health/** 时遇到了问题,我想删除它(我也想删除 /info,因为我不需要它)。

我已尝试将以下内容添加到我的 application.properties 文件中:

management.endpoints.web.exposure.exclude=health,info

和

management.endpoints.jmx.exposure.exclude=health,info

但它没有任何区别(它们仍然会生成)。我尝试使用* 来查看是否会强制所有端点消失,但它也不会改变任何东西。

知道如何解决这个问题吗?

编辑 1

我发现一个属性文件被另一个文件覆盖。因此,使用以下命令:

management.endpoints.enabled-by-default=false
management.endpoint.health.enabled=true

摆脱/actuator/info 端点。但是,我仍然需要摆脱 /actuator/health/{*path} 并保留 /actuator/health 端点。

【问题讨论】:

  • 在我的情况下,management.endpoints.web.exposure.exclude=health,info 禁用了 Web 端点和 swagger 条目。您是否确认没有更高优先级的外部配置覆盖您的application.properties?对于所有财产来源,请查看docs.spring.io/spring-boot/docs/current/reference/html/…
  • @Lesiak 我已经解决了覆盖属性的问题。但是,仍然存在一个问题。我已经详细编辑了我的答案。

标签: spring-boot swagger spring-boot-actuator


【解决方案1】:

如执行器手册的Exposing Endpoints 部分所述,Web 默认公开两个端点:health 和info。

正如您正确注意到的那样,可以使用以下方法进行修改:

  • management.endpoints.web.exposure.exclude
  • management.endpoints.web.exposure.include

属性(排除具有更高优先级)。

因此,您可以轻松摆脱info 端点。

现在,健康端点提供 2 个 URL:

  • /actuator/health
  • /actuator/health/{*path}

我不清楚你离开前者并禁用后者的动机是什么,但我检查过你至少有两个选择:

选项 1 - 将 health 替换为您自己的实现

你只需要:

  • 排除 HealthEndpointAutoConfiguration 以删除默认运行状况端点
  • 提供您自己的custom actuator endpoint 映射到health

选项 2:保留 /actuator/health 但删除 /actuator/health/{*path}

这两个操作都定义在org.springframework.boot.actuate.health.HealthEndpoint

@Endpoint(id = "health")
public class HealthEndpoint extends HealthEndpointSupport<HealthContributor, HealthComponent> {

    // ...

    @ReadOperation
    public HealthComponent health() {
        HealthComponent health = health(ApiVersion.V3, EMPTY_PATH);
        return (health != null) ? health : DEFAULT_HEALTH;
    }

    @ReadOperation
    public HealthComponent healthForPath(@Selector(match = Match.ALL_REMAINING) String... path) {
        return health(ApiVersion.V3, path);
    }
}

在第二种方法中摆脱@ReadOperation的最简单方法是:

  • 将HealthEndpoint复制到您的项目中(注意:包必须匹配)
  • 删除healthForPath上的@ReadOperation注解
  • 复制HealthEndpointSupport,防止IllegalAccessError由不同的类加载器引起。

【讨论】:

    【解决方案2】:

    公开 Swagger UI 的推荐方法是仅启用特定路径。这是一个示例,您可以使用它来仅公开 /rest/* 端点。您可以将其替换为您需要的任何模式。

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Give a meaningful Title")
            .description("Describe your API")
            .termsOfServiceUrl("Put your T&C URL")
            .contact("Contact details")
            .license("Licence Description")
            .licenseUrl("Licence Endpoint")
            .version("API Version")
            .build();
    }
    private Predicate<String> paths() {
        return or(regex("/rest/.*"));
    }
    
    @Bean
    public Docket newsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("rest")
            .apiInfo(apiInfo())
            .select()
            .paths(paths())
            .build();
    }
    
    

    此示例仅允许您要公开的端点。

    参见https://github.com/reflexdemon/shop/blob/master/src/main/java/org/shop/Application.java#L85-L107 示例实现。

    查看端点的演示:https://shop.vpv.io/swagger-ui.html 使用登录 用户名 stack 密码 overflow 查看招摇。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-27
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多