聊聊redis的HealthIndicator

本文探讨了Spring Boot Actuator中RedisHealthIndicator与RedisReactiveHealthIndicator的工作原理。前者用于同步客户端,后者专为响应式客户端设计,如lettuce。两者都通过调用info命令来检查Redis的运行状态。

本文主要研究一下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);
		}
	}

}
复制代码
  • 这里判断是否是cluster,如果是则调用clusterGetClusterInfo,否则调用info方法

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

}
复制代码
  • 采用lettuce的话,启用的是RedisReactiveHealthIndicator,这里调用info方法,不过这个方法返回的属性有点多,大约有83个

小结

redis的HealthIndicator分为RedisHealthIndicator以及RedisReactiveHealthIndicator。如果底层client采用的是lettuce,则使用的是reactive版本。其health检测方法,就是调用info命令。

doc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值