微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

bucket1-> cluster1

bucket2 -> cluster2

有人可以帮忙吗?

解决方法

其中一些已被注释掉,但在取消注释时仍然有效。它来自 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"),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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。