In this article, we will see the most simplest way of configuring cassandra following spring boot way.
All the factory beans that Spring boot supports for auto-configuration are defined in the the META-INF/spring.factories file, against the key org.springframework.boot.autoconfigure.EnableAutoConfiguration
.
Spring boot relies on these factory classes to auto-configures the necessary beans but will include/exclude factory beans on the basis of the jars that we have included in the classpath.
Spring Boot Starter Data Cassandra
In case of cassandra, it relies on CassandraAutoConfiguration
to configure cassandra.
@ConditionalOnClass({ Cluster.class }) @EnableConfigurationProperties(CassandraProperties.class) public class CassandraAutoConfiguration { ... }
As you can see below, spring relies on existence of cassandra driver class com.datastax.driver.core.Cluster
to decide whether to include cassandra auto configuration or not. It does this using the
@ConditionalOnClass({ Cluster.class })
We will next look at the dependencies added in build.gradle file.
Dependencies
We add the spring boot starter for cassandra. Once can either add org.springframework.boot:spring-boot-starter-data-cassandra
or the reactive flavour org.springframework.boot:spring-boot-starter-data-cassandra-reactive
dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-cassandra' runtimeOnly 'org.springframework.boot:spring-boot-devtools' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
Spring Cassandra Configuration
Spring uses org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration
to automatically configure the cluster for us.
It also configures additional beans using org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration
or org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration
in case we have selected reactive cassandra APIs.
Cassandra properties
The keyspace must already be existing. Here is the set of application properties used:
spring.data.cassandra.keyspace-name=movies spring.data.cassandra.schema-action=recreate spring.data.cassandra.contactpoints=localhost spring.data.cassandra.port=9042
And below is the list of complete cassandra properties that one can configure. We will break the properties into four categories.
Cluster properties
spring.data.cassandra.keyspaceName
spring.data.cassandra.schema-action
spring.data.cassandra.clusterName
spring.data.cassandra.contactPoints
spring.data.cassandra.port
Security properties
spring.data.cassandra.username
spring.data.cassandra.password
spring.data.cassandra.ssl
Query Options
spring.data.cassandra.consistencyLevel
spring.data.cassandra.serialConsistencyLevel
spring.data.cassandra.fetchSize
Socket Options
spring.data.cassandra.connectTimeout
spring.data.cassandra.readTimeout
Pool Options
spring.data.cassandra.idleTimeout
spring.data.cassandra.poolTimeout
spring.data.cassandra.heartbeatInterval
spring.data.cassandra.maxQueueSize
Others
spring.data.cassandra.compression
spring.data.cassandra.jmxEnabled
In our next article, we will see how to use our own way of configuring cassandra in case we want to have more control.