Spring-boot 添加自定义健康检测 /actuator/health

一、如何添加自己的一个健康检测项到检测体系里


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

/**
 *  添加一个自定义的健康监测
 * Author: zhangjianshou 
* Description: * @Date: 2022/12/28 17:25 */ @Component public class LocalDiyHealth implements HealthIndicator { private static final Logger log = LoggerFactory.getLogger(LocalDiyHealth.class); /** * Description: 自定义健康监测接口,http://127.0.0.1/actuator/health,请求时候,会触发本方法 * @author zhangjianshou * @param includeDetails * @return: * @Exception:
* @Date: 2022/12/28 17:38 */ @Override public Health getHealth(boolean includeDetails) { log.debug("正在检查health..."); Health.Builder up = Health.up().withDetail("我是健康的", "up up"); try { /*if (up != null) { throw new Exception("test down"); }*/ return up.build(); } catch (Exception e) { log.error("检查异常", e); return Health.down().withException(e).withDetail("我是不健康的", "down down").build(); } } @Override public Health health() { log.debug("正在检查health..."); Health.Builder up = Health.up().withDetail("localDiyHealth", "up"); try { return up.build(); } catch (Exception e) { log.error("检查异常"); return up.withException(e).build(); } } }

二、如果开发了一个组件,想让用户引入组件后,让spring自动检测该组件,下面以nacos为例

  1. 组件代码里添加定义选项
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;

public class DiyNacosConfigHealthIndicator extends AbstractHealthIndicator {

    private final ConfigService configService;

    public DiyNacosConfigHealthIndicator(ConfigService configService) {
        this.configService = configService;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        // Just return "UP" or "DOWN"
        String status = configService.getServerStatus();
        // Set the status to Builder
        builder.status(status);
        switch (status) {
        case "UP":
            builder.up();
            break;
        case "DOWN":
            builder.down();
            break;
        default:
            builder.unknown();
            break;
        }
    }

}
  1. 注册健康检测选项
 
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.dhgate.zjs.health.indicator.DiyNacosConfigHealthIndicator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;

/**
 * @author xiaojing
 */
@ConditionalOnWebApplication
@ConditionalOnClass(Endpoint.class)
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
public class NacosConfigEndpointAutoConfiguration {

    @Autowired
    private NacosConfigManager nacosConfigManager;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledHealthIndicator("nacos-config")
    public DiyNacosConfigHealthIndicator nacosConfigHealthIndicator() {
        return new DiyNacosConfigHealthIndicator(nacosConfigManager.getConfigService());
    }

}
  1. 启动工程请求地址:http://127.0.0.1:8081/actuator/health

就会出现如下

{"status":"UP","components":{"diskSpace":{"status":"UP","details":{"total":"500106792960","free":"215939887104","threshold":"10485760","exists":true}},"nacos":{"status":"UP"}}}

你可能感兴趣的:(java,spring,spring,cloud)