Spring 启动应用程序 spring data cassandra 无法启动

Spring boot application with spring data cassandra failing to start

我有一个 spring 启动应用程序,其中 spring web & spring data cassandra 作为依赖项。 我在 class 中有一个主要方法,用 @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>

我 运行 此应用程序 & 运行 进入以下错误。

    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)]

我猜这可能是一些依赖相关的问题,但无法弄清楚到底是什么。我尝试了一些在 pom.xml 中评论的东西,但没有用。

问题出在与 cassandra 的连接上。确保您已在 cassandra 上创建了键空间名称。之后在你的项目中声明一个带有键空间的 Bean 配置,在我的例子中是 mykeyspace。您可以访问 https://kayaerol84.medium.com/cassandra-cluster-management-with-docker-compose-40265d9de076 了解有关设置密钥空间的更多详细信息。

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

我开发了一个关于 Cassandra 的 CRUD spring 引导应用程序的简单示例。你可以在我的 Github 上查看。下面是我的比较中的一些差异。

docker-compose.yml

version: "3"

services:
  cassandra:
    image: docker.io/bitnami/cassandra:latest
    ports:
      - '7000:7000'
      - '9042:9042'
    environment:
      - CASSANDRA_SEEDS=cassandra
      - CASSANDRA_PASSWORD_SEEDER=yes
#      - CASSANDRA_USERNAME=user
#      - CASSANDRA_PASSWORD=pass123
      - CASSANDRA_CFG_RACKDC_PREFER_LOCAL=true

application.properties

spring.data.cassandra.local-datacenter=datacenter1
spring.data.cassandra.keyspace-name=hello
spring.data.cassandra.contact-points=localhost
spring.data.cassandra.port=9042
spring.data.cassandra.username=cassandra
spring.data.cassandra.password=cassandra
spring.data.cassandra.schema-action=create_if_not_exists