Spring Boot Actuator自定义监控集成到/health接口实战

本文详细介绍了如何在SpringBoot应用中使用Actuator的HealthEndpoint进行自定义监控,包括HealthEndpointWebExtension的扩展、AbstractHealthIndicator的自定义健康指标创建,以及如何通过MonitorAutoConfiguration和自定义HealthCheck类集成到/health端点。

导言

Spring Boot Actuator 为应用带来了生产就绪的功能。有了 Actuator 后,监控应用程序、收集指标、了解流量或数据库状态就变得易如反掌。Actuator 主要公开应用的运行信息 - 健康状况、指标、信息、转储(dump)、环境等。它使用 HTTP 端点或 JMX Bean 与客户端进行交互。

HealthEndpointWebExtension

在这里插入图片描述
HealthEndpointWebExtension 是一个用于监控和管理应用程序健康状况的网络扩展。它提供了一个端点(/actuator/health),可以返回应用程序的健康状况信息,包括数据库、缓存、消息队列等组件的健康状态。

源码如下:

@EndpointWebExtension(endpoint = HealthEndpoint.class)
public class HealthEndpointWebExtension extends HealthEndpointSupport<HealthContributor, HealthComponent> {
   
   

	private static final String[] NO_PATH = {
   
   };

	/**
	 * 使用委托端点创建一个新的HealthEndpointWebExtension实例。
	 */
	@Deprecated
	public HealthEndpointWebExtension(HealthEndpoint delegate, HealthWebEndpointResponseMapper responseMapper) {
   
   
	}

	/**
	 *创建一个新的HealthEndpointWebExtension实例
	 */
	public HealthEndpointWebExtension(HealthContributorRegistry registry, HealthEndpointGroups groups) {
   
   
		super(registry, groups);
	}

	@ReadOperation
	public WebEndpointResponse<HealthComponent> health(ApiVersion apiVersion, SecurityContext securityContext) {
   
   
		return health(apiVersion, securityContext, false, NO_PATH);
	}

	@ReadOperation
	public WebEndpointResponse<HealthComponent> health(ApiVersion apiVersion, SecurityContext securityContext,
			@Selector(match = Match.ALL_REMAINING) String... path) {
   
   
		return health(apiVersion, securityContext, false, path);
	}

	public WebEndpointResponse<HealthComponent> health(ApiVersion apiVersion, SecurityContext securityContext,
			boolean showAll, String... path) {
   
   
		HealthResult<HealthComponent> result = getHealth(apiVersion, securityContext, showAll, path);
		if (result == null) {
   
   
			return (Arrays.equals(path, NO_PATH))
					? new WebEndpointResponse<>(DEFAULT_HEALTH, WebEndpointResponse.STATUS_OK)
					: new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND);
		}
		HealthComponent health = result.getHealth();
		HealthEndpointGroup group = result.getGroup();
		int statusCode = group.getHttpCodeStatusMapper().getStatusCode(health
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庄隐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值