首页 文章

如何使用基于注释的配置在Couchbase中配置多个存储桶?

提问于
浏览
1

在Couchbase文档中,以下是配置环境的示例 . 如果有多个桶,会怎么做?

@Configuration
public class Config extends AbstractCouchbaseConfiguration {

    @Override
    protected List<String> getBootstrapHosts() {
        return Collections.singletonList("127.0.0.1");
    }

    @Override
    protected String getBucketName() {
        return "beer-sample";
    }

    @Override
    protected String getBucketPassword() {
        return "";
    }
}

1 回答

  • 0

    对于 2.0.x branch中的多个存储桶,它当前的工作方式是你必须实例化第二个 Bucket bean和相关的 CouchbaseTemplate (这是最难的部分):

    //we want all User objects to be stored in a second bucket
    //let's define the bucket reference...
    @Bean
    public Bucket userBucket() {
      return couchbaseCluster().openBucket("users", "");
    }
    
    //... then the template (inspired by couchbaseTemplate() method)...
    @Bean
    public CouchbaseTemplate userTemplate() {
      CouchbaseTemplate template = new CouchbaseTemplate(
        couchbaseClusterInfo(), //reuse the default bean
         userBucket(), //the bucket is non-default
        mappingCouchbaseConverter(), translationService() //default beans here as well
      );
      template.setDefaultConsistency(getDefaultConsistency());
      return template;
    }
    

    在那之后,你'll probably also want some of your repositories to use this second template (and bucket). There'目前也是一个实现( RepositoryOperationsMapping ),但它可能会改变一点直到即将到来的 2.0.0-RC 所以我不会在那里详细说明 .

相关问题