如何在spring-boot couchbase中配置muliple桶

时间:2018-03-06 19:33:28

标签: spring-boot couchbase

我是沙发基地的新手,并使用了couchbase 5.1。单个spring引导应用程序,数据库配置只接受一个存储桶名称。

是否可以在弹簧靴中连接多个沙发靴桶?如果是,那我该如何实现呢?

这是我的代码

@Configuration
@EnableCouchbaseRepositories(basePackages = {"com.example" })
public class MyCouchbaseConfig extends AbstractCouchbaseConfiguration {

 @Override
 protected CouchbaseEnvironment getEnvironment() {
     CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().connectTimeout(150000).build();
     return env;
 }

 @Override
 protected List < String > getBootstrapHosts() {
     return Arrays.asList("localhost");
 }

 @Override
 protected String getBucketName() {
     return "student";
 }

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

 @Override
 public String typeKey() {
     return MappingCouchbaseConverter.TYPEKEY_SYNCGATEWAY_COMPATIBLE;
 }

 }

1 个答案:

答案 0 :(得分:1)

您可以使用com.couchbase.client.java.Cluster#openBucket创建尽可能多的存储桶,以便添加另一个存储桶,使您的类看起来像这样:

@Configuration
@EnableCouchbaseRepositories(basePackages = {
 "com.example"
})
public class MyCouchbaseConfig extends AbstractCouchbaseConfiguration {


 /* ... ALL YOUR CODE FROM EARLIER ... */


 @Bean
 public Bucket anotherBucket() throws Exception {
  return couchbaseCluster().openBucket("bucket2", "password");
 }

 // if using repositories you need to create another template and
 // override the entity<->template mapping
 @Bean
 public CouchbaseTemplate anotherTemplate() throws Exception {
  CouchbaseTemplate template = new CouchbaseTemplate(
   couchbaseClusterInfo(), anotherBucket(),
   mappingCouchbaseConverter(), translationService());
  template.setDefaultConsistency(getDefaultConsistency());
  return template;
 }

 @Override
 public void configureRepositoryOperationsMapping(
  RepositoryOperationsMapping baseMapping) {
  try {
   baseMapping.mapEntity(AnotherEntity.class, anotherTemplate());
  } catch (Exception e) {
   //custom Exception handling
  }
 }

}