聊聊redis的HealthIndicator

本文主要研究一下redis的HealthIndicatorhtml

RedisHealthIndicator

spring-boot-actuator-2.0.4.RELEASE-sources.jar!/org/springframework/boot/actuate/redis/RedisHealthIndicator.javajava

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);
        }
    }

}
  • 這裏判斷是不是cluster,若是是則調用clusterGetClusterInfo,不然調用info方法

RedisReactiveHealthIndicator

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

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();
    }

}
  • 採用lettuce的話,啓用的是RedisReactiveHealthIndicator,這裏調用info方法,不過這個方法返回的屬性有點多,大約有83個

小結

redis的HealthIndicator分爲RedisHealthIndicator以及RedisReactiveHealthIndicator。若是底層client採用的是lettuce,則使用的是reactive版本。其health檢測方法,就是調用info命令。redis

doc

相關文章
相關標籤/搜索