springboot的diskSpaceHealthIndicator

diskSpaceHealthIndicator

用來對磁盤空間的狀態進行檢測,這個很是有用,以避免磁盤空間佔用快100%致使服務異常。java

@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();
        }

    }

實例spring

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

具體見org/springframework/boot/actuate/health/DiskSpaceHealthIndicator.javaide

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來進行設置的ui

@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的時候,認爲不健康。this

相關文章
相關標籤/搜索