springboot的diskSpaceHealthIndicator
博客专区 > go4it 的博客 > 博客详情
springboot的diskSpaceHealthIndicator
go4it 发表于1年前
springboot的diskSpaceHealthIndicator
  • 发表于 1年前
  • 阅读 16
  • 收藏 1
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

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

标签: Spring Boot
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 42
博文 612
码字总数 410717
×
go4it
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: