我有 2 个项目(亲子)。父(spring)项目配置了一个 Cassandra CqlSession bean:
//package com.soumav.commonfw.configurations.cassandra
@Bean(name = "astraCassandraCqlSession")
@Primary
public CqlSession cqlSession() throws BusinessException {
CqlSession session = null;
try {
session = CqlSession.builder()
.withCloudSecureConnectBundle(Paths.get(ClassLoader.getSystemResource(astraConfigzipPath).toURI()))
.withAuthCredentials(clientId, clientSecret).build();
} catch (URISyntaxException e) {
log.error("Error Occured: {}", e.toString());
throw new BusinessException(e.toString(), HttpStatus.INTERNAL_SERVER_ERROR);
} catch (Exception e) {
log.error("Error Occured: {}", e.toString());
throw new BusinessException(e.toString(), HttpStatus.INTERNAL_SERVER_ERROR);
}
return session;
}
我将父(spring)项目作为依赖项(maven)添加到子(spring-boot)项目中,我在这里使用 Spring-Data-JPA。当我将孩子中的 cassandra 配置配置为:
//package com.soumav.test.astracassandraapp.config
@Configuration
@EnableCassandraRepositories(basePackages = "com.soumav.test.astracassandraapp.*")
public class TestCassandraConfig extends AbstractCassandraConfiguration {
@Value("${cassandra.keyspacename}")
String keyspacename;
@Autowired
@Qualifier("astraCassandraCqlSession")
CqlSession session;
@Override
protected CqlSession getRequiredSession() {
return this.session;
}
@Override
public String getKeyspaceName() {
return keyspacename;
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
@Bean
public CassandraAdminTemplate cassandraTemplate() {
session.setSchemaMetadataEnabled(false);
return new CassandraAdminTemplate(session);
}
@Bean
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
return new CassandraMappingContext();
}
}
我的 Spring-Boot(child) 应用程序类:
//package com.soumav.test.astracassandraapp
@SpringBootApplication(exclude = { CassandraAutoConfiguration.class,
CassandraDataAutoConfiguration.class }, scanBasePackages = "com.soumav.*")
public class TestAstraCassandraApplication {
public static void main(String[] args) {
SpringApplication.run(TestAstraCassandraApplication.class, args);
}
}
回购接口(子):
//package com.soumav.test.astracassandraapp.repo
@Repository
public interface TestRepo extends CassandraRepository<Student, Integer> {
}
当我运行子项目时,我得到一个异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraSession' defined in class path resource [com/soumav/test/astracassandraapp/config/TestCassandraConfig.class]: Invocation of init method failed; nested exception is com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node(endPoint=localhost:9042, hostId=null, hashCode=6a571b5d): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s1|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)]
依赖项已正确导入(mvn 树)。我还需要在子项目的自定义 cassandra 配置中覆盖什么?