springboot的diskSpaceHealthIndicator

diskSpaceHealthIndicator

用來對(duì)磁盤空間的狀態(tài)進(jìn)行檢測(cè),這個(gè)非常有用,以免磁盤空間占用快100%導(dǎo)致服務(wù)異常。

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

    }

實(shí)例

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

}

默認(rèn)的閾值是通過DiskSpaceHealthIndicatorProperties來進(jìn)行設(shè)置的

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

}

默認(rèn)是剩余空間小于10M的時(shí)候,認(rèn)為不健康。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容