Move code from spring-boot-actuator to spring-boot-data-redis

This commit is contained in:
Andy Wilkinson
2025-05-09 15:19:52 +01:00
committed by Phillip Webb
parent 631653cd1d
commit ee458e1909
12 changed files with 33 additions and 30 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.health;
import java.util.Properties;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.data.redis.connection.ClusterInfo;
/**
* Shared class used by {@link RedisHealthIndicator} and
* {@link RedisReactiveHealthIndicator} to provide health details.
*
* @author Phillip Webb
*/
final class RedisHealth {
private RedisHealth() {
}
static Builder up(Health.Builder builder, Properties info) {
builder.withDetail("version", info.getProperty("redis_version"));
return builder.up();
}
static Builder fromClusterInfo(Health.Builder builder, ClusterInfo clusterInfo) {
builder.withDetail("cluster_size", clusterInfo.getClusterSize());
builder.withDetail("slots_up", clusterInfo.getSlotsOk());
builder.withDetail("slots_fail", clusterInfo.getSlotsFail());
if ("fail".equalsIgnoreCase(clusterInfo.getState())) {
return builder.down();
}
else {
return builder.up();
}
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.health;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.util.Assert;
/**
* Simple implementation of a {@link HealthIndicator} returning status information for
* Redis data stores.
*
* @author Christian Dupuis
* @author Richard Santana
* @author Scott Frederick
* @since 4.0.0
*/
public class RedisHealthIndicator extends AbstractHealthIndicator {
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 {
doHealthCheck(builder, connection);
}
finally {
RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory);
}
}
private void doHealthCheck(Health.Builder builder, RedisConnection connection) {
if (connection instanceof RedisClusterConnection clusterConnection) {
RedisHealth.fromClusterInfo(builder, clusterConnection.clusterGetClusterInfo());
}
else {
RedisHealth.up(builder, connection.serverCommands().info());
}
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.health;
import java.util.Properties;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
import org.springframework.data.redis.connection.ClusterInfo;
import org.springframework.data.redis.connection.ReactiveRedisClusterConnection;
import org.springframework.data.redis.connection.ReactiveRedisConnection;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
/**
* A {@link ReactiveHealthIndicator} for Redis.
*
* @author Stephane Nicoll
* @author Mark Paluch
* @author Artsiom Yudovin
* @author Scott Frederick
* @since 4.0.0
*/
public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
private final ReactiveRedisConnectionFactory connectionFactory;
public RedisReactiveHealthIndicator(ReactiveRedisConnectionFactory connectionFactory) {
super("Redis health check failed");
this.connectionFactory = connectionFactory;
}
@Override
protected Mono<Health> doHealthCheck(Health.Builder builder) {
return getConnection().flatMap((connection) -> doHealthCheck(builder, connection));
}
private Mono<ReactiveRedisConnection> getConnection() {
return Mono.fromSupplier(this.connectionFactory::getReactiveConnection)
.subscribeOn(Schedulers.boundedElastic());
}
private Mono<Health> doHealthCheck(Health.Builder builder, ReactiveRedisConnection connection) {
return getHealth(builder, connection).onErrorResume((ex) -> Mono.just(builder.down(ex).build()))
.flatMap((health) -> connection.closeLater().thenReturn(health));
}
private Mono<Health> getHealth(Health.Builder builder, ReactiveRedisConnection connection) {
if (connection instanceof ReactiveRedisClusterConnection clusterConnection) {
return clusterConnection.clusterGetClusterInfo().map((info) -> fromClusterInfo(builder, info));
}
return connection.serverCommands().info("server").map((info) -> up(builder, info));
}
private Health up(Health.Builder builder, Properties info) {
return RedisHealth.up(builder, info).build();
}
private Health fromClusterInfo(Health.Builder builder, ClusterInfo clusterInfo) {
return RedisHealth.fromClusterInfo(builder, clusterInfo).build();
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Redis health integration using Spring Data Redis.
*/
package org.springframework.boot.data.redis.actuate.health;