【问题标题】:How to check Database health and connection pool health如何检查数据库运行状况和连接池运行状况
【发布时间】:2019-10-21 15:40:03
【问题描述】:
@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    // Use the builder to build the health status details that should be reported.
    // If you throw an exception, the status will be DOWN with the exception message.

    builder.up()
            .withDetail("app", "Alive and Kicking")
            .withDetail("error", "Nothing! I'm good.");
}

} 在这里,我注意到默认的健康检查是通过 /health 进行的,当 dataConnection 池缺少可用连接时,我想覆盖上面的内容,我需要返回未准备好的 pod。我还需要检查数据库的运行状况。如何实现?

【问题讨论】:

    标签: spring spring-boot spring-data-jpa spring-jdbc spring-boot-actuator


    【解决方案1】:

    多年来我都做过类似的事情。 根据我对 HikariDataSource 的经验,这是我的结果:

    @Autowired
        private DataSource dataSource;
    
        @Override
        protected void doHealthCheck(Health.Builder builder) throws Exception {
    
            HikariDataSource ds = (HikariDataSource) dataSource;
            Integer maxSize = ds.getMaximumPoolSize();
            Integer active = new HikariDataSourcePoolMetadata(ds).getActive();
            Double usage = new Double((active / maxSize) * 100);
    
            Health.Builder workingBuilder;
    
            if(usage > 90) {
                workingBuilder = builder.down();
            }else {
                workingBuilder = builder.up();
            }
    
            workingBuilder.withDetail("max", maxSize) //
            .withDetail("active", active)//
            .withDetail("usage", usage);
        }
    

    也许Actuator Metrics或使用

    有更好的方法
       ds.getHealthCheckProperties()
       ds.getHealthCheckRegistry()
    

    【讨论】:

    • 嗨,谢谢 bemar,它在 openshift 中是否可以工作,并与 UDB 连接
    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多