Spring Boot 2.3.1.Release和Cassandra驱动程序超时问题

3

我正在升级到Spring Boot 2.3.1.Release版本。由于Java驱动程序升级为v4,Spring Data Cassandra发生了重大变化。应用程序启动时出现DriverTimeout异常,导致我陷入困境。

 com.datastax.oss.driver.api.core.DriverTimeoutException: [s0|control|id: 0x8572a9d7, L:/My_IPv4:Random_Port - R:/Cassandra_Server:Port] Protocol initialization request, step 1 (OPTIONS): timed out after 500 ms

我的Cassandra配置:

    @Bean(name = "mySession")
     @Primary
      public CqlSession session() {
        
         String containerIpAddress = getContactPoints();
         int containerPort = getPort();
         InetSocketAddress containerEndPoint = new InetSocketAddress(containerIpAddress, containerPort);
         
         return CqlSession.builder().withLocalDatacenter(getLocalDataCenter())
                .addContactPoint(containerEndPoint)
                .withAuthCredentials(dbProperties.getCassandraUserName(), dbProperties.getCassandraPassword())
                .withKeyspace(getKeyspaceName()).build();
    }

我还尝试使用DriverConfigLoader选项,通过显式设置连接超时时间来实现:

     @Bean(name = "mySession")
     @Primary
      public CqlSession session() {
        
         String containerIpAddress = getContactPoints();
         int containerPort = getPort();
         InetSocketAddress containerEndPoint = new InetSocketAddress(containerIpAddress, containerPort);
         
         DriverConfigLoader loader =
                 DriverConfigLoader.programmaticBuilder()
                     .withDuration(DefaultDriverOption.CONNECTION_CONNECT_TIMEOUT, Duration.ofSeconds(5))
                     .build();

         return CqlSession.builder().withLocalDatacenter(getLocalDataCenter())
                 .withConfigLoader(loader)
                .addContactPoint(containerEndPoint)
                .withAuthCredentials(dbProperties.getCassandraUserName(), dbProperties.getCassandraPassword())
                .withKeyspace(getKeyspaceName()).build();
    }

尝试过多种方法,但都无济于事,仍出现相同的异常。我当前使用的Spring Boot版本是2.2.0.Release,甚至没有指定任何超时时间,但它仍然正常工作。如何解决这个问题?


你解决了你的问题吗? - pixel
1个回答

0
假设您正在使用spring-data-cassandra。您可以通过应用程序属性完全配置它。
或者,您可以选择按照您现在的方式构建自己的会话。如果您选择这种方式,则可能需要关闭Spring的自动配置。
@EnableAutoConfiguration(exclude = {
    CassandraDataAutoConfiguration.class })

然后,不要构建CqlSession,而是使用CqlSessionFactory,如下所示:

  @Primary
  @Bean("mySessionFactory")
  public CqlSessionFactoryBean getCqlSession() {
    CqlSessionFactoryBean factory = new CqlSessionFactoryBean();
    factory.setUsername(userName);
    factory.setPassword(password);
    factory.setPort(port);
    factory.setKeyspaceName(keyspaceName);
    factory.setContactPoints(hostList);
    factory.setLocalDatacenter(datacenter);
    return factory;
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接