使用RESTful Spring MVC的多个Cassandra集群的Bean

时间:2017-01-10 11:47:35

标签: java spring spring-mvc spring-data-cassandra

我们如何使用Beans连接到多个Cassandra集群?我们如何配置Controller以使用特定集群进行操作?我们是否保持这些联系?

我有这个CassandraOperations代码:

@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());
    }

}

1 个答案:

答案 0 :(得分:0)

目前,您唯一的选择是配置多个使用多个CassandraTemplate的{​​{1}}个实例。 Cassandra Session(和Session)和Cluster具有很强的1:1关系。

这将改变Spring Data Cassandra 2.0,因为我们将添加一个可插拔的CassandraTemplate接口,以便您可以定义自己的路由。此外,我们还会添加与AbstractRoutingDataSource类似的SessionFactory。有关详细信息,请参阅https://jira.spring.io/browse/DATACASS-330,并将您的想法留在那里。