Trying to set up locally Spring Boot with Cassandra [duplicate]
Author: kalnar
Originally Sourced from: https://stackoverflow.com/questions/79714915/trying-to-set-up-locally-spring-boot-with-cassandra
I am learning Spring Boot 3.5.3 and I am trying to set up a Cassandra database with it following the instructions of the book Spring in Action by Craig Walls. When my repository tries to connect to the database, I have an error saying that I didn't set the local DC, although I did in the application.yml.
Here is the minimal working example on github: https://github.com/kalnar/TacoCloudApp/tree/cassandra_example.
My application.yml
:
spring:
application:
name: TacoCloudApp
thymeleaf:
cache: false
data:
cassandra:
keyspace-name: tacocloud
contact-points: localhost
port: 9042 # This is the default port, but good to include for clarity
local-datacenter: datacenter1
schema-action: create-if-not-exists
logging:
level:
com.datastax.oss.driver: DEBUG
org.springframework.data.cassandra: DEBUG
I am using docker to run the Cassandra cluster.
I created the docker image using:
docker network create cassandra-net
docker run --name my-cassandra \
--network cassandra-net \
-p 9042:9042 \
-d cassandra:latest
I logged in the database and created a namespace using:
docker run -it --network cassandra-net --rm cassandra cqlsh my-cassandra
create keyspace tacocloud
... with replication={'class':'SimpleStrategy', 'replication_factor':1}
... and durable_writes=true;
The error that I have when trying to use the repository on localhost:8080/design
:
java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=/127.0.0.1:9042, hostId=dd5ae1ee-c050-4062-8968-52fc4ccd6cc5, hashCode=9891df0)=datacenter1. Current DCs in this cluster are: datacenter1
What I have found about this issue:
https://github.com/spring-projects/spring-boot/issues/19779#issuecomment-834365162 Where they said in the end that locally it was working for them, not saying how they resolved the issue. Also they were using Spring 2.3.
https://medium.com/geekculture/internal-working-of-cassandra-driver-in-springboot-application-d45ce58b748b
I tried using the property localDC
instead of local-datacenter
.
https://stackoverflow.com/a/79206092/2847905
I also tried passing CLI arguments when running the app via IntellIJ IDEA using:
-Ddatastax-java-driver.basic.load-balancing-policy.local-datacenter=datacenter1
I have also verified the cluster, the local datacenter is named datacenter1.