springboot的健康检查HealthIndicators实战
⽬录
springboot 健康检查HealthIndicators
springboot health indicator原理及其使⽤
作⽤
⾃动配置的Health Indicator
分组
如何管理Health Indicator
RedisHealthIndicator源码解析
⾃定义Indicator
springboot 健康检查HealthIndicators
想提供⾃定义健康信息,你可以注册实现了HealthIndicator接⼝的Spring beans。
你需要提供⼀个health()⽅法的实现,并返回⼀个Health响应。
Health响应需要包含⼀个status和可选的⽤于展⽰的详情。
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealth implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // perform some specific health check
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
} r
eturn Health.up().build();
}
}
除了Spring Boot预定义的Status类型,Health也可以返回⼀个代表新的系统状态的⾃定义Status。
在这种情况下,需要提供⼀个HealthAggregator接⼝的⾃定义实现,或使⽤management.der属性配置默认的实现。
例如,假设⼀个新的,代码为FATAL的Status被⽤于你的⼀个HealthIndicator实现中。为了配置严重程度,你需要将下⾯的配
置添加到application属性⽂件中:
management.der: DOWN, OUT_OF_SERVICE, UNKNOWN, UP
如果使⽤HTTP访问health端点,你可能想要注册⾃定义的status,并使⽤HealthMvcEndpoint进⾏映射。例如,你可以将
FATAL映射为HttpStatus.SERVICE_UNAVAILABLE。
springboot health indicator原理及其使⽤
作⽤
sping boot health 可以通过暴露的接⼝来提供系统及其系统组件是否可⽤。默认通过/health来访问。返回结果如下:
{
"status": "UP",
"discoveryComposite": {
"description": "Spring Cloud Eureka Discovery Client",
"status": "UP",
"discoveryClient": {
"description": "Spring Cloud Eureka Discovery Client",
"status": "UP",
"services": [
"..."
]
"eureka": {
"description": "Remote status from Eureka server",
"status": "UP",
"applications": {
"AOMS-MOBILE": 1,
"DATA-EXCHANGE": 1,
"CLOUD-GATEWAY": 2,
"AOMS": 1,
"AOMS-AIIS": 0,
"AOMS-EUREKA": 2
}
}
},
"diskSpace": {
"status": "UP",
"total": 313759301632,
"free": 291947081728,
"threshold": 10485760
},
"refreshScope": {
"status": "UP"
},
"hystrix": {
"status": "UP"
}
}
状态说明:
UNKNOWN:未知状态,映射HTTP状态码为503
UP:正常,映射HTTP状态码为200
DOWN:失败,映射HTTP状态码为503
OUT_OF_SERVICE:不能对外提供服务,但是服务正常。映射HTTP状态码为200
注意:UNKNOWN,DOWN,OUT_OF_SERVICE在为微服务环境下会导致注册中⼼中的实例也为down状态,请根据具体的业务来正确使⽤状态值。
⾃动配置的Health Indicator
⾃动配置的HealthIndicator主要有以下内容:
Key Name Description
cassandra CassandraDriverHealthIndicator Checks that a Cassandra database is up.
couchbase CouchbaseHealthIndicator Checks that a Couchbase cluster is up.
datasource DataSourceHealthIndicator Checks that a connection to DataSource can be obtained.
diskspace DiskSpaceHealthIndicator Checks for low disk space.
elasticsearch ElasticsearchRestHealthIndicator Checks that an Elasticsearch cluster is up.
hazelcast HazelcastHealthIndicator Checks that a Hazelcast server is up.
influxdb InfluxDbHealthIndicator Checks that an InfluxDB server is up.
jms JmsHealthIndicator Checks that a JMS broker is up.
ldap LdapHealthIndicator Checks that an LDAP server is up.
mail MailHealthIndicator Checks that a mail server is up.
mongo MongoHealthIndicator Checks that a Mongo database is up.
neo4j Neo4jHealthIndicator Checks that a Neo4j database is up.
ping PingHealthIndicator Always responds with UP.
rabbit RabbitHealthIndicator Checks that a Rabbit server is up.
redis RedisHealthIndicator Checks that a Redis server is up.
solr SolrHealthIndicator Checks that a Solr server is up.
分组
可以通过⼀个别名来启⽤⼀组指标的访问。配置的格式如下:
如何管理Health Indicator
开启
可以通过management.abled来启⽤key对应的indicator。例如:management.abled=true
关闭
management.abled=false
RedisHealthIndicator源码解析
下⾯,通过RedisHealthIndicator源码来为什么可以这么写。
代码结构
⾃动配置的health indicator有HealthIndicator和HealthIndicatorAutoConfiguration两部分组成。HealthIndicator所在包在org.springframework.boot.actuate,HealthIndicatorAutoConfiguration所在包在org.springframework.boot.actuate.autoconfigure下
//RedisHealthIndicator.java
public class RedisHealthIndicator extends AbstractHealthIndicator {
static final String VERSION = "version";
static final String REDIS_VERSION = "redis_version";
private final RedisConnectionFactory redisConnectionFactory;
public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
super("Redis health check failed");
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
RedisConnection connection = RedisConnectionUtils
.disConnectionFactory);
try {
if (connection instanceof RedisClusterConnection) {
ClusterInfo clusterInfo = ((RedisClusterConnection) connection)
.clusterGetClusterInfo();
builder.up().withDetail("cluster_size", ClusterSize())
.withDetail("slots_up", SlotsOk())
.withDetail("slots_fail", SlotsFail());
}
else {
Properties info = connection.info();
builder.up().withDetail(VERSION, Property(REDIS_VERSION));
}
}
finally {
}
}
}
主要实现doHealthCheck⽅法来实现具体的判断逻辑。注意,操作完成后在finally中释放资源。
在⽗类AbstractHealthIndicator中,对doHealthCheck进⾏了try catch,如果出现异常,则返回Down状态。 //AbstractHealthIndicator.java
@Override
public final Health health() {
Health.Builder builder = new Health.Builder();
try {
doHealthCheck(builder);
}
catch (Exception ex) {
if (this.logger.isWarnEnabled()) {
String message = this.healthCheckFailedMessage.apply(ex);
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE,
ex);
}
builder.down(ex);
}
return builder.build();
}
RedisHealthIndicatorAutoConfiguration完成RedisHealthIndicator的⾃动配置
@Configuration
@ConditionalOnClass(RedisConnectionFactory.class)
@ConditionalOnBean(RedisConnectionFactory.class)
@ConditionalOnEnabledHealthIndicator("redis")
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
@AutoConfigureAfter({ RedisAutoConfiguration.class,
RedisReactiveHealthIndicatorAutoConfiguration.class })
public class RedisHealthIndicatorAutoConfiguration extends
CompositeHealthIndicatorConfiguration<RedisHealthIndicator, RedisConnectionFactory> {
private final Map<String, RedisConnectionFactory> redisConnectionFactories;
public RedisHealthIndicatorAutoConfiguration(
Map<String, RedisConnectionFactory> redisConnectionFactories) {
}
@Bean
@ConditionalOnMissingBean(name = "redisHealthIndicator")
public HealthIndicator redisHealthIndicator() {
return disConnectionFactories);
}
}
重点说明ConditionalOnEnabledHealthIndicator:如果abled为true,则⽣效。CompositeHealthIndicatorConfiguration 中会通过HealthIndicatorRegistry注册创建的HealthIndicator
⾃定义Indicator
@Component
@ConditionalOnProperty(name="spring.dfs.http.send-url")
@Slf4j
public class DfsHealthIndicator implements HealthIndicator {
@Value("${spring.dfs.http.send-url}")
private String dsfSendUrl;
@Override
public Health health() {
log.debug("正在检查dfs配置项...");
log.debug("dfs 请求地址:{}",dsfSendUrl);
Health.Builder up = Health.up().withDetail("url", dsfSendUrl);
try {
return up.build();
} catch (IOException e) {
e.printStackTrace();
<("DFS配置项错误或⽹络超时");
return up.withException(e).build();
}
}
}
返回值:
{
"dfs": {
"status": "UP",
"url": "10.254.131.197:8088",
"error": "java.ConnectException: Connection refused (Connection refused)"    }
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
mvc实例