如何在Spring Actuator的健康检查中排除Redis检查

10

当 Redis 宕机时,服务健康检查 API 返回 503。

{
    "status": "DOWN",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 250790436864,
                "free": 95412813824,
                "threshold": 10485760
            }
        },
        "db": {
            "status": "UP",
            "details": {
                "database": "PostgreSQL",
                "hello": 1
            }
        },
        "refreshScope": {
            "status": "UP"
        },
        "redis": {
            "status": "DOWN",
            "details": {
                "error": "org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379"
            }
        }
    }
}

实际上,即使Redis宕机,服务仍然可用。

服务状态能否不受Redis状态的影响?我还需要在健康检查API中查看Redis的详细信息。

3个回答

14

management.health.redis.enabled=false

将此代码添加到您的application.yaml/properties中以禁用Redis健康检查。


4

非常感谢。在将config management.health.redis.enabled设置为false后,Redis健康检查已经消失了。但是我是否有办法在执行器中检查Redis状态呢? - huangydyn
我认为(之前没有做过),你需要为Redis制作一个自定义的健康指标,然后(提示:扩展默认值),使其始终返回UP,并在详细信息中添加其实际状态。否则,当Redis宕机时,您的主要健康检查将会是DOWN。 - Bavo Daniels

0
我们通过重写SimpleStatusAggregatorgetAggregateStatus方法来解决这个问题,以便在详细信息中显示Redis状态,但不影响聚合状态。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.*;
import org.springframework.boot.actuate.redis.RedisHealthIndicator;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.Collections.singletonList;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.toList;


@Component
public class CustomStatusAggregator extends SimpleStatusAggregator {

    @Autowired
    private List<HealthContributor> healthContributorList;

       
    @Override
    public Status getAggregateStatus(Set<Status> statuses) {
        return super.getAggregateStatus(getStatuses());
    }

    private Set<Status> getStatuses() {
        Stream<Status> statuses = healthContributorList //
                .stream() //
                .map(CustomStatusAggregator::getHealthIndicators) //
                .flatMap(List<HealthIndicator>::stream) //
                .filter(not(RedisHealthIndicator.class::isInstance)) //
                .map(HealthIndicator::health) //
                .map(Health::getStatus);

        return statuses.collect(Collectors.toSet());
    }

    private static List<HealthIndicator> getHealthIndicators(HealthContributor healthContributor) {
        if (healthContributor instanceof HealthIndicator) {
            return singletonList(((HealthIndicator) healthContributor));
        }

        return stream((CompositeHealthContributor) healthContributor) //
                .map(NamedContributor::getContributor) //
                .map(CustomStatusAggregator::getHealthIndicators) //
                .flatMap(List<HealthIndicator>::stream) //
                .collect(toList());
    }

    private static <T> Stream<T> stream(Iterable<T> iterable) {
        return StreamSupport.stream(iterable.spliterator(), false);
    }

}

如果你同时拥有 ReactiveHealthContributorHealthContributor,你可以将它们的状态合并。
@Autowired
private List<ReactiveHealthContributor> reactiveHealthContributorList;

return Stream.concat(statuses, reactiveStatuses).collect(Collectors.toSet());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接