springboot的diskSpaceHealthIndicator

diskSpaceHealthIndicator

用来对磁盘空间的状态进行检测,这个非常有用,以免磁盘空间占用快100%导致服务异常。

    @Configuration
    @ConditionalOnEnabledHealthIndicator("diskspace")
    public static class DiskSpaceHealthIndicatorConfiguration {

        @Bean
        @ConditionalOnMissingBean(name = "diskSpaceHealthIndicator")
        public DiskSpaceHealthIndicator diskSpaceHealthIndicator(
                DiskSpaceHealthIndicatorProperties properties) {
            return new DiskSpaceHealthIndicator(properties);
        }

        @Bean
        public DiskSpaceHealthIndicatorProperties diskSpaceHealthIndicatorProperties() {
            return new DiskSpaceHealthIndicatorProperties();
        }

    }

实例

{
  "status": "UP",
  "diskSpace": {
    "status": "UP",
    "total": 120108089344,
    "free": 4064759808,
    "threshold": 10485760
  }
}

具体见org/springframework/boot/actuate/health/DiskSpaceHealthIndicator.java

public class DiskSpaceHealthIndicator extends AbstractHealthIndicator {

    private static final Log logger = LogFactory.getLog(DiskSpaceHealthIndicator.class);

    private final DiskSpaceHealthIndicatorProperties properties;

    /**
     * Create a new {@code DiskSpaceHealthIndicator}.
     * @param properties the disk space properties
     */
    public DiskSpaceHealthIndicator(DiskSpaceHealthIndicatorProperties properties) {
        this.properties = properties;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        File path = this.properties.getPath();
        long diskFreeInBytes = path.getFreeSpace();
        if (diskFreeInBytes >= this.properties.getThreshold()) {
            builder.up();
        }
        else {
            logger.warn(String.format(
                    "Free disk space below threshold. "
                            + "Available: %d bytes (threshold: %d bytes)",
                    diskFreeInBytes, this.properties.getThreshold()));
            builder.down();
        }
        builder.withDetail("total", path.getTotalSpace())
                .withDetail("free", diskFreeInBytes)
                .withDetail("threshold", this.properties.getThreshold());
    }

}

默认的阈值是通过DiskSpaceHealthIndicatorProperties来进行设置的

@ConfigurationProperties(prefix = "management.health.diskspace")
public class DiskSpaceHealthIndicatorProperties {

    private static final int MEGABYTES = 1024 * 1024;

    private static final int DEFAULT_THRESHOLD = 10 * MEGABYTES;

    /**
     * Path used to compute the available disk space.
     */
    private File path = new File(".");

    /**
     * Minimum disk space that should be available, in bytes.
     */
    private long threshold = DEFAULT_THRESHOLD;

    public File getPath() {
        return this.path;
    }

    public void setPath(File path) {
        Assert.isTrue(path.exists(), "Path '" + path + "' does not exist");
        Assert.isTrue(path.canRead(), "Path '" + path + "' cannot be read");
        this.path = path;
    }

    public long getThreshold() {
        return this.threshold;
    }

    public void setThreshold(long threshold) {
        Assert.isTrue(threshold >= 0, "threshold must be greater than 0");
        this.threshold = threshold;
    }

}

默认是剩余空间小于10M的时候,认为不健康。

你可能感兴趣的:(springboot的diskSpaceHealthIndicator)