如何忽略 spring-boot-cassandra 默认配置来加载 cassandra 连接实例

Posted

技术标签:

【中文标题】如何忽略 spring-boot-cassandra 默认配置来加载 cassandra 连接实例【英文标题】:How to ignore the spring-boot-cassandra default config to load the cassandra connection instance 【发布时间】:2019-03-29 12:28:25 【问题描述】:

我已经添加了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.data.cassandra.*'时,我希望spring应用程序不会加载cassandra连接实例(如cassandraSession)

有什么办法?

【问题讨论】:

【参考方案1】:

您需要排除 CassandraDataAutoConfiguration 以禁用 spring boot cassandra 自动配置,例如

@SpringBootApplication
@EnableAutoConfiguration(exclude =  CassandraDataAutoConfiguration.class )
public class Application 

然后定义您自己的 Cassandra 配置,例如

@Configuration
@EnableReactiveCassandraRepositories
public class CassandraConfig extends AbstractReactiveCassandraConfiguration 

【讨论】:

谢谢,仅排除默认配置即可解决问题,因为它试图在没有身份验证详细信息的情况下进行连接。 我不得不排除 CassandraAutoConfiguration.class 而不是 CassandraDataAutoConfiguration.class【参考方案2】:

最终定义了我自己的自定义集群 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;
    

https://github.com/spring-projects/spring-data-cassandra/blob/f3115017d4a04e105d4046f6fd716ac308ecd7aa/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/AbstractClusterConfiguration.java#L88

【讨论】:

以上是关于如何忽略 spring-boot-cassandra 默认配置来加载 cassandra 连接实例的主要内容,如果未能解决你的问题,请参考以下文章

如何忽略其中已经忽略的部分html代码[重复]

如何评论为啥可以忽略捕获

如何忽略.idea/workspace.xml

如何忽略“idea/workspace.xml”?

如何在 TortoiseSVN 中取消忽略文件?

如何忽略已在 Tweepy 中转发的推文?