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

时间:2021-06-01 03:40:47

标签: java spring spring-boot couchbase spring-data-couchbase

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

bucket1-> cluster1

bucket2 -> cluster2

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

其中一些已被注释掉,但在取消注释时仍然有效。它来自 spring-data-couchbase/src/test/java 中的 Config 类。自下而上地查看方法以了解其机制。 请注意,我在许多方法前加上了 'my',因为如果超类中有一个同名的 @Bean 方法,那么将使用该 bean 的值而不是执行该方法的结果。 myCouchbaseClientFactory() 将创建一个带有指定参数的 couchbaseClientFactory。 结果可以被 myCouchbaseTemplate/myReactiveCouchbaseTemplate 用来制作模板。 两个 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);
}