如何在 spring boot health 中添加自定义健康检查?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44849568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a custom health check in spring boot health?
提问by shabinjo
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
This will add several useful endpoints to your application. One of them is /health. When you start your application and navigate to the /health endpoint you will see it returns already some data.
这将为您的应用程序添加几个有用的端点。其中之一是/health。当您启动应用程序并导航到 /health 端点时,您会看到它已经返回了一些数据。
{
"status":"UP",
"diskSpace": {
"status":"UP",
"free":56443746,
"threshold":1345660
}
}
How to add a custom health check in spring boot health?
如何在 spring boot health 中添加自定义健康检查?
回答by shabinjo
Adding a custom health check is easy. Just create a new Java class, extend it from the AbstractHealthIndicator and implement the doHealthCheck method. The method gets a builder passed with some useful methods. Call builder.up() if your health is OK or builder.down() if it is not. What you do to check the health is completely up to you. Maybe you want to ping some server or check some files.
添加自定义运行状况检查很容易。只需创建一个新的 Java 类,从 AbstractHealthIndicator 扩展它并实现 doHealthCheck 方法。该方法获取一个带有一些有用方法的构建器。如果您的健康状况良好,则调用 builder.up(),否则调用 builder.down()。您如何检查健康完全取决于您。也许你想 ping 一些服务器或检查一些文件。
@Component
public class CustomHealthCheck extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder bldr) throws Exception {
// TODO implement some check
boolean running = true;
if (running) {
bldr.up();
} else {
bldr.down();
}
}
}
This is enough to activate the new health check (make sure @ComponentScan is on your application). Restart your application and locate your browser to the /health endpoint and you will see the newly added health check.
这足以激活新的健康检查(确保 @ComponentScan 在您的应用程序上)。重新启动您的应用程序并将您的浏览器定位到 /health 端点,您将看到新添加的健康检查。
{
"status":"UP",
"CustomHealthCheck": {
"status":"UP"
},
"diskSpace": {
"status":"UP",
"free":56443746,
"threshold":1345660
}
}
回答by Tobske
Since Spring Boot 2.X
从 Spring Boot 2.X 开始
As stated by @yuranos87 the actuator concept has changed in Spring Boot 2.X but you can still add custom health checkseasily by implementing HealthIndicatoror for reactive applications ReactiveHealthIndicator:
正如@yuranos87 所述,执行器概念在 Spring Boot 2.X 中发生了变化,但您仍然可以通过实现或响应式应用程序轻松添加自定义健康检查:HealthIndicatorReactiveHealthIndicator
@Component
public class CacheHealthIndicator implements HealthIndicator {
@Override
public Health health() {
long result = checkSomething();
if (result <= 0) {
return Health.down().withDetail("Something Result", result).build();
}
return Health.up().build();
}
}
or
或者
@Component
public class CacheHealthIndicator implements ReactiveHealthIndicator {
@Override
public Mono<Health> health() {
return Mono.fromCallable(() -> checkSomething())
.map(result -> {
if (result <= 0) {
return Health.down().withDetail("Something Result", result).build();
}
return Health.up().build();
});
}
}
Additionally you can add or extend any endpointwith @Endpointor @EndpointWebExtension. Endpoints here are info, healthand many more. So you can add custom health check by using @Endpointbut it is much easier to do with HealthIndicator.
此外,您可以使用或来添加或扩展任何端点。这里的端点是,还有更多。因此,您可以使用添加自定义运行状况检查,但使用.@Endpoint@EndpointWebExtensioninfohealth@EndpointHealthIndicator
You can find more information about custom health checksand custom endpointsin the spring boot documentation.
回答by yuranos
Spring Boot 2.X has significantly changed actuator. A new, better mechanism to extend existing endpoints is enabled via @EndpointWebExtension.
Spring Boot 2.X 显着改变了执行器。通过@EndpointWebExtension.
That being said, health endpoint is a bit trickier to extend because one extension for it is provided out of the box by actuator itself. Without manipulating beans initialization process, your application will not be able to start since it will see 2 extensions and will not understand which one to choose. An easier way would be to use info instead and extend it:
话虽如此,健康端点的扩展有点棘手,因为它的一个扩展是由执行器本身提供的。如果不操作 beans 初始化过程,您的应用程序将无法启动,因为它将看到 2 个扩展并且不知道选择哪一个。一种更简单的方法是使用 info 并扩展它:
@Component
@EndpointWebExtension(endpoint = InfoEndpoint.class)
public class InfoWebEndpointExtension {
@Value("${info.build.version}")
private String versionNumber;
@Value("${git.commit.id}")
private String gitCommit;
@Value("${info.build.name}")
private String applicationName;
...
@ReadOperation
public WebEndpointResponse<Map> info() {
Don't forget that you can also re-map URLs. In my case I prefer /statusto /healthand don't want /actuator/in the path:
不要忘记您还可以重新映射 URL。在我的情况下,我更喜欢/status到/health并且不希望/actuator/在路径中:
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.info=status
Another reason why I prefer /infois because I don't get this nested structure, which is default for /health:
我更喜欢/info 的另一个原因是因为我没有得到这个嵌套结构,这是/health 的默认值:
{
"status": {
"status": "ON",

