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

问题描述 投票:0回答:1

我正在使用弹簧数据反应性Cassandra。以下是我的配置类。我的期望是,当春季启动应用程序加载时,它必须连接到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:在com.company.domain.CassandraConfig中定义名为'reactiveCassandraTemplate'的bean时出错:通过工厂方法进行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:创建在com.company.domain.CassandraConfig中定义名为'reactiveSession'的bean时出错:通过工厂方法的Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.cassandra.ReactiveSession]:工厂方法'reactiveSession'抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:在com.company.domain.CassandraConfig中定义名为'session'的bean时出错:init方法的调用失败;嵌套异常是com.datastax.driver.core.exceptions.InvalidQueryException:Keyspace'mykeyspace'不存在

spring-data reactive-programming reactive spring-data-cassandra
1个回答
1
投票

最后通过以下代码修复它。我们需要在CassandraClusterFactoryBean上设置KeckCreations并启用@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));
}



}
© www.soinside.com 2019 - 2024. All rights reserved.