【问题标题】:How to connect to mulitple couchbase clusters from spring application如何从 Spring 应用程序连接到多个 couchbase 集群
【发布时间】:2021-06-01 03:40:47
【问题描述】:

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

bucket1->集群1

bucket2 -> cluster2

有人可以帮忙吗?

【问题讨论】:

  • 欢迎来到 Stackoverflow 。正确阐述问题。

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


【解决方案1】:

其中一些已被注释掉,但在未注释时会起作用。它来自 spring-data-couchbase/src/test/java 中的 Config 类。从下往上看方法以了解机制。 请注意,我在许多方法前添加了“我的”,因为如果超类中有一个同名的 @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);
}

【讨论】:

    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 2018-02-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多