首页 > 解决方案 > 带有 cassandra 的 Spring 数据给出 IllegalStateException

问题描述

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

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

我有一个带有消息的 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

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

在应用程序代码中,我有以下配置类:

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"};
    }
}

我的属性文件包含以下配置:

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 驱动程序

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

dc=dc1

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

标签: spring-bootspring-data-cassandra

解决方案


只需添加

spring.data.cassandra.local-datacenter=DC1

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


推荐阅读