[英]Spring data with cassandra giving IllegalStateException
I'm completly new to cassandra, so my error might be obvious.我是 cassandra 的新手,所以我的错误可能很明显。
I'm trying to create an application with spring boot (version 2.3.0.M2) that contacts a cassandra (version 3.11.6) installed in localhost.我正在尝试使用 spring 引导(版本 2.3.0.M2)创建一个应用程序,该应用程序联系安装在本地主机中的 cassandra(版本 3.11.6)。
I've got an java.lang.IllegalStateException with the message: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter).我有一个 java.lang.IllegalStateException 消息:由于您提供了明确的联系点,因此必须明确设置本地 DC(请参阅配置中的 basic.load-balancing-policy.local-datacenter,或以编程方式设置它SessionBuilder.withLocalDatacenter)。 Current contact points are: Node(endPoint=localhost:9042, hostId=16a785a4-eaf3-4a4d-a216-5244d75206aa, hashCode=7b0b99d7)=datacenter1.
当前联系点为:Node(endPoint=localhost:9042, hostId=16a785a4-eaf3-4a4d-a216-5244d75206aa, hashCode=7b0b99d7)=datacenter1。 Current DCs in this cluster are: datacenter1
此集群中的当前 DC 是:datacenter1
My pom is the following one:我的 pom 是以下一个:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.M2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>cassandra</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cassandra</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
In the application code, I've got a the following configuration class:在应用程序代码中,我有以下配置 class:
public class CassandraConfig extends AbstractCassandraConfiguration {
public static final String KEYSPACE = "test_keyspace";
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
@Override
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
CreateKeyspaceSpecification specification = CreateKeyspaceSpecification.createKeyspace(KEYSPACE);
return Arrays.asList(specification);
}
@Override
protected List<DropKeyspaceSpecification> getKeyspaceDrops() {
return Arrays.asList(DropKeyspaceSpecification.dropKeyspace(KEYSPACE));
}
@Override
protected String getKeyspaceName() {
return KEYSPACE;
}
@Override
public String[] getEntityBasePackages() {
return new String[]{"com.test.cassandra.entity"};
}
}
My properties file contains the following configuration:我的属性文件包含以下配置:
spring.data.cassandara.keyspace-name=test_keyspace
spring.data.cassandra.contact-points=localhost
spring.data.cassanda.port=9042
spring.data.cassandra.schema-act=create_if_not_exists
I also have tried with我也试过
spring.data.cassandra.contact-points=dc1
When I execute the application, I got see from the logs that I'm using the following version DataStax Java driver for Apache Cassandra(R) (com.datastax.oss:java-driver-core) version 4.4.0当我执行应用程序时,我从日志中看到我正在使用以下版本的 DataStax Java 驱动程序 Apache Cassandra(R) (com.datastax.oss:java-driver-core) 版本 4.4.0
In the cassandra-rackdc.properties I've set the name to在 cassandra-rackdc.properties 中,我将名称设置为
dc=dc1
I've done several test adding configuration parameters, even adding an application.conf to the classpath as described datastax documentation, but didn't have any success.我已经完成了几个添加配置参数的测试,甚至按照 datastax 文档中的描述将 application.conf 添加到类路径中,但没有任何成功。 Any clue where should I do it?
任何线索我应该在哪里做?
Simply add只需添加
spring.data.cassandra.local-datacenter=DC1
to your application.properties file, where DC1 your local datacenter name (default DC1).到您的application.properties文件,其中 DC1 您的本地数据中心名称(默认 DC1)。
Since Spring Boot 2.3.0.X you need to explicitly provide the name of the local datacenter (because of the added Cassandra driver 4.X support which requires local datacenter to be set explicitly).从Spring Boot 2.3.0.X 开始,您需要显式提供本地数据中心的名称(因为添加的 Cassandra 驱动程序 4.X 支持需要显式设置本地数据中心)。 You can do this by overrirding the
getLocalDataCenter
method in AbstractSessionConfiguration
.您可以通过overrirding的做到这一点
getLocalDataCenter
的方法AbstractSessionConfiguration
。 AbstractCassandraConfiguration
extends this class so you just need to add in your CassandraConfig
class: AbstractCassandraConfiguration
扩展了这个类,所以你只需要在你的CassandraConfig
类中添加:
@Override
protected String getLocalDataCenter() {
return "dc1";
}
Run nodetool status
in the Cassandra shell.在 Cassandra shell 中运行
nodetool status
。 It gives you the datacenter name.它为您提供数据中心名称。
> root@d21e7b0dfc44:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 172.17.0.2 143.37 KiB 16 ? a3584baf-0665-474b-84e9-ba30528a7781 rack1
root@d21e7b0dfc44:/#
In my case, it is "datacenter1".就我而言,它是“datacenter1”。 This goes inside the properties file like so:
这进入属性文件,如下所示:
spring.data.cassandra.local-datacenter=datacenter1
There's actually a different config needed for Apache Cassandra to truly work with the new spring versions.实际上,Apache Cassandra 需要不同的配置才能真正与新的 spring 版本一起使用。 It took me about 50 hours of searching before finding this out on this documentation - https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.cassandra-java-config
我花了大约 50 个小时的搜索时间才在本文档中找到了这一点 - https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.cassandra-java-config
You'll need to create a new config file as specified there.您需要按照那里的规定创建一个新的配置文件。 To do this, follow thus:
为此,请按照以下步骤操作:
Create a new package named config
创建一个名为
config
的新包
Create a .java class file in the package named - CassandraConfig
在名为 -
CassandraConfig
的包中创建一个 .java 类文件
Copy and paste the below code into it.将以下代码复制并粘贴到其中。
@Configuration public class CassandraConfig { public @Bean CqlSession session() { return CqlSession.builder().withKeyspace("mykeyspacename").build(); } }
Note, change the Keyspace name to your Keyspace name defined in your property file.请注意,将 Keyspace 名称更改为您在属性文件中定义的 Keyspace 名称。 If you have not created a Keyspace, start Cassandra server on your system, login to the
cqlsh
on the terminal
or cmd
by issuing cqlsh
, and issue this command -如果您尚未创建密钥空间,在系统上启动卡桑德拉服务器,登录到
cqlsh
上terminal
或cmd
发出cqlsh
,并发出此命令-
CREATE KEYSPACE IF NOT EXISTS mykeyspacename WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 } AND DURABLE_WRITES = true ;
By now, when you start the spring boot app, it should run without any error.现在,当您启动 spring boot 应用程序时,它应该可以正常运行。
Its mentioned in your error logs "Current DCs in this cluster are: datacenter1"它在您的错误日志中提到“此集群中的当前 DC 是:datacenter1”
Just add spring.data.cassandra.local-datacenter=datacenter1 in application.properties只需在 application.properties 中添加 spring.data.cassandra.local-datacenter=datacenter1
I solved this problem by setting local datacenter like this :我通过像这样设置本地数据中心解决了这个问题:
application properties file : cassandra.local.datacenter=datacenter1
应用程序属性文件:
cassandra.local.datacenter=datacenter1
@Value("${cassandra.local.datacenter}")
private String localDatacenter;
@Bean
@Override
public CqlSessionFactoryBean cassandraSession() {
CqlSessionFactoryBean cqlSessionFactoryBean = new CqlSessionFactoryBean();
cqlSessionFactoryBean.setUsername(username);
cqlSessionFactoryBean.setPassword(password);
cqlSessionFactoryBean.setKeyspaceName(keySpace);
cqlSessionFactoryBean.setLocalDatacenter(localDatacenter);
cqlSessionFactoryBean.setPort(port);
cqlSessionFactoryBean.setContactPoints(contactPoint);
return cqlSessionFactoryBean;
}
Note : This is not all of the configuration code.注意:这不是配置代码的全部。 I just wanted to show local datacenter configuration by this code snippet.
我只想通过此代码片段显示本地数据中心配置。
Since Spring Boot 3.0 Cassandra properties' prefix is spring.cassandra.*
, not spring.data.cassandra.*
(specifically since 3.0.0-M5, as can be seen from @ConfigurationProperties.prefix
value here and here ), 由于 Spring Boot 3.0 Cassandra 属性的前缀是
spring.cassandra.*
,而不是spring.data.cassandra.*
(具体自 3.0.0-M5 起),从这里可以看到@ConfigurationProperties.prefix
as canProties
so you need to write所以你需要写
spring.cassandra.local-datacenter=datacenter1
in your application.properties
file to set local datacenter在您的
application.properties
文件中设置本地数据中心
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.