![](/img/trans.png)
[英]Spring boot data Cassandra throws com.datastax.oss.driver.api.core.DriverTimeoutException: query 'SELECT * FROM system.peers' timed out after PT0.5S
[英]Caused by: com.datastax.oss.driver.api.core.InvalidKeyspaceException: Invalid keyspace mykeyspace in Spring Boot Cassandra
我設計了一個使用 Spring 引導和 Cassandra 的項目,它在 Docker 容器上運行。
在我實現了 Cassandra 的配置后,我運行了這個項目,它拋出了如下所示的錯誤。
Caused by: com.datastax.oss.driver.api.core.InvalidKeyspaceException: Invalid keyspace mykeyspace
我該如何解決這個問題?
這是我的 application.properties 文件。
spring.cassandra.contactpoints=127.0.0.1
spring.cassandra.port=9042
spring.data.cassandra.keyspace-name=mykeyspace
spring.cassandra.basepackages=com.springboot.cassandra
這里是Cassandra的配置文件
@Configuration
@EnableCassandraRepositories
public class CassandraConfiguration extends AbstractCassandraConfiguration {
@Value("${spring.cassandra.contactpoints}")
private String contactPoint;
@Value("${spring.cassandra.port}")
private int port;
@Value("${spring.data.cassandra.keyspace-name}")
private String keyspaceName;
@Value("${spring.cassandra.basepackages}")
private String basePackages;
@Override
protected String getKeyspaceName() {
return keyspaceName;
}
@Override
protected int getPort() {
return port;
}
@Override
protected String getContactPoints() {
return contactPoint;
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
@Override
public String[] getEntityBasePackages() {
return new String[] {basePackages};
}
}
默認情況下 Spring 數據不會為您創建或更改架構。 這對於大多數用例來說都是一件好事,因為通常您不希望基於 java class 創建架構。 一般來說,改變會更糟,尤其是 Cassandra 也很難。
如果您想要 spring 創建它,您需要:
spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS
我仍然建議不要在生產中使用它。
然而,在談論鍵空間時,根據我的知識和文檔spring 的措辭,即使您使用上面的代碼,也不會創建鍵空間。 這對於 Cassandra 很有意義,因為密鑰空間需要復制策略和復制因子等信息,后者會因添加或刪除新數據中心等內容而更改。 這些是管理任務,不應該留給 Spring。
如果您可以刪除“-”字符,我認為您可以運行代碼錯誤為 0。
@Value("${spring.data.cassandra.keyspace-name}")
代替
@Value("${spring.data.cassandra.keyspacename}")
使用spring-boot
無需擴展AbstractCassandraConfiguration
,它可以為您自動配置 Cassandra。 您可以刪除您的配置 class ,一切都會正常工作。 但是,即使在這種情況下,您也需要做一些工作來自動創建密鑰空間。 我已經決定將自動配置添加到我們公司的 spring-boot 啟動器中,但您也可以在應用程序中將其定義為常規配置。
/**
* create the configured keyspace before the first cqlSession is instantiated. This is guaranteed by running this
* autoconfiguration before the spring-boot one.
*/
@ConditionalOnClass(CqlSession.class)
@ConditionalOnProperty(name = "spring.data.cassandra.create-keyspace", havingValue = "true")
@AutoConfigureBefore(CassandraAutoConfiguration.class)
public class CassandraCreateKeyspaceAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(CassandraCreateKeyspaceAutoConfiguration.class);
public CassandraCreateKeyspaceAutoConfiguration(CqlSessionBuilder cqlSessionBuilder, CassandraProperties properties) {
// It's OK to mutate cqlSessionBuilder because it has prototype scope.
try (CqlSession session = cqlSessionBuilder.withKeyspace((CqlIdentifier) null).build()) {
logger.info("Creating keyspace {} ...", properties.getKeyspaceName());
session.execute(CreateKeyspaceCqlGenerator.toCql(
CreateKeyspaceSpecification.createKeyspace(properties.getKeyspaceName()).ifNotExists()));
}
}
}
就我而言,我還添加了一個配置屬性來控制創建, spring.data.cassandra.create-keyspace
,如果您不需要靈活性,可以將其省略。
請注意, spring-boot
自動配置取決於某些配置屬性,這是我在開發環境中擁有的:
spring:
data:
cassandra:
keyspace-name: mykeyspace
contact-points: 127.0.0.1
port: 9042
local-datacenter: datacenter1
schema-action: CREATE_IF_NOT_EXISTS
create-keyspace: true
更多詳細信息: spring-boot 和 Cassandra
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.