Jay Wagner
Jay Wagner

Reputation: 51

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?

Upvotes: 5

Views: 17666

Answers (4)

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

Upvotes: 0

plp6k0ff
plp6k0ff

Reputation: 41

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;
    }

Upvotes: 4

StMa
StMa

Reputation: 149

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");
}

Upvotes: 10

derFuerst
derFuerst

Reputation: 51

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

Upvotes: 5

Related Questions