使用多个键空间配置spring Cassandra

时间:2018-03-10 06:50:44

标签: spring spring-boot cassandra spring-data-cassandra

我正在尝试在春季启动应用程序中创建多个Spring Cassandra会话

  • spring.data.cassandra.keyspace-name
  • 中配置的一个会话连接到主要Keysapce
  • 一个会话连接到keyspaceB

配置

@Configuration
// all the sessions have spring Cassandra configuration except keyspace
public class CassandraConfig extends AbstractCassandraConfiguration {

    @Value("${spring.data.cassandra.keyspace-name}")
    private String primaryKeysapce;

    @Value("${item.keysapce.b:keyspaceB}")
    private String keyspaceB;


    @Override
    @Primary
    @Bean
    public CassandraSessionFactoryBean session() throws ClassNotFoundException {
        return super.session();
    }

    @Bean("cassandraOperations")
    @Primary
    public CassandraOperations cassandraOperations() throws ClassNotFoundException {
        return new CassandraTemplate(session().getObject(), cassandraConverter());
    }

    @Bean("sessionB")
    public CassandraSessionFactoryBean sessionB() throws ClassNotFoundException {
        CassandraSessionFactoryBean session = super.session();
        session.setKeyspaceName(keyspaceB);
        return session;
    }

    @Bean("cassandraOperationsB")
    public CassandraOperations cassandraOperationsB() throws ClassNotFoundException {
        return new CassandraTemplate(sessionB().getObject(), cassandraConverter());
    }


    @Override
    protected String getKeyspaceName() {
        return primaryKeysapce;
    }

}

application.properties

spring.data.cassandra.keyspace-name = primary_keysapce
spring.data.cassandra.username = xyz
spring.data.cassandra.password = abcd
spring.data.cassandra.contact-points=myhost.company.com

在我的代码中,

@SpringBootApplication
@EnableAutoConfiguration
public class DemoApplication implements CommandLineRunner {


    @Resource(name = "cassandraOperationsB")
    CassandraOperations cassandraOperationsB;

    // hall point to primary one as per spring properties
    @Autowired
    CassandraOperations cassandraOperations;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

虽然它在localhost中按键空间正常工作。

但这不符合预期。它没有在application.properties中使用spring cassandra属性。它始终指向默认属性。

我想在myhost.company.com上连接到cassandra,但我的应用程序一直在连接到localhost。

有人可以帮我解决这个错误吗?

0 个答案:

没有答案