如何检查数据库运行状况和连接池运行状况


@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 未准备好。我还需要检查数据库的运行状况。如何实现?

我做了很多类似的事情。 根据我对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);
}

也许有更好的方法 执行器 指标或

ds.getHealthCheckProperties()
ds.getHealthCheckRegistry()

最新更新