Couchbase

Module 5. Spring
Level 17 , Lesson 9
Available

Couchbase is an open source, document-oriented, distributed, multi-model NoSQL database optimized for interactive applications. In addition, Spring Boot provides auto-configuration for Couchbase and abstractions provided through Spring Data Couchbase. For convenient collection of dependencies, there are “starter packages” spring-boot-starter-data-couchbase and spring-boot-starter-data-couchbase-reactive

Connect to Couchbase

You can get a Cluster by adding the Couchbase SDK and some configuration. The spring.couchbase.* properties can be used to configure the connection. As a rule, connection string is specified, username and password, as shown in the following example:

Properties
spring.couchbase.connection-string=couchbase://192.168.1.123
spring.couchbase.username=user
spring.couchbase.password=secret
Yaml
spring:
  couchbase:
    connection-string: "couchbase://192.168.1.123"
    username: "user"
    password: "secret"

You can also configure some ClusterEnvironment parameters. For example, the following configuration changes the timeout setting used to open a new Bucket and also enables SSL protocol support:

Properties
spring.couchbase.env.timeouts.connect=3s
spring.couchbase.env.ssl.key-store=/location/of/keystore.jks
spring.couchbase.env.ssl.key-store-password=secret
Yaml
spring:
  couchbase:
    env:
      timeouts:
        connect: "3s"
      ssl:
        key-store: "/location/of/keystore.jks"
        key-store-password: "secret"
See the spring.couchbase.env.* properties for more details. To gain more control, you can use one or more ClusterEnvironmentBuilderCustomizer beans.

Spring Data Couchbase Repositories

Spring Data includes repository support for Couchbase.

An auto-configurable CouchbaseTemplate instance can be implemented just like any other Spring bean, as long as the CouchbaseClientFactory bean is present. This is possible if you have the Cluster described above, and the name of the bucket is given:

Properties
spring.data.couchbase.bucket-name=my-bucket
Yaml
spring:
  data:
    couchbase:
      bucket-name: "my-bucket"

The following examples show how to implement the CouchbaseTemplate bean:

Java
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
    private final CouchbaseTemplate template;
    public MyBean(CouchbaseTemplate template) {
        this.template = template;
    }
 // ...
 public String someMethod() {
        return this.template.getBucketName();
    }
}
Kotlin
import org.springframework.data.couchbase.core.CouchbaseTemplate
import org.springframework.stereotype.Component
@Component
class MyBean(private val template: CouchbaseTemplate) {
 // ...
 fun someMethod(): String {
        return template.bucketName
    }
}

There are several beans that you can define in your own configuration to override those provided by autoconfiguration:

  • A CouchbaseMappingContext bean marked with the @Bean annotation, named couchbaseMappingContext.

  • A CustomConversions bean marked with a @Bean annotation named couchbaseCustomConversions.

  • A CouchbaseTemplate bean marked with the @Bean annotation, named couchbaseTemplate.

To avoid hard-coding these names in your own configuration, you can use the BeanNames provided by Spring Data Couchbase. For example, you can configure the converters you use as follows:

Java
import org.assertj.core.util.Arrays;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.BeanNames;
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions;
@Configuration(proxyBeanMethods = false)
public class MyCouchbaseConfiguration {
    @Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
    public CouchbaseCustomConversions myCustomConversions() {
        return new CouchbaseCustomConversions(Arrays.asList(new MyConverter()));
    }
}
Kotlin
import org.assertj.core.util.Arrays
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.couchbase.config.BeanNames
import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions
@Configuration(proxyBeanMethods = false)
class MyCouchbaseConfiguration {
    @Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
    fun myCustomConversions(): CouchbaseCustomConversions {
        return CouchbaseCustomConversions(Arrays.asList(MyConverter()))
    }
}
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION