Sampling minimal configuration to work with Apache Cassandra
- java 17
- gradle 8.5
- docker 20 or newer
- a modern ide (intellij ultimate)
First spin up a cassandra server with docker compose:
docker compose -f src/infrastructure/docker-compose.yml up
Then start the project either using the ide or using gradle wrapper
./gradlew bootRun
Note: if this error occurs:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraSession' defined in class path resource [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception with message: Invalid keyspace spring_cassandra
You will need to manually create the cassandra keyspace. there is a test script that you can use to prepare cassandra database.
There are a few test samples using testcontainers, so you don't need to manually create and run cassandra:
./gradlew test
It also allows CI to run with no external dependencies, which is the correct way to do CI runs. See gradle.yml for details.
One thing is regular cassandra. Another beast is AWS Keyspaces.
Using the sample project as reference, this configuration was made for better reuse in future.
In order to proper get this working, you must provide the following environment variables:
AWS_ACCESS_KEY_ID=<aws account id>
AWS_SECRET_ACCESS_KEY=<aws account key>
CASSANDRA_KEYSPACE_NAME=<cassandra keyspace>
CASSANDRA_USERNAME=<service account user>
CASSANDRA_PASSWORD=<service account password>
Then you can configure the IDE to run with the aws profile. Or try this command line:
./gradlew bootRun --args="--spring.profiles.active=aws"
For further reference, please consider the following sections:
- Official Gradle documentation
- Spring Boot Gradle Plugin Reference Guide
- Create an OCI image
- Spring Boot Testcontainers support
- Testcontainers Cassandra Module Reference Guide
- Spring Web
- Spring Data for Apache Cassandra
- Testcontainers
The following guides illustrate how to use some features concretely:
- Building a RESTful Web Service
- Serving Web Content with Spring MVC
- Building REST services with Spring
- Spring Data for Apache Cassandra
These additional references should also help you:
This project uses Testcontainers at development time.
Testcontainers has been configured to use the following Docker images:
Please review the tags of the used images and set them to the same as you're running in production.