简体   繁体   中英

Spring Boot Actuator Health Indicator

We have been using Spring Boot for several projects now, We are using the latest version 1.2.3. We are incorporating Actuator. So far things are working well except we are finding that the /health indicator [default] is showing that the service is down. This is not true. These services are that implement with datasources. It may call other SOAP or Rest services. What is the health service looking at to measure whether a service is down?

As #derFuerst said the DataSourceHealthIndicator has the default query to check whether the DB is up or not.

If you want to use this the proper vendor specific query you should write your own health indicator in your configuration class, like this in case of Oracle data source:

@Autowired(required = false)
private DataSource dataSource;

@Bean
@Primary
public DataSourceHealthIndicator dataSourceHealthIndicator() {
    return new DataSourceHealthIndicator(dataSource, "SELECT 1 FROM DUAL");
}

The DataSourceHealthIndicator is used to check availablity. The default query is SELECT 1 , but there are some product specific queries, too.
You can write your own HealthIndicator . Either you implement the interface or extend the AbstractHealthIndicator .
To disable the default db-health-check put this line into your application properties management.health.db.enabled=false . Hope that helps

The above comment helpedme to init my research but for me it was not enough :

@Bean
@Primary
public DataSourceHealthIndicator dataSourceHealthIndicator() {
    return new DataSourceHealthIndicator(dataSource, "SELECT 1 FROM DUAL");
}

Here is the configuration that helped me to make it run: Define HealthIndicator @Bean like the follow and provide the required query :

@Bean
@Primary
public HealthIndicator dbHealthIndicator() {
  return new DataSourceHealthIndicator(dataSource, "SELECT 1 FROM DUMMY");
}

If no Query is providen the SELECT 1 will be used . As #derFuerst said will be used , Here is the defailt implementation of DataSourceHealthIndicator :

public DataSourceHealthIndicator(DataSource dataSource, String query) {
    super("DataSource health check failed");
    this.dataSource = dataSource;
    this.query = query;
    this.jdbcTemplate = dataSource != null ? new JdbcTemplate(dataSource) : null;
}
...
protected String getValidationQuery(String product) {
        String query = this.query;
        if (!StringUtils.hasText(query)) {
            DatabaseDriver specific = DatabaseDriver.fromProductName(product);
            query = specific.getValidationQuery();
        }

        if (!StringUtils.hasText(query)) {
            query = "SELECT 1";
        }

        return query;
    }

Hi everyone I'm a beginner to health check using actuator. I used the below solution and it worked,

@Autowired(required = false)
private DataSource dataSource;

@Bean
@Primary
public DataSourceHealthIndicator dataSourceHealthIndicator() {
    return new DataSourceHealthIndicator(dataSource, "SELECT 1 FROM DUAL");
}

But can anyone please let me know how validation query is working even though there isn't a table named Dual. Also as per my understanding when we request "/actuator/health", all implementations of HealthIndicator are called automatically and health check methods are executed. Kindly correct me if I'm wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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