【问题标题】:Spring Boot 2.3.1.Release and Cassandra DriverTimeout issueSpring Boot 2.3.1.Release 和 Cassandra DriverTimeout 问题
【发布时间】:2020-12-02 07:39:03
【问题描述】:

我正在升级到 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,我什至没有在那里指定任何超时并且它工作正常。如何解决这个问题?

【问题讨论】:

  • 您的问题解决了吗?

标签: java spring-boot datastax-java-driver spring-data-cassandra


【解决方案1】:

假设您使用的是 spring-data-cassandra。您可以完全通过应用程序属性对其进行配置。

或者,您也可以按照自己的路线走并建立自己的会话。如果你这样做,你可能想要关闭 Spring 的自动配置。

@EnableAutoConfiguration(exclude = {
    CassandraDataAutoConfiguration.class })

然后使用 CqlSessionFactory 代替构建 CqlSession:

  @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;
  }

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 2020-02-25
    • 1970-01-01
    • 2019-03-14
    • 2021-03-02
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多