You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot连接Cassandra报错:cassandraSession创建失败及NoHostAvailableException

排查Spring Boot连接Cassandra时端口不匹配问题

以下是针对你遇到的BeanCreationException(因连接9042失败导致cassandraSession创建失败)的具体排查步骤:

  • 检查配置优先级与生效情况
    Spring Boot配置存在优先级,若仅在配置类中设置9142端口,但application.properties未显式指定,可能被自动配置的默认9042端口覆盖:

    1. 确保application.properties中添加配置:spring.data.cassandra.port=9142
    2. 验证配置类是否正确生效:配置类需标注@Configuration,创建会话时明确指定端口,示例代码:
      @Configuration
      public class CassandraConfig {
          @Bean
          public CqlSession cqlSession() {
              return CqlSession.builder()
                      .withContactPoint(new InetSocketAddress("你的Cassandra主机地址", 9142))
                      .withKeyspace(CqlIdentifier.fromCql("你的键空间名称"))
                      .build();
          }
      }
      
    3. 若使用Spring Data Cassandra自动配置,确保自定义配置类优先级高于自动配置,可通过@EnableCassandra注解绑定自定义配置。
  • 验证Cassandra节点实际监听端口
    nodetool status仅能确认节点状态,需确认节点实际监听的端口:

    1. 打开Cassandra安装目录下的conf/cassandra.yaml,检查native_transport_port值是否为9142,修改后需重启Cassandra节点。
    2. 通过命令验证端口监听:
      • Linux系统:netstat -tulpn | grep 9142
      • Windows系统:netstat -ano | findstr "9142"
  • 确认配置加载正确性
    启动项目时添加--debug参数,查看日志中Spring Boot加载的Cassandra配置项,确认spring.data.cassandra.port是否为9142,排查是否加载了错误的环境配置文件(如未指定环境时加载默认配置)。

  • 排查网络连通性
    确认Spring Boot应用所在机器能访问Cassandra节点的9142端口:

    • 使用telnet <Cassandra节点IP> 9142或nc -zv <Cassandra节点IP> 9142测试端口连通性,排除防火墙或网络策略拦截。

内容的提问来源于stack exchange,提问作者Ajay Kulkarni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:45:12