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的时候,认为不健康。

转载于:https://my.oschina.net/go4it/blog/873057

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值