对于最新的稳定版本,请使用 Spring Data Couchbase 5.5.2spring-doc.cadn.net.cn

安装与配置

本章介绍使用库时所需的常见安装和配置步骤。spring-doc.cadn.net.cn

安装

所有用于生产用途的版本都分布在Maven Central和Spring版本存储库中。 因此,该库可以像任何其他 maven 依赖项一样包含在内:spring-doc.cadn.net.cn

兼容性

获得正确依赖关系的最简单方法是使用 spring initializr 制作一个项目父 Spring Boot Starter 工件具有所需的依赖项,无需指定它们。spring-doc.cadn.net.cn

Spring Boot 版本兼容性

Couchbase Java SDK 兼容性

配置

示例 1.通过 maven 包括依赖
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-couchbase</artifactId>
    <version>5.4.8</version>
</dependency>

这将拉入多个依赖项,包括底层 Couchbase Java SDK、常见的 Spring 依赖项以及作为 JSON 映射基础设施的 Jackson。spring-doc.cadn.net.cn

您还可以从 spring snapshot 存储库( https://repo.spring.io/snapshot )中获取快照,并从 spring 里程碑存储库( https://repo.spring.io/milestone )中获取里程碑版本。 以下是如何使用当前 SNAPSHOT 依赖项的示例:spring-doc.cadn.net.cn

快照配置

示例 2.使用快照版本
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-couchbase</artifactId>
  <version>${version}-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>https://repo.spring.io/snapshot</url>
</repository>

覆盖 Couchbase SDK 版本

一些用户可能希望使用与 Spring Data Couchbase 版本中引用的版本不同的 Couchbase Java SDK 版本,以获取错误和漏洞修复。由于Couchbase Java SDK次要版本版本向后兼容,因此此版本的Spring Data Couchbase与比发布依赖项中指定的版本更新的任何3.x版本的Couchbase Java SDK兼容并受支持。要更改 Spring Data Couchbase 使用的 Couchbase Java SDK 版本,只需覆盖应用程序pom.xml中的依赖项,如下所示:spring-doc.cadn.net.cn

示例 3.如果直接使用 spring-data-couchbase 依赖项
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-couchbase</artifactId>
  <version>${version}</version>
  <exclusions> <!-- exclude Couchbase Java SDK -->
    <exclusion>
      <groupId>com.couchbase.client</groupId>
      <artifactId>java-client</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency> <!-- add dependency for specific Couchbase Java SDK version -->
  <groupId>com.couchbase.client</groupId>
  <artifactId>java-client</artifactId>
  <version>3.4.7</version>
</dependency>
示例 4.如果使用 spring-data-starter-couchbase 依赖项(来自 Spring Initialzr)
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>x.y.z</version>
  <relativePath/>
</parent>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-couchbase</artifactId>
  <exclusions> <!-- exclude Couchbase Java SDK -->
    <exclusion>
      <groupId>com.couchbase.client</groupId>
      <artifactId>java-client</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency> <!-- add dependency for specific Couchbase Java SDK version -->
  <groupId>com.couchbase.client</groupId>
  <artifactId>java-client</artifactId>
  <version>3.4.7</version>
</dependency>

一旦你对类路径有了所有必需的依赖项,你就可以开始配置它了。 仅支持 Java 配置(XML 配置已在 4.0 中删除)。spring-doc.cadn.net.cn

基于注释的配置(“JavaConfig”)

要开始,您需要做的就是将AbstractCouchbaseConfiguration并实现抽象方法。spring-doc.cadn.net.cn

示例 5.扩展AbstractCouchbaseConfiguration
@Configuration
public class Config extends AbstractCouchbaseConfiguration {

    @Override
    public String getConnectionString() {
        return "couchbase://127.0.0.1";
    }

    @Override
    public String getUserName() {
        return "Administrator";
    }

    @Override
    public String getPassword() {
        return "password";
    }

    @Override
    public String getBucketName() {
        return "travel-sample";
    }
}

连接字符串由主机列表和可选方案 (couchbase://),如上面的代码所示。 您需要提供的只是要引导进入的 Couchbase 节点列表(由,).请注意,虽然一个 host 在开发中足够了,建议在这里添加 3 到 5 个 bootstrap 节点。Couchbase 将拾取所有节点 自动从集群中,但可能是你提供的唯一节点在 您正在启动应用程序。spring-doc.cadn.net.cn

userNamepassword通过 RBAC(基于角色的访问控制)在您的 Couchbase Server 集群中配置。 这bucketName反映要用于此配置的存储桶。spring-doc.cadn.net.cn

此外,可以通过重写configureEnvironment方法,该方法采用ClusterEnvironment.Builder返回已配置的ClusterEnvironment.spring-doc.cadn.net.cn

可以从此配置中自定义和覆盖更多内容作为自定义 Bean(例如,存储库、 验证和自定义转换器)。spring-doc.cadn.net.cn

如果您使用SyncGatewayCouchbaseMobile,您可能会遇到以 为前缀的字段的问题。 由于 Spring Data Couchbase 默认将类型信息存储为__class属性这可能会有问题。 覆盖typeKey()(例如,返回MappingCouchbaseConverter.TYPEKEY_SYNCGATEWAY_COMPATIBLE) 将 所述属性的名称。

如果您启动应用程序,您应该会在日志中看到 Couchbase INFO 级别的日志记录,这表明底层 Couchbase Java SDK 正在连接到数据库。如果报告任何错误,请确保给定的凭据 并且主机信息正确。spring-doc.cadn.net.cn

配置多个存储桶

要利用多存储桶存储库,请在 Config 类中实现以下方法。 config*OperationsMapping 方法配置实体对象到存储桶的映射。 小心方法名称 - 使用作为 Bean 的方法名称将导致使用该 Bean 的值而不是方法的结果。spring-doc.cadn.net.cn

此示例将 Person → protected、User → mybucket 映射,其他所有内容都映射到 getBucketName()。 请注意,这仅通过存储库映射调用。spring-doc.cadn.net.cn

@Override
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
 try {
  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()
 } catch (Exception e) {
  throw e;
 }
}
@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
 try {
  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()
 } 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 );
}