Spring boot application with spring data cassandra failing to start

Tags: , , , ,



I have a spring boot application with spring web & spring data cassandra as dependencies. And I have a main method in a class annotated with @SpringBootApplication.

    <dependency>
      <artifactId>spring-boot-starter-web</artifactId>
      <groupId>org.springframework.boot</groupId>
    </dependency>
    <dependency>
      <artifactId>spring-boot-starter-data-cassandra</artifactId>
      <groupId>org.springframework.boot</groupId>
    </dependency>
    <!--<dependency>
      <artifactId>java-driver-core</artifactId>
      <exclusions>
        <exclusion>
          <artifactId>*</artifactId>
          <groupId>org.apache.tinkerpop</groupId>
        </exclusion>
      </exclusions>
      <groupId>com.datastax.oss</groupId>
      <version>${cassandra-java-driver.version}</version>
    </dependency>
    <dependency>
      <artifactId>native-protocol</artifactId>
      <groupId>com.datastax.oss</groupId>
      <version>${cassandra-native-protocol.version}</version>
    </dependency>-->
    <dependency>
      <artifactId>lombok</artifactId>
      <groupId>org.projectlombok</groupId>
    </dependency>
  </dependencies>


  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.3.4.RELEASE</version>
  </parent>

  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <relativePath/>
    <version>2.3.4.RELEASE</version> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <cassandra-java-driver.version>4.9.0</cassandra-java-driver.version>
    <cassandra-native-protocol.version>1.4.11</cassandra-native-protocol.version>
    <java.version>11</java.version>
    <lombok.version>1.18.16</lombok.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <properties>
    <java.version>11</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

I run this application & run into the below error.

    org.springframework.beans.factory.UnsatisfiedDependencyException: **Error creating bean with name 'org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraSession' defined in class path resource **[org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception; nested exception is com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node(endPoint=/127.0.0.1:9042, hostId=null, hashCode=7ecf2a53): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)]
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:797) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:227) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at 
    
    .
    .
    .
    
    
    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]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception; nested exception is com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node(endPoint=/127.0.0.1:9042, hostId=null, hashCode=7ecf2a53): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)]
        at 
    
    .
    .
    .
    
    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception; nested exception is com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node(endPoint=/127.0.0.1:9042, hostId=null, hashCode=7ecf2a53): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        ... 32 common frames omitted
    Caused by: com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node(endPoint=/127.0.0.1:9042, hostId=null, hashCode=7ecf2a53): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedCh

annelException)]

I am guessing this might be a some dependency related problem, but can’t figure out exactly what. I have tried few things which are commented in the pom.xml, those did not work.

Answer

The problem is the connection with the cassandra. Make sure that you’ve been created the keyspace-name on cassandra. After that in your project declare a Bean Configuration with the keyspace, in my case mykeyspace. You can visit https://kayaerol84.medium.com/cassandra-cluster-management-with-docker-compose-40265d9de076 for more details about setting up the keyspace.

 @Configuration
 public class AppConfig {
    public @Bean CqlSession session() {
       return CqlSession.builder().withKeyspace("mykeyspace").build();
    }
 } 


Source: stackoverflow