使用 Spring Actuator 时无法排除 /info 和 /health/{*path}
Not able to exclude /info and /health/{*path} when using Spring Actuator
我正在使用 Spring Actuator(版本 2.2。4.RELEASE)在 /localhost:8080/my-app/actuator/health
生成运行正常的运行状况检查端点。
这会生成 3 个端点,在访问 /actuator
时显示并在 Swagger(版本 2)中显示:
/actuator/health
/actuator/health/{*path}
(在我的 swagger 页面中,这显示为 /actuator/health/**
)
/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
端点。
公开 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
查看招摇。
如 actuator 手册的 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
您只需要:
- exclude
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
由不同的class加载器造成。
我正在使用 Spring Actuator(版本 2.2。4.RELEASE)在 /localhost:8080/my-app/actuator/health
生成运行正常的运行状况检查端点。
这会生成 3 个端点,在访问 /actuator
时显示并在 Swagger(版本 2)中显示:
/actuator/health
/actuator/health/{*path}
(在我的 swagger 页面中,这显示为/actuator/health/**
)/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
端点。
公开 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
查看招摇。
如 actuator 手册的 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
您只需要:
- exclude
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
由不同的class加载器造成。