Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

26.4. 健康状态

curl www.netkiller.cn:8080/actuator/health

				
neo@MacBook-Pro ~ % curl -s http://www.netkiller.cn:8080/actuator/health | jq
{
  "status": "UP"
}
		
		

26.4.1. 健康状态

详细的健康状态信息

			
management.endpoint.health.show-details=always			
			
			

			
neo@MacBook-Pro ~ % curl -s http://www.netkiller.cn:8080/actuator/health | jq
{
  "status": "UP",
  "details": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 250790436864,
        "free": 23556112384,
        "threshold": 10485760
      }
    }
  }
}			
			
			

26.4.2. 自定义健康检查

方案一
				
package cn.aigcsst.config;

import org.springframework.boot.health.contributor.AbstractHealthIndicator;
import org.springframework.boot.health.contributor.Health;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        builder.up().withDetail("netkiller", "OK");
//        builder.down().withDetail("Item", "xxx").withDetail("error", "xxxErrorCode");
    }
}
				
				
方案二
			
package cn.aigcsst.config;

import org.jspecify.annotations.Nullable;
import org.springframework.boot.health.contributor.Health;
import org.springframework.boot.health.contributor.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {

    @Override
    public @Nullable Health health(boolean includeDetails) {
        return HealthIndicator.super.health(includeDetails);
    }

    @Override
    public @Nullable Health health() {
//        return null;
        // 执行自定义检查逻辑
        if (false) {
            // 健康状态为UP,可添加额外的详细信息
            return Health.up()
                    .withDetail("message", "订单服务连接正常")
                    .withDetail("version", "v1.0.0")
                    .withDetail("timestamp", System.currentTimeMillis())
                    .build();
        } else {
            // 健康状态为DOWN,添加错误信息
            return Health.down()
                    .withDetail("message", "订单服务连接失败")
                    .withDetail("error", "连接超时/服务未启动")
                    .build();
        }
    }
}