我添加了cassandra starter的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
但默认配置对我来说很差。
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'cassandraSession'
defined in class path resource
[org/springframework/boot/autoconfigure/data/cassandra/
CassandraDataAutoConfiguration.class]:
Invocation of init method failed; nested exception is
com.datastax.driver.core.exceptions.NoHostAvailableException:
All host(s) tried for query failed (tried: localhost/0:0:0:0:0:0:0:1:9042
(com.datastax.driver.core.exceptions.TransportException:
[localhost/0:0:0:0:0:0:0:1:9042] Cannot connect), localhost/127.0.0.1:9042
(com.datastax.driver.core.exceptions.TransportException:
[localhost/127.0.0.1:9042] Cannot connect))
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException:
All host(s) tried for query failed (tried: localhost/0:0:0:0:0:0:0:1:9042
(com.datastax.driver.core.exceptions.TransportException:
[localhost/0:0:0:0:0:0:0:1:9042]
Cannot connect), localhost/127.0.0.1:9042
(com.datastax.driver.core.exceptions.TransportException:
[localhost/127.0.0.1:9042] Cannot connect))
我希望spring应用程序在我没有配置'spring.data.cassandra。*'时不加载cassandra连接实例(比如cassandraSession)
能做什么?
You need to exclude CassandraDataAutoConfiguration to disable spring boot cassandra auto configuration e.g.
@SpringBootApplication
@EnableAutoConfiguration(exclude = { CassandraDataAutoConfiguration.class })
public class Application {
}
Then define your own cassandra configuration e.g.
@Configuration
@EnableReactiveCassandraRepositories
public class CassandraConfig extends AbstractReactiveCassandraConfiguration {
}
如果它工作正常,请告诉我!
结束定义我自己的自定义集群bean
@Configuration
@EnableReactiveCassandraRepositories
public class CassandraConfig extends AbstractReactiveCassandraConfiguration {
// read contact points from config
@Value("${spring.data.cassandra.contact-points}")
private String contactPoints;
@Override
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean bean = super.cluster();
bean.setContactPoints(contactPoints);
return bean;
}