Engineering
Releases
News and Events

Couchbase as a First Class Citizen of Spring Boot 1.4

This is a cross-post blog from Simon BASLÉ from Couchbase. You can find him on twitter (@simonbasle) or github. Learn more about Couchbase and the Couchbase Java SDK on the developer portal.

Spring Boot 1.4.0 MILESTONE 2 is out! This is a good time to tell you about the joint effort between Spring Boot team members and the Couchbase Java SDK team to offer a first class integration of Couchbase into Spring Boot :)

In Spring Boot 1.4.0, Couchbase becomes a first class citizen of the Spring Boot ecosystem!

Couchbase SDK Integration

Spring Boot now directly recognizes when you have the Couchbase SDK in your classpath. And when that’s the case, it instantiates a Cluster and a Bucket bean for you using autoconfiguration.

Spring Boot can pick up properties to further configure these core SDK classes, even the CouchbaseEnvironment!

The following bootstrapping properties are recognized:

spring.couchbase.bootstrap-hosts
spring.couchbase.bucket.name
spring.couchbase.bucket.password

Environment tuning (IO endpoints, SSL support and default sync API timeouts) are exposed through the following properties:

spring.couchbase.env.endpoints.key-value
spring.couchbase.env.endpoints.query
spring.couchbase.env.endpoints.view

spring.couchbase.env.ssl.enabled
spring.couchbase.env.ssl.key-store
spring.couchbase.env.ssl.key-store-password

spring.couchbase.env.timeouts.connect
spring.couchbase.env.timeouts.key-value
spring.couchbase.env.timeouts.query
spring.couchbase.env.timeouts.view

Now this is ⭐️⭐️⭐️⭐️⭐️ support!

WARNING: Note that in the previous milestone the bootstrapping properties were prefixed with “ spring.*data*.couchbase ”, now becoming “ spring.couchbase ”.

Spring Cache Implementation

The Spring Cache abstraction has a Couchbase 2.x implementation, couchbase-spring-cache.

Spring Boot now recognizes this cache implementation when both the Java SDK and the couchbase-spring-cache artifacts are on the classpath.

This integrates nicely with the previous section, seeing as the default underlying storage Bucket for the caches is the one autoconfigured by Spring Boot :)

NOTE: The cache implementation has been pulled out of the Spring Data Couchbase project into its own project (in Couchbase’s github repository), so that it could be used and released separately from Spring Data.

TIP: Don’t forget to at least configure the spring.couchbase.bootstrap-hosts property.

This implementation of the CacheManager allows for storing data from several caches into the same Couchbase bucket, by automatically prefixing the keys in Couchbase with the name of each cache.

The CouchbaseCacheManager will automatically reuse the Bucket autoconfigured by Spring Boot. By simply adding a property to declaratively list cache names, the corresponding caches will be pre-loaded:

spring.cache.type=couchbase
spring.cache.cache-names=foo,bar

Five-star support, we tell you!

You can even configure it to store data for different caches into multiple buckets. The caches can then be configured using a fluent builder pattern (eg. in a CacheManagerCustomizer<CouchbaseCacheManager>). This CacheBuilder also allows you to further tune the caches, like setting a default expiration time:

@Configuration
public class CouchbaseCacheConfiguration {

  private final Cluster cluster;

  //inject the Cluster from Boot core Couchbase support
  public CouchbaseCacheConfiguration(Cluster cluster) {
    this.cluster = cluster;
  }

  @Bean
  public Bucket anotherBucket() {
    return this.cluster.openBucket("another", "secret");
  }

  @Bean
  public CacheManagerCustomizer<CouchbaseCacheManager> cmCustomizer() {
    return c -> {
      c.prepareCache("biz", CacheBuilder
              .newInstance(anotherBucket())
              .withExpirationInMillis(2000));
    };
  }
}

This implementation can use views to selectively clear caches that are collocated in the same Bucket.

Finally, it supports dynamic creation of caches as they are requested. To activate that, simply omit to declare any cache name and just activate Couchbase caching with the relevant property:

spring.cache.type=couchbase

This will use the Spring Boot autoconfigured Bucket as the default bucket for all dynamically created caches.

Fun with @Cacheable awaits 👍

Spring Data Integration

Spring Data Couchbase 2.1.0 (release train Hopper) has included several modifications that makes integration with Spring Boot a breeze.

First the usual suspect: by default Spring Boot will autoconfigure Spring Data Couchbase to use the Bucket it created. This is made possible by having separated the core SDK configuration parts into a CouchbaseConfigurer class, while what is really specific to Spring Data is located in a new base class, AbstractCouchbaseDataConfiguration.

If you use only Spring Data, you can go ahead and continue using the AbstractCouchbaseConfiguration, which is now both a CouchbaseConfigurer and an AbstractCouchbaseDataConfiguration.

If you use Spring Boot however, it will autoconfigure a CouchbaseConfigurer. You can always tune your own AbstractCouchbaseDataConfiguration and inject the configurer in it.

Other Spring Data Couchbase Features

The 2.1.0 release also includes a few new features:

Conclusion

Go grab that ⭐️⭐️⭐️⭐️⭐️ MILESTONE!

As always, feedback is welcome (in the Spring Data issue tracker, Spring Boot issue tracker or on the Couchbase forums).

Happy Coding!

comments powered by Disqus