简体   繁体   中英

Beans for multiple Cassandra Clusters with RESTful Spring MVC

How do we connect to multiple Cassandra Clusters using Beans and how do we configure the Controller to use a specific cluster for an operation ? Do we keep these connections live ?

I have this CassandraOperations code:

@Configuration
@PropertySource(value = { "classpath:META-INF/cassandra.properties" })
@EnableCassandraRepositories(basePackages = { "com.rg" })
public class CassandraConfig {

    @Autowired
    private Environment environment;

    private static final Logger LOGGER = LoggerFactory.getLogger(CassandraConfig.class);

    @Bean
    public CassandraClusterFactoryBean cluster() {

        CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        cluster.setContactPoints(environment.getProperty("cassandra.contactpoints"));
        cluster.setPort(Integer.parseInt(environment.getProperty("cassandra.port")));
        return cluster;
    }

    @Bean
    public CassandraMappingContext mappingContext() {
        return new BasicCassandraMappingContext();
    }

    @Bean
    public CassandraConverter converter() {
        return new MappingCassandraConverter(mappingContext());
    }

    @Bean
    public CassandraSessionFactoryBean session() throws Exception {

        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
        session.setCluster(cluster().getObject());
        session.setKeyspaceName(environment.getProperty("cassandra.keyspace"));
        session.setConverter(converter());
        session.setSchemaAction(SchemaAction.NONE);
        return session;
    }

    @Bean
    public CassandraOperations cassandraTemplate() throws Exception {
        return new CassandraTemplate(session().getObject());
    }

}

Currently, your only option is configuring multiple CassandraTemplate instances that use multiple Session s. A Cassandra Session (and Cluster ) and a CassandraTemplate have a strong 1:1 relation.

This will change will Spring Data Cassandra 2.0 as we're going to add a SessionFactory interface that is pluggable regarding its implementation so you can define own routing. Additionally, we will add an AbstractRoutingSessionFactory that is similar to AbstractRoutingDataSource . See https://jira.spring.io/browse/DATACASS-330 for further details and leave your thoughts in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM