带有 cassandra 的 Spring 数据给出 IllegalStateException [英] Spring data with cassandra giving IllegalStateException

查看:36
本文介绍了带有 cassandra 的 Spring 数据给出 IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 cassandra 完全陌生,所以我的错误可能很明显.

I'm completly new to cassandra, so my error might be obvious.

我正在尝试使用 Spring Boot(版本 2.3.0.M2)创建一个应用程序,该应用程序与安装在 localhost 中的 cassandra(版本 3.11.6)联系.

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.

我有一个带有消息的 java.lang.IllegalStateException:由于您提供了明确的联系点,因此必须明确设置本地 DC(请参阅配置中的 basic.load-balancing-policy.local-datacenter,或使用 SessionBuilder.withLocalDatacenter 以编程方式设置).目前的联系点是:Node(endPoint=localhost:9042, hostId=16a785a4-eaf3-4a4d-a216-5244d75206aa, hashCode=7b0b99d7)=datacenter1.该集群中的当前 DC 是:datacenter1

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). Current contact points are: Node(endPoint=localhost:9042, hostId=16a785a4-eaf3-4a4d-a216-5244d75206aa, hashCode=7b0b99d7)=datacenter1. Current DCs in this cluster are: datacenter1

我的 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:

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

我也试过

spring.data.cassandra.contact-points=dc1

当我执行应用程序时,我从日志中看到我使用的是以下版本适用于 Apache Cassandra(R) (com.datastax.oss:java-driver-core) 4.4.0 版的 DataStax Java 驱动程序

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

在 cassandra-rackdc.properties 中,我将名称设置为

In the cassandra-rackdc.properties I've set the name to

dc=dc1

我已经完成了几个添加配置参数的测试,甚至如 datastax 文档中描述的那样将 application.conf 添加到类路径,但没有任何成功.任何线索我应该在哪里做?

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. Any clue where should I do it?

推荐答案

只需添加

spring.data.cassandra.local-datacenter=DC1

到您的 application.properties 文件,其中 DC1 您的本地数据中心名称(默认 DC1).

to your application.properties file, where DC1 your local datacenter name (default DC1).

这篇关于带有 cassandra 的 Spring 数据给出 IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆