簡體   English   中英

如何在Spring-boot Couchbase中配置多個桶

[英]How to configure muliple bucket in spring-boot couchbase

我是不熟悉Couchbase和使用Couchbase 5.1的人。 單個spring boot應用程序的數據庫配置僅采用單個存儲桶名稱。

在春季引導中是否可以連接多個沙發床桶? 如果是,那我該如何實施呢?

這是我的代碼

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

 }

您可以使用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
  }
 }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM