如何从spring应用程序连接到多个couchbase集群



我需要从我的spring应用程序连接到分别位于集群1和集群2中的bucket1和bucket2

桶1->集群1

桶2->集群2

有人能帮忙吗?

其中一些已被注释掉,但在取消注释时会起作用。它来自spring数据couchbase/src/test/java中的Config类。从下往上看这些方法,以了解其机制。注意,我在许多方法前面加了"my",因为如果超类中有一个同名的@Bean方法,那么将使用该Bean的值,而不是执行该方法的结果。myCouchbaseClientFactory((将使用指定的参数创建一个couchbaseClientFactory。myCouchbaseTemplate/myCreactiveCouchbaseTemplate可以使用该结果来制作模板。两个configure*RespositoryOperationsMapping方法可以使用这些模板来映射存储库操作。

@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// ReactiveCouchbaseTemplate personTemplate =
// myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
}
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
}
}
// do not use reactiveCouchbaseTemplate for the name of this method, otherwise the value of that bean
// will be used instead of the result of this call (the client factory arg is different)
public ReactiveCouchbaseTemplate myReactiveCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
MappingCouchbaseConverter mappingCouchbaseConverter) {
return new ReactiveCouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter);
}
// do not use couchbaseTemplate for the name of this method, otherwise the value of that been
// will be used instead of the result from this call (the client factory arg is different)
public CouchbaseTemplate myCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
MappingCouchbaseConverter mappingCouchbaseConverter) {
return new CouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter);
}
// do not use couchbaseClientFactory for the name of this method, otherwise the value of that bean will
// will be used instead of this call being made ( bucketname is an arg here, instead of using bucketName() )
public CouchbaseClientFactory myCouchbaseClientFactory(String bucketName) {
return new SimpleCouchbaseClientFactory(getConnectionString(), authenticator(), bucketName);
}

最新更新