在我的应用程序中,使用以下技术
Spring boot 2.7.x
spring-boot-starter-data-cassandra 2.7.x
spring batch 5. x
java 11
Configuration
@EnableCassandraRepositories(
basePackages = {"com.package1"},
cassandraTemplateRef = "cassandraTemplate1",
repositoryFactoryBeanClass = CassandraRepositoryWithTtlFactoryBean.class
)
public class CassandraConfigurationCluster1 extends CassandraConfiguration {
Configuration
@EnableCassandraRepositories(
basePackages = {"com.package2"},
cassandraTemplateRef = "cassandraTemplate2",
repositoryFactoryBeanClass = CassandraRepositoryWithTtlFactoryBean.class
)
public class CassandraConfigurationCluster2 extends CassandraConfiguration {
我已经为
CassandraAdminTemplate
创建了两个bean(不确定我是否使用了 Spring Boot 2.2.x 中的 CassandraTemplate,但它在 2.7.x 中更改为其他内容
@Bean("session1")
public CqlSessionFactoryBean session() {
return new CqlSessionFactoryBean();
}
@Bean("cassandraTemplate1")
public CassandraAdminOperations cassandraTemplate(
@Qualifier("session1") final CqlSessionFactoryBean session) {
return new CassandraAdminTemplate(session.getObject(), cassandraConverter());
}
@Bean("session2")
public CqlSessionFactoryBean session() {
return new CqlSessionFactoryBean();
}
@Bean("cassandraTemplate2")
public CassandraAdminOperations cassandraTemplate(
@Qualifier("session2") final CqlSessionFactoryBean session) {
return new CassandraAdminTemplate(session.getObject(), cassandraConverter());
}
但我收到错误
通过字段“cassandraTemplate”表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [/config/CassandraConfiguration.class] 中定义的名为“cassandraTemplate”的 bean 时出错:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.cassandra.core.CassandraAdminTemplate]:工厂方法“cassandraTemplate”抛出异常;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建在类路径资源 [/config/CassandraConfiguration.class] 中定义的名称为“cassandraSessionFactory”的 bean 时出错:通过方法“cassandraSessionFactory”参数 0 表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有类型为“com.datastax.oss.driver.api.core.CqlSession”的合格 bean 可用:预期单个匹配 bean,但发现 3:session1、session2"、"thread ":"main","logger":"org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext"}
我知道这个错误,但我不知道我还应该使用它来管理两个单独的 Cassandra 连接
谢谢
我通过添加以下代码成功创建了两个独特的会话来连接到两个不同的密钥空间
@Configuration
@EnableCassandraRepositories(
basePackages = {"com.package1"},
cassandraTemplateRef = "cassandraTemplate1",
repositoryFactoryBeanClass = CassandraRepositoryWithTtlFactoryBean.class
)
public class CassandraConfigurationCluster1 extends CassandraConfiguration {
@Primary
@Bean("session1")
public SessionFactoryFactoryBean cassandraSessionFactory(CqlSession cqlSession) {
SessionFactoryFactoryBean bean = new SessionFactoryFactoryBean();
bean.setSession(cqlSession);
bean.setConverter((CassandraConverter)this.requireBeanOfType(CassandraConverter.class));
bean.setKeyspaceCleaner(this.keyspaceCleaner());
bean.setKeyspacePopulator(this.keyspacePopulator());
bean.setSchemaAction(this.getSchemaAction());
return bean;
}
@Bean("cassandraTemplate1")
public CassandraAdminOperations cassandraTemplate(
@Qualifier("session1") final SessionFactoryFactoryBean session) throws Exception {
return new CassandraAdminTemplate(session.getObject(), cassandraConverter());
}
第二场
@Configuration
@EnableCassandraRepositories(
basePackages = {"com.package2"},
cassandraTemplateRef = "cassandraTemplate2",
repositoryFactoryBeanClass = CassandraRepositoryWithTtlFactoryBean.class
)
public class CassandraConfigurationCluster2 extends CassandraConfiguration {
@Bean("session2")
public SessionFactoryFactoryBean cassandraSessionFactory(CqlSession cqlSession) {
SessionFactoryFactoryBean bean = new SessionFactoryFactoryBean();
bean.setSession(cqlSession);
bean.setConverter((CassandraConverter)this.requireBeanOfType(CassandraConverter.class));
bean.setKeyspaceCleaner(this.keyspaceCleaner());
bean.setKeyspacePopulator(this.keyspacePopulator());
bean.setSchemaAction(this.getSchemaAction());
return bean;
}
@Bean("cassandraTemplate2")
public CassandraAdminOperations cassandraTemplate(
@Qualifier("session2") final SessionFactoryFactoryBean session) throws Exception {
return new CassandraAdminTemplate(session.getObject(), cassandraConverter());
}
有了这个,我可以毫无问题地连接到两个集群