/actuator/health
此节点可以检查各个组件的健康状态,例如调用接口时,DB组件会返回OK。
HealthMvcEndpoint && HealthEndpoint
- /actuator/health接口是由HealthMvcEndpoint的invoke方法来处理的。
- HealthMvcEndpoint的底层是通过HealthEndpoint来实现的。
- HealthMvcEndpoint初始化代码
@Bean
@ConditionalOnBean(HealthEndpoint.class)
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint("health")
public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate,
ManagementServerProperties managementServerProperties) {
HealthMvcEndpoint healthMvcEndpoint = new HealthMvcEndpoint(delegate,
this.managementServerProperties.getSecurity().isEnabled(),
managementServerProperties.getSecurity().getRoles());
if (this.healthMvcEndpointProperties.getMapping() != null) {
healthMvcEndpoint
.addStatusMapping(this.healthMvcEndpointProperties.getMapping());
}
return healthMvcEndpoint;
}
- HealthEndpoint初始化代码
@Bean
@ConditionalOnMissingBean
public HealthEndpoint healthEndpoint() {
return new HealthEndpoint(
this.healthAggregator == null ? new OrderedHealthAggregator()
: this.healthAggregator,
this.healthIndicators == null
? Collections.<String, HealthIndicator>emptyMap()
: this.healthIndicators);
}
自定义组件的健康状态检查
某些情况下,当我们需要对我们新建的一个模块进行健康监控的话,可以把这些监控信息统一归纳到health节点下,只需要实现HealthIndicator接口即可。
/**
* Strategy interface used to provide an indication of application health.
*
* @author Dave Syer
* @see ApplicationHealthIndicator
*/
public interface HealthIndicator {
/**
* Return an indication of health.
* @return the health for
*/
Health health();
}
一般都是通过继承AbstractHealthIndicator来实现的,添加@Component即可。
/**
* Base {@link HealthIndicator} implementations that encapsulates creation of
* {@link Health} instance and error handling.
* <p>
* This implementation is only suitable if an {@link Exception} raised from
* {@link #doHealthCheck(org.springframework.boot.actuate.health.Health.Builder)} should
* create a {@link Status#DOWN} health status.
*
* @author Christian Dupuis
* @since 1.1.0
*/
public abstract class AbstractHealthIndicator implements HealthIndicator {
private final Log logger = LogFactory.getLog(getClass());
@Override
public final Health health() {
Health.Builder builder = new Health.Builder();
try {
doHealthCheck(builder);
}
catch (Exception ex) {
this.logger.warn("Health check failed", ex);
builder.down(ex);
}
return builder.build();
}
/**
* Actual health check logic.
* @param builder the {@link Builder} to report health status and details
* @throws Exception any {@link Exception} that should create a {@link Status#DOWN}
* system status.
*/
protected abstract void doHealthCheck(Health.Builder builder) throws Exception;
}