[英]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.