Spring 即使 Cassandra 关闭,启动应用程序也应该启动

Spring Boot application should start even if Cassandra is down

我想确保我的应用程序即使在 Cassandra 关闭的情况下也能启动。 处理该问题的最佳方法是什么?

我正在使用 AbstractReactiveCassandraConfiguration 和多键空间实现。

    @Bean
    @NonNull
    @Override
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean cqlSessionFactoryBean = new CqlSessionFactoryBean();
        cqlSessionFactoryBean.setContactPoints(contactPoints);
        cqlSessionFactoryBean.setLocalDatacenter(localDataCenter);
        cqlSessionFactoryBean.setUsername(username);
        cqlSessionFactoryBean.setPassword(password);
        return cqlSessionFactoryBean;
    }

键空间在单独的键空间配置文件中设置。

键空间 1 和键空间 2 具有类似的配置。

@Configuration
@EnableReactiveCassandraRepositories(basePackages = "com.abc.repository.cassandra.xyz", reactiveCassandraTemplateRef = "keyspaceXYZTemplate")
public class XYZCassandraConfiguration extends CassandraConfig {

    @Override
    protected String getKeyspaceName() {
        return "KEYSPACE_NAME";
    }

    @Bean("keyspaceXYZTemplate")
    public ReactiveCassandraTemplate reactiveCassandraTemplate(@Qualifier("xyzBean") CqlSessionFactoryBean cqlSessionFactoryBean) {
        final ReactiveSession reactiveSession = new DefaultBridgedReactiveSession(cqlSessionFactoryBean.getObject());
        return new ReactiveCassandraTemplate(reactiveSession, cassandraConverter());
    }

    @NonNull
    @Override
    @Primary
    @Bean(name = "xyzBean")
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean cqlSessionFactoryBean = super.cassandraSession();
        cqlSessionFactoryBean.setKeyspaceName(getKeyspaceName());
        return cqlSessionFactoryBean;
    }
}

堆栈跟踪:当应用程序启动且 Cassandra 关闭时。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
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=70c95ec9): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=13=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.8.jar:5.3.8]

这是不可能的,因为您的应用依赖于在启动之前连接到数据库。

除非驱动程序连接到 Cassandra,否则无法实例化 bean。干杯!