Search code examples
javaspringcassandraspring-webfluxamazon-keyspaces

Connect to AWS keyspaces with Spring reactive data autoconfiguration


Im trying to connect to my aws keyspaces using spring-boot-starter-data-cassandra-reactive and I get the following error:

Error creating bean with name 'cassandraSession' defined in class path resource [com/pams/framework/windsoncore/configurations/CassandraConfiguration.class]: Invocation of init method failed; 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=cassandra.us-east-2.amazonaws.com:9142, hostId=null, hashCode=33a6c4f3): [com.datastax.oss.driver.api.core.DriverTimeoutException: [s5|control|id: 0xe0ebe047, L:/10.0.0.128:54474 - R:cassandra.us-east-2.amazonaws.com/3.12.23.155:9142] Protocol initialization request, step 1 (OPTIONS): timed out after 500 ms]

I tried their documentation, that works, but I wanted to used the spring auto configuration and reactive cassandra repos. I added all the configurations in application.yml and passed the trustStore information as JVM args. But I cannot get past these errors. I've tried looking around for this and have not found a working solution.

I would appreciate if someone can help me out here.

I have also tried the following:

@Configuration
@Slf4j
@EnableReactiveCassandraRepositories
public class CassandraConfiguration extends AbstractReactiveCassandraConfiguration {

  
  @Override
  protected String getContactPoints() {
    return "cassandra.us-east-2.amazonaws.com";
  }

  @Override
  protected int getPort() {
    return 9142;
  }

  @Override
  protected String getKeyspaceName() {
    return "windson";
  }

  @Override
  protected CqlSession getRequiredSession() {
    // TODO Auto-generated method stub
    DriverConfigLoader loader = DriverConfigLoader.fromClasspath("application.conf");

    return CqlSession.builder().withConfigLoader(loader).build();
  }
}

Solution

  • You need to increase connection timeout in the application.conf file to:

    datastax-java-driver {
      advanced.connection {
        connect-timeout = 5 seconds
        init-query-timeout = 5 seconds
      }
    }
    

    or upgrade to Java driver 4.9 where the init-query-timeout is already increased from 500 milliseconds to 5 seconds.