【问题标题】:DriverTimeoutException: Query timed out after PT2S. Unable to set spring.data.cassandra.request.timeout property?DriverTimeoutException: PT2S 后查询超时。无法设置 spring.data.cassandra.request.timeout 属性?
【发布时间】:2021-03-12 08:11:43
【问题描述】:

在启动我的应用程序时,总是会创建键空间,并且可能会在 PT2S 错误消息之前创建一两个表。不知何故 spring.data.cassandra.request.timeout 属性不被尊重,或者我的配置有问题? “DriverConfigLoaderBuilderCustomizer” bean 没有任何区别。

pom.xml

<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<spring.framework.version>5.3.1</spring.framework.version>

application.yml

spring:
  data:
    cassandra:
      port: 9042
      keyspace-name: abc
      contact-points: localhost
      local-datacenter: datacenter1
      replication-factor: 1
      request:
        timeout: 15s
      connection:
        init-query-timeout: 15s

CassandraConfig.java

@Configuration
@EnableReactiveCassandraRepositories(basePackages = "a.b.c.repository")
public class CassandraConfig extends AbstractReactiveCassandraConfiguration {
    @Value("${spring.data.cassandra.contactpoints}")
    .
    .
    @Override
    protected String getKeyspaceName() {
        return keyspace;
    }
    @Override protected String getContactPoints() {
        return contactPoints;
    }
    @Override protected int getPort() {
        return port;
    }
    @Override
    protected String getLocalDataCenter() {
        return datacenter;
    }
   @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.NONE;
    }
    @Override
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
        return Collections.singletonList(CreateKeyspaceSpecification.createKeyspace(getKeyspaceName())
                .ifNotExists()
                .with(KeyspaceOption.DURABLE_WRITES, true)
                .withNetworkReplication(DataCenterReplication.of(getLocalDataCenter(), getReplicationFactor())));
    }
    @Override
    protected KeyspacePopulator keyspacePopulator() {
        ResourceKeyspacePopulator keyspacePopulate = new ResourceKeyspacePopulator();
        keyspacePopulate.addScript(new ClassPathResource("table-schema.cql"));
        return keyspacePopulate;
    }
    private long getReplicationFactor() {
        return replicationFactor;
    }
    //@Bean
    //public DriverConfigLoaderBuilderCustomizer driverConfigLoaderBuilderCustomizer() {
        //return loaderBuilder -> loaderBuilder
                //.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(15))
                //.withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofSeconds(15));
    //}

}

修剪的错误日志

BeanCreationException: Error creating bean with name 'cassandraSessionFactory' 
Invocation of init method failed; nested exception is ScriptStatementFailedException: Failed to execute CQL script statement #2 of class path resource [table-schema.cql]: CREATE TABLE IF NOT EXISTS mytable...

Caused by: org.springframework.data.cassandra.core.cql.session.init.ScriptStatementFailedException: Failed to execute CQL script statement #2 of class path resource [table-schema.cql]: CREATE TABLE IF NOT EXISTS mytanble... nested exception is com.datastax.oss.driver.api.core.DriverTimeoutException: Query timed out after PT2S
    at org.springframework.data.cassandra.core.cql.session.init.ScriptUtils.executeCqlScript(ScriptUtils.java:555) ~[spring-data-cassandra-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.cassandra.core.cql.session.init.ResourceKeyspacePopulator.populate
    
Caused by: com.datastax.oss.driver.api.core.DriverTimeoutException: Query timed out after PT2S
at com.datastax.oss.driver.api.core.DriverTimeoutException.copy(DriverTimeoutException.java:34)

【问题讨论】:

    标签: cassandra spring-data-cassandra


    【解决方案1】:

    我们遇到了类似的问题,并首先尝试了您的确切步骤。 driver examples code 让我们朝着正确的方向前进。 我们有一个自定义 cassandra 配置,其中排除了自动配置:

    @SpringBootApplication(exclude = {CassandraAutoConfiguration.class,CassandraDataAutoConfiguration.class})
    

    运行 spring-boot-starter-parent 版本 2.3.0.RELEASE。

    Cassandra 配置:

    @Configuration
    @EnableCassandraRepositories(basePackages = {"com.example.model.cassandra"})
    public class CassandraConfig extends AbstractCassandraConfiguration {
    
    @Override
    protected SessionBuilderConfigurer getSessionBuilderConfigurer() {
        return new SessionBuilderConfigurer() {
            @Override
            public CqlSessionBuilder configure(CqlSessionBuilder cqlSessionBuilder) {
                return cqlSessionBuilder
                        .withConfigLoader(DriverConfigLoader.programmaticBuilder().withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(15000)).build());
            }
        };
    }
    }
    

    【讨论】:

    • 为什么它对我不起作用。如您所说,我删除了所有自动配置。但是无论我在“DriverConfigLoader”上设置什么时间,仍然总是出现超时错误。我坚持了 2 天,仍然无法解决这个问题。我正在使用 spring boot 2.4.4 和 java driver 4.9(托管)
    • 为什么需要为此排除自动配置?如果您从父 cassandra 配置扩展,您已经在进行完整的用户配置。不会使用任何 Spring Boot 的定制器,因为自动配置无论如何都不适用。如果有一些不支持的东西,你应该让 Spring Boot 做它的事情并创建一个问题。
    • 我做了所有我应该做的事情。一旦我删除并完成了用户配置,我仍然面临着这个问题。然后我删除了所有东西,让 spring boot 为自己做事,但仍然面临同样的问题。这里给出的解决方案对我来说看起来不错,但根本不起作用。我在这里发布了这个问题stackoverflow.com/questions/67217692/…
    猜你喜欢
    • 2020-11-17
    • 2021-07-16
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多