Spring Boot连接Cassandra数据库报错,如何排查解决?
Spring Boot连接Cassandra数据库问题解决
问题场景
我是Spring Boot应用新手,尝试连接Cassandra数据库,已完成以下配置:
添加的依赖(pom.xml)
<dependency> <groupId>com.datastax.oss</groupId> <artifactId>java-driver-core</artifactId> <version>4.1.0</version> </dependency>
配置文件(application.yml)
spring: data: cassandra: keyspace-name: test-keyspace contact-points: localhost port: 9042 schemaAction: CREATE_IF_NOT_EXISTS request: timeout: 10s connection: connect-timeout: 10s locaDatacenter: datacenter1
运行应用时抛出错误:
Caused by: 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.UnsatisfiedDependencyException: Error creating bean with name 'cassandraSession' defined in class path resource [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cassandraSession' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cassandraSessionBuilder' defined in class path resource [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cassandraSessionBuilder' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraDriverConfigLoader' 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.config.DriverConfigLoader]: Factory method 'cassandraDriverConfigLoader' threw exception; nested exception is java.lang.NoSuchFieldError: CONNECTION_CONNECT_TIMEOUT
解决方案
1. 修复依赖版本冲突
手动引入的java-driver-core版本与Spring Boot自动配置依赖的驱动版本不兼容,导致字段找不到。改用Spring Boot官方提供的starter依赖,它会自动管理兼容的版本:
替换pom.xml中的依赖为:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-cassandra</artifactId> </dependency>
2. 修正配置项拼写错误
配置文件中的locaDatacenter拼写错误,改为local-datacenter(YAML推荐短横线命名),修正后的完整配置:
spring: data: cassandra: keyspace-name: test-keyspace contact-points: localhost port: 9042 schema-action: CREATE_IF_NOT_EXISTS request: timeout: 10s connection: connect-timeout: 10s local-datacenter: datacenter1
3. 确认Cassandra服务状态
确保本地Cassandra服务正在运行,端口9042可正常访问;如果test-keyspace不存在,schemaAction: CREATE_IF_NOT_EXISTS会自动创建该keyspace。
内容的提问来源于stack exchange,提问作者Nab
相关产品推荐
相关产品推荐

