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

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

我添加了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)

能做什么?

spring-boot spring-data cassandra-3.0 spring-data-cassandra
2个回答
1
投票
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 {
}

如果它工作正常,请告诉我!


0
投票

结束定义我自己的自定义集群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

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