1

I am building an web services that exposes data from Cassandra to RESTful interface. Also, using Spring-boot-web for the REST service part, and spring-boot-actuator for production ready features, Spring-data-cassandra for Cassandra interface. I'm looking for a custom HealthIndicator (http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html#production-ready-health) for CassandraTemplate that I can plug-in.

I haven't found any from the Spring-data-cassandra documentation. Do we have any under-development?

More in general, what would be a good strategy to check the health of CassandraTemplate?

Sewook Wee
  • 247
  • 1
  • 3
  • 6

2 Answers2

1

Neither Spring Boot nor Spring Data Cassandra provides a HealthIndicator for Cassandra out of the box, but building your own is straightforward. You just need to create a new HealthIndicator bean that interacts with Cassandra. For example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.dao.DataAccessException;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Component;

@Component
public class CassandraHealthIndicator extends AbstractHealthIndicator {

    private final CassandraOperations cassandra;

    @Autowired
    public CassandraHealthIndicator(CassandraOperations cassandra) {
        this.cassandra = cassandra;
    }

    @Override
    protected void doHealthCheck(Builder builder) throws Exception {
        try {
            this.cassandra.execute("SELECT now() FROM system.local");
            builder.up().build();
        } catch (DataAccessException ex) {
            builder.down(ex).build();
        }
    }

}

As long as this bean exists in your application context Spring Boot's Actuator component will find it and use it when determining your application's health.

Exactly what query you execute against Cassandra may vary depending on your requirements. The query used above was taken from this answer to a question about performing a health check on Cassandra.

Community
  • 1
  • 1
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
1

CassandraHealthIndicator is available since Spring Boot 2.0

Tomáš Poch
  • 790
  • 6
  • 13