Spring 数据反应 Cassandra 未在启动时创建键空间 - InvalidQueryException:键空间 'xxx' 不存在

Spring data reactive Cassandra not creating keyspace on startup - InvalidQueryException: Keyspace 'xxx' does not exist

我正在使用 spring 数据反应式 Cassandra。下面给出的是我的配置 class。我的期望是,当 spring 启动应用程序加载时,它必须连接到 Cassandra 并创建密钥空间。但是我的应用程序能够连接到 Cassandra,但它没有创建密钥空间。我在启动时收到以下异常。如果我遗漏了什么,请告诉我。

import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.AbstractReactiveCassandraConfiguration;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.DropKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption;
import org.springframework.data.cassandra.repository.config.EnableReactiveCassandraRepositories;

import java.util.Arrays;
import java.util.List;



@Configuration
@EnableReactiveCassandraRepositories
public class CassandraConfig extends AbstractReactiveCassandraConfiguration {
    @Value("${spring.data.cassandra.contactpoints}") private String contactPoints;
    @Value("${spring.data.cassandra.port}") private int port;
    @Value("${spring.data.cassandra.keyspace-name}") private String keyspace;

    @Value("${spring.data.cassandra.username}") private String userName;
    @Value("${spring.data.cassandra.password}") private String password;
    @Value("${cassandra.basepackages}") private String basePackages;


    @Override protected String getKeyspaceName() {
        return keyspace;
    }
    @Override protected String getContactPoints() {
        return contactPoints;
    }
    @Override protected int getPort() {
        return port;
    }
    @Override public SchemaAction getSchemaAction() {
        return SchemaAction.CREATE_IF_NOT_EXISTS;
    }
    @Override public String[] getEntityBasePackages() {
        return new String[] {
                basePackages
        };
    }

    @Override
    public CassandraClusterFactoryBean cluster() {
        PlainTextAuthProvider authProvider = new PlainTextAuthProvider(userName, password);

        CassandraClusterFactoryBean cluster=new CassandraClusterFactoryBean();

        cluster.setJmxReportingEnabled(false);
        cluster.setContactPoints(contactPoints);
        cluster.setPort(port);
        cluster.setAuthProvider(authProvider);
        cluster.setReconnectionPolicy(new ConstantReconnectionPolicy(1000));

        return cluster;
    }



    @Override
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {

        CreateKeyspaceSpecification specification = CreateKeyspaceSpecification.createKeyspace(keyspace)
                .ifNotExists()
                .with(KeyspaceOption.DURABLE_WRITES, true);

        return Arrays.asList(specification);
    }


    @Override
    protected List<DropKeyspaceSpecification> getKeyspaceDrops() {
        return Arrays.asList(DropKeyspaceSpecification.dropKeyspace(keyspace));
    }

}

通过构造函数参数 5 表示的未满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“myRepository”的 bean 时出错:设置 bean 属性 'reactiveCassandraOperations' 时无法解析对 bean 'reactiveCassandraTemplate' 的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'reactiveCassandraTemplate' 的 bean 在 com.company.domain.CassandraConfig 中定义时出错:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.cassandra.core.ReactiveCassandraTemplate]:工厂方法 'reactiveCassandraTemplate' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'reactiveSessionFactory' 的 bean 在 com.company.domain.CassandraConfig 中定义时出错:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.cassandra.ReactiveSessionFactory]:工厂方法 'reactiveSessionFactory' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'reactiveSession' 的 bean 在 com.company.domain.CassandraConfig 中定义时出错:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.cassandra.ReactiveSession]:工厂方法 'reactiveSession' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'session' 的 bean 在 com.company.domain.CassandraConfig 中定义时出错:调用 init 方法失败;嵌套异常是 com.datastax.driver.core.exceptions.InvalidQueryException:键空间“mykeyspace”不存在

终于通过下面的代码修复了它。我们需要在 CassandraClusterFactoryBean 上设置 KeyspaceCreations 并启用 @ComponentScan。

import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.*;
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.DropKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption;
import org.springframework.data.cassandra.repository.config.EnableReactiveCassandraRepositories;

import java.util.Arrays;
import java.util.List;



@Configuration
@EnableReactiveCassandraRepositories(basePackages = "com.company.domain.data")
public class CassandraConfig extends AbstractReactiveCassandraConfiguration{
@Value("${spring.data.cassandra.contactpoints}") private String contactPoints;
@Value("${spring.data.cassandra.port}") private int port;
@Value("${spring.data.cassandra.keyspace-name}") private String keyspace;

@Value("${spring.data.cassandra.username}") private String userName;
@Value("${spring.data.cassandra.password}") private String password;
@Value("${cassandra.basepackages}") private String basePackages;


@Override protected String getKeyspaceName() {
    return keyspace;
}
@Override protected String getContactPoints() {
    return contactPoints;
}
@Override protected int getPort() {
    return port;
}
@Override public SchemaAction getSchemaAction() {
    return SchemaAction.CREATE_IF_NOT_EXISTS;
}
@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.company.domain.data"};
}

@Override
public CassandraClusterFactoryBean cluster() {
    PlainTextAuthProvider authProvider = new PlainTextAuthProvider(userName, password);

    CassandraClusterFactoryBean cluster=new CassandraClusterFactoryBean();

    cluster.setJmxReportingEnabled(false);
    cluster.setContactPoints(contactPoints);
    cluster.setPort(port);
    cluster.setAuthProvider(authProvider);
    cluster.setKeyspaceCreations(getKeyspaceCreations());
    cluster.setReconnectionPolicy(new ConstantReconnectionPolicy(1000));

    return cluster;
}


@Override
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {

    CreateKeyspaceSpecification specification = CreateKeyspaceSpecification.createKeyspace(keyspace)
            .ifNotExists()
            .with(KeyspaceOption.DURABLE_WRITES, true);

    return Arrays.asList(specification);
}



@Override
protected List<DropKeyspaceSpecification> getKeyspaceDrops() {
    return Arrays.asList(DropKeyspaceSpecification.dropKeyspace(keyspace));
}



}