简体   繁体   English

春季健康指标-超时

[英]Spring HealthIndicator - Timeout

We do use Spring Boot Health Checks in our application. 我们在应用程序中确实使用了Spring Boot运行状况检查。 On one of the checked applications it looks like the DB cannot answer in a timely manner. 在一个已检查的应用程序上,数据库似乎无法及时回答。 We are using the DataSourceHealthIndicator, and this is answering after several seconds with an exception which is fine, but takes different timeframes to come back. 我们正在使用DataSourceHealthIndicator,这是在几秒钟后回答,但可以,但需要不同的时间范围才能返回。

Could we set a timeout on this HealthIndicator (and most probably on others as well), so that the HealthIndicator reports back after say 2s in the latest with an error, if no connection to the DB can be established? 我们是否可以在此HealthIndicator上设置一个超时时间(并且很可能在其他状态上也设置超时),以便HealthIndicator至少在2秒后报告错误,如果无法建立与数据库的连接?

Or could we ask the Datasource if there are still any open connections available? 还是我们可以问数据源,是否仍然有可用的开放连接?

I know, that we should fix the problem with these connections and we are already working on this one, but a HealthCheck for something like this would be nice as well. 我知道,我们应该使用这些连接来解决问题,并且我们已经在努力解决这一问题,但是类似这样的HealthCheck也将很不错。

You could disable the default db health indicator in your application.properties 您可以在application.properties中禁用默认的数据库运行状况指示器

management.health.db.enabled=false

and implement custom HealthIndicator that has desired behavior, as described here . 并实现自定义HealthIndicator具有期望的行为,如所描述这里

In your custom HealthIndicator implemetation you could use a different JdbcTemplate that will have desired timeout value of 2 seconds, something like this: 在您的自定义HealthIndicator实现中,您可以使用其他JdbcTemplate ,其期望的超时值为2秒,如下所示:

JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
jdbcTemplate.setQueryTimeout(2);
jdbcTemplate.execute(...);

If execute call throws an exception, your indicator should return Health.down() , otherwise Health.up() should be returned. 如果执行调用抛出一个异常,你的指标应该返回Health.down()否则Health.up()应返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM