使用 Spring 连接到 Couchbase 集群上的多个存储桶
Connecting to multiple buckets on Couchbase cluster with Spring
在我的项目中,我有一个 Couchbase 集群。对于不同的数据,我也有不同的存储桶。我想按照 Documentation 中指定的方式在 CouchbaseConfiguration
文件中配置它们,但我得到了 NullPointerException
.
我在 pom.xml
中使用 spring-data-couchbase
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>4.2.5</version>
</dependency>
这是我的 Couchbase 配置文件。
package com.example.cbase.configuration;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
@Configuration
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
@Override
public String getConnectionString() {
return "10.10.56.12,10.10.56.13,10.10.56.14";
}
@Override
public String getUserName() {
return "user1";
}
@Override
public String getPassword() {
return "password1!";
}
@Override
public String getBucketName() {
return null;
}
@Bean
public Bucket testCaseBucket(Cluster cluster) {
return cluster.bucket("testCaseBucket");
}
@Bean
public Bucket testRunBucket(Cluster cluster) {
return cluster.bucket("testRunBucket");
}
@Bean
public Bucket userLogBucket(Cluster cluster) {
return cluster.bucket("userLogBucket");
}
@Bean
public Bucket scheduleBucket(Cluster cluster) {
return cluster.bucket("scheduleBucket");
}
}
在您的配置中,您需要重写 configureReactiveRepositoryOperationsMapping() 和 configureRepositoryOperationsMapping()
您可以在
中找到它
git clone git@github.com:spring-projects/spring-data-couchbase.git
cd spring-data-couchbase
./src/test/java/org/springframework/data/couchbase/domain/Config.java
@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
ReactiveCouchbaseTemplate personTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName()
} catch (Exception e) {
throw e;
}
}
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
MappingCouchbaseConverter cvtr = (MappingCouchbaseConverter)baseMapping.getDefault().getConverter();
CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName()
} 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,
new JacksonTranslationService(), getDefaultConsistency());
}
// 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, new JacksonTranslationService(),
getDefaultConsistency());
}
// 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);
}
在我的项目中,我有一个 Couchbase 集群。对于不同的数据,我也有不同的存储桶。我想按照 Documentation 中指定的方式在 CouchbaseConfiguration
文件中配置它们,但我得到了 NullPointerException
.
我在 pom.xml
中使用spring-data-couchbase
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>4.2.5</version>
</dependency>
这是我的 Couchbase 配置文件。
package com.example.cbase.configuration;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
@Configuration
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
@Override
public String getConnectionString() {
return "10.10.56.12,10.10.56.13,10.10.56.14";
}
@Override
public String getUserName() {
return "user1";
}
@Override
public String getPassword() {
return "password1!";
}
@Override
public String getBucketName() {
return null;
}
@Bean
public Bucket testCaseBucket(Cluster cluster) {
return cluster.bucket("testCaseBucket");
}
@Bean
public Bucket testRunBucket(Cluster cluster) {
return cluster.bucket("testRunBucket");
}
@Bean
public Bucket userLogBucket(Cluster cluster) {
return cluster.bucket("userLogBucket");
}
@Bean
public Bucket scheduleBucket(Cluster cluster) {
return cluster.bucket("scheduleBucket");
}
}
在您的配置中,您需要重写 configureReactiveRepositoryOperationsMapping() 和 configureRepositoryOperationsMapping()
您可以在
中找到它git clone git@github.com:spring-projects/spring-data-couchbase.git
cd spring-data-couchbase
./src/test/java/org/springframework/data/couchbase/domain/Config.java
@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
ReactiveCouchbaseTemplate personTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName()
} catch (Exception e) {
throw e;
}
}
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
MappingCouchbaseConverter cvtr = (MappingCouchbaseConverter)baseMapping.getDefault().getConverter();
CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
(MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName()
} 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,
new JacksonTranslationService(), getDefaultConsistency());
}
// 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, new JacksonTranslationService(),
getDefaultConsistency());
}
// 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);
}