30.8 Couchbase
Couchbase is an open-source, distributed multi-model NoSQL document-oriented database that is optimized for interactive applications. Spring Boot offers auto-configuration for Couchbase and abstractions on top of it provided by Spring Data Couchbase. There is a spring-boot-starter-data-couchbase
‘Starter’ for collecting the dependencies in a convenient way.
30.8.1 Connecting to Couchbase
You can very easily get a Bucket
and Cluster
by adding the Couchbase SDK and some configuration. The spring.couchbase.*
properties can be used to customize the connection. Generally you will provide the bootstrap hosts, bucket name and password:
spring.couchbase.bootstrap-hosts=my-host-1,192.168.1.123 spring.couchbase.bucket.name=my-bucket spring.couchbase.bucket.password=secret
Tip | |
---|---|
You need to provide at least the bootstrap host(s), in which case the bucket name is default and the password is the empty String. Alternatively, you can define your own org.springframework.data.couchbase.config.CouchbaseConfigurer @Bean to take control over the whole configuration. |
It is also possible to customize some of the CouchbaseEnvironment
settings. For instance the following configuration changes the timeout to use to open a new Bucket
and enables SSL support:
spring.couchbase.env.timeouts.connect=3000 spring.couchbase.env.ssl.key-store=/location/of/keystore.jks spring.couchbase.env.ssl.key-store-password=secret
Check the spring.couchbase.env.*
properties for more details.
30.8.2 Spring Data Couchbase repositories
Spring Data includes repository support for Couchbase. For complete details of Spring Data Couchbase, refer to their reference documentation.
You can inject an auto-configured CouchbaseTemplate
instance as you would with any other Spring Bean as long as a default CouchbaseConfigurer
is available (that happens when you enable the couchbase support as explained above). If you want to bypass the auto-configuration for Spring Data Couchbase, provide your own org.springframework.data.couchbase.config.AbstractCouchbaseDataConfiguration
implementation.
_@Component_ public class MyBean { private final CouchbaseTemplate template; _@Autowired_ public MyBean(CouchbaseTemplate template) { this.template = template; } // ... }
If you add a @Bean
of your own of type CouchbaseTemplate
named couchbaseTemplate
it will replace the default.