简体   繁体   English

Spring Boot Actuator - 自定义健康端点

[英]Spring Boot Actuator - Custom Health Endpoint

I am using SpringBoot Actuator to return health of the app.我正在使用 SpringBoot Actuator 来返回应用程序的运行状况。

public class HealthMonitor implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check();
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

    public int check() {
        return 0;
    }

}

I see the below response我看到下面的回复

{
  "status": "UP",
  "diskSpace": {
    "status": "UP",
    "free": 55020113920,
    "threshold": 10485760
  },
  "db": {
    "status": "UP",
    "database": "Oracle",
    "hello": "Hello"
  }
}

I want to return a response similar to below我想返回类似于下面的响应

{status: "Healthy"}

Is there a way to do it?有没有办法做到这一点?

To customize your Status message there is one method named: withDetail() .要自定义您的状态消息,有一种名为: withDetail() So when you are writing return Health.up().build();所以当你写的时候return Health.up().build(); just replace this with只需将其替换为

return Health.up().withDetail("My Application Status","Healthy")

So this will give所以这会给

{My Application Status: "Healthy"}

I don't think you're going to be able to persuade the supplied Spring Boot health endpoint to display in a dramatically different format.我认为您无法说服提供的 Spring Boot 运行状况端点以截然不同的格式显示。

Most of the Spring source code is very clear, and this is no exception.大多数Spring源代码都非常清楚,这次也不例外。 Look at the source for org.springframework.boot.actuate.endpoint.HealthEndpoint and you'll see it has an invoke() method that returns a Health .查看org.springframework.boot.actuate.endpoint.HealthEndpoint的源代码,您会看到它有一个返回Healthinvoke()方法。 The JSON you are seeing is Jackson's marshalling of that object.您看到的 JSON 是 Jackson 对该对象的编组。

You could, however, create your own endpoint, extending AbstractEndpoint in exactly the same way as HealthEndpoint does, returning whatever class of your own you like.但是,您可以创建自己的端点,以与HealthEndpoint完全相同的方式扩展AbstractEndpoint ,返回您喜欢的任何类。

You could even make it delegate to the HealthEndpoint bean:您甚至可以将其委托给HealthEndpoint bean:

 public MyHealthEndpoint(HealthEndpoint delegate) {
     this.delegate = delegate;
 }

 @Override
 public MyHealth invoke() {
      Health health = delegate.invoke();
      return new MyHealth(health.getStatus());
 }

There is a lot of flexibility with Spring Boot and its health indicators. Spring Boot 及其健康指标具有很大的灵活性。

First, depending on the complexity of your service, you may not need the HealthIndicator you describe above.首先,根据您的服务的复杂程度,您可能不需要上面描述的 HealthIndicator。 Out of the box, Spring Boot will return status' based upon your spring configurations.开箱即用,Spring Boot 将根据您的弹簧配置返回状态。 This includes databases (both SQL and some noSQL), Mail, JMS and others.这包括数据库(SQL 和一些 noSQL)、Mail、JMS 等。 This is described in the latest Spring Boot documents in section 47.6.1这在最新的 Spring Boot 文档的第 47.6.1 节中有所描述

The easiest way to return what you want, with or without the HealthIndicator class, is to set a property in your application.properties:无论是否使用 HealthIndicator 类,返回您想要的内容的最简单方法是在 application.properties 中设置一个属性:

endpoints.health.sensitive=true endpoints.health.sensitive=true

This may only apply to the later versions (after 1.4?) of Spring Boot.这可能仅适用于 Spring Boot 的更高版本(1.4 之后?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM