使用 Spring Actuator 时无法排除 /info 和 /health/{*path}

Hay*_*ych 5 swagger spring-boot spring-boot-actuator

我正在使用 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
Run Code Online (Sandbox Code Playgroud)

management.endpoints.jmx.exposure.exclude=health,info
Run Code Online (Sandbox Code Playgroud)

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

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

编辑1

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

management.endpoints.enabled-by-default=false
management.endpoint.health.enabled=true
Run Code Online (Sandbox Code Playgroud)

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

Les*_*iak 8

正如执行器手册的“暴露端点”部分中所指定的,Web 默认情况下公开两个端点:healthinfo

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

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

属性(不包括具有更高优先级的属性)。

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

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

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

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

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

您只需要:

  • 排除HealthEndpointAutoConfiguration以删除默认运行状况端点
  • 提供您自己的映射到的自定义执行器端点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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 复制HealthEndpoint到您的项目(注意:包必须匹配)
  • 删除@ReadOperation注释healthForPath
  • HealthEndpointSupport防止IllegalAccessError类加载器不同造成的复制。