如何解决Spring Boot 2.3.1.Release和Cassandra DriverTimeout问题
我正在升级到Spring引导版本2.3.1。因此,由于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引导版本是2.2.0.Release,我什至没有在其中指定任何超时时间并且可以正常工作。该问题如何解决?
解决方法
假设您正在使用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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。