@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 not ready。我还需要检查数据库的运行状况。该如何实现?
答案 0 :(得分:0)
我做过类似的事情很久了。 根据我对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()