Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

ken4ward avatar image
ken4ward asked Erick Ramirez answered

How do I set the local DC in my Spring boot application?

I have been dealing with this for the past 36 hours, and yet to resolve it. I added Cassandra to the Spring boot application I'm working on. The configuration of the app is as follows:

Spring boot 2.4.5, Cassandra-3.11. After much effort I followed the instruction given to add this file in the resource folder of the app - application.conf as suggested, yet the error continue to happen

Application.conf

// Add `application.conf` to your classpath with the following contents:
datastax-java-driver {
  basic {
    contact-points = [ "127.0.0.1:9042"]
    load-balancing-policy.local-datacenter = datacenter1
  }
}

Spring boot dependency

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>

This is my spring boot properties for Cassandra

spring.data.cassandra.contact-points=127.0.0.1
spring.data.cassandra.username=cassandra
spring.data.cassandra.password=cassandra
spring.data.cassandra.keyspace=accountservicedb
spring.data.cassandra.port=9042
spring.data.cassandra.schema-action=CREATE_IF_NOT_EXIST
spring.data.cassandra.local-datacenter=datacenter1
load-balancing-policy.local-datacenter = datacenter1

This is the crux of the error

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=c762a627-1a89-4fc0-89dc-0d8b8cccbee1, hashCode=4c83b585)=datacenter1. Current DCs in this cluster are: datacenter1

With all of the implementation of the suggested actions, nothing happened, the error continued to occur. And it seems the properties.conf file isn't picked up at all.

Please, help out. Much time has been wasted on this simple thing already. Thanks a lot.

spring-boot
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered

With Spring Boot, you need to set the properties either in the application.properties file, application.yml file or as command-line switches. Spring Boot does not use application.conf.

As you stated, the configuration isn't getting picked up by your application so I suspect that the configuration file is not in the correct location. If you're using the properties file, make sure that it is located in:

src/
  main/
    resources/
      application.properties

To avoid conflicts, make sure that you only have the properties file and remove all other files like application.yml or application.conf.

When you load the application, check the classpath to make sure that the resources/ directory is included. Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.