Talk about redis’ HealthIndicator

  redis

Order

This article mainly studies redis’ HealthIndicator

RedisHealthIndicator

spring-boot-actuator-2.0.4.RELEASE-sources.jar! /org/springframework/boot/actuate/redis/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");
        Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
        this.redisConnectionFactory = connectionFactory;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        RedisConnection connection = RedisConnectionUtils
                .getConnection(this.redisConnectionFactory);
        try {
            if (connection instanceof RedisClusterConnection) {
                ClusterInfo clusterInfo = ((RedisClusterConnection) connection)
                        .clusterGetClusterInfo();
                builder.up().withDetail("cluster_size", clusterInfo.getClusterSize())
                        .withDetail("slots_up", clusterInfo.getSlotsOk())
                        .withDetail("slots_fail", clusterInfo.getSlotsFail());
            }
            else {
                Properties info = connection.info();
                builder.up().withDetail(VERSION, info.getProperty(REDIS_VERSION));
            }
        }
        finally {
            RedisConnectionUtils.releaseConnection(connection,
                    this.redisConnectionFactory);
        }
    }

}
  • It is determined here whether it is a cluster. If it is, clusterGetClusterinfo is called, otherwise, Info method is called.

RedisReactiveHealthIndicator

spring-boot-actuator-2.0.4.RELEASE-sources.jar! /org/springframework/boot/actuate/redis/RedisReactiveHealthIndicator.java

public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicator {

    private final ReactiveRedisConnectionFactory connectionFactory;

    public RedisReactiveHealthIndicator(
            ReactiveRedisConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    @Override
    protected Mono<Health> doHealthCheck(Health.Builder builder) {
        ReactiveRedisConnection connection = this.connectionFactory
                .getReactiveConnection();
        return connection.serverCommands().info().map((info) -> up(builder, info))
                .doFinally((signal) -> connection.close());
    }

    private Health up(Health.Builder builder, Properties info) {
        return builder.up().withDetail(RedisHealthIndicator.VERSION,
                info.getProperty(RedisHealthIndicator.REDIS_VERSION)).build();
    }

}
  • If lettuce is used, RedisReactiveHealthIndicator is enabled. info method is called here, but this method returns a little more attributes, about 83

Summary

Redis’ HealthIndicator is divided into Redistribution and RedisReactiveHealthIndicator. If the underlying client uses lettuce, the reactive version is used. Its health detection method is to call info command.

doc