Access multiple couchbase buckets from a Reactive Spring Boot application

A few days ago, I got a situation where I needed to access more than one couchbase bucket from a single reactive spring boot (Spring Web-flux) application.

It is all about configuration. So, first of all, we will see how we can access a single couchbase bucket and then will move forward to access multiple buckets.

Access Single bucket:

To access a single bucket,  we need to define a couchbase configuration file and need to define some properties in the application.properties file.

CouchBaseConfiguration:

@Configuration
public class CouchBaseConfiguration extends AbstractReactiveCouchbaseConfiguration {

    private final String bucketUsername;
    private CouchbaseProperties couchbaseProperties;

    public CouchBaseConfiguration(CouchbaseProperties couchbaseProperties,
                                  @Value("${spring.couchbase.bucket.username}") String bucketUsername) {
        this.couchbaseProperties = couchbaseProperties;
        this.bucketUsername = bucketUsername;
    }

    @Override
    protected List<String> getBootstrapHosts() {
        return couchbaseProperties.getBootstrapHosts();
    }

    @Override
    protected String getBucketName() {
        return couchbaseProperties.getBucket().getName();
    }

    @Override
    protected String getBucketPassword() {
        return couchbaseProperties.getBucket().getPassword();
    }

    @Override
    protected String getUsername() {
        return bucketUsername;
    }
}

application.properties:

spring.couchbase.bootstrap-hosts=${COUCHBASE_HOST:localhost:8091}
spring.couchbase.bucket.name=${COUCHBASE_BUCKET:club}
spring.couchbase.bucket.username=${COUCHBASE_USERNAME:admin}
spring.couchbase.bucket.password=${COUCHBASE_PASSWORD:123456}

Access multiple buckets:

To access multiple buckets, we need to override configureReactiveRepositoryOperationsMapping() function of AbstractReactiveCouchbaseConfiguration class.

CouchBaseConfiguration:

@Configuration
@Slf4j
public class CouchBaseConfiguration extends AbstractReactiveCouchbaseConfiguration {

    private CouchbaseProperties couchbaseProperties;
    private final String username;
    private final String clubBucketName;
    private final String clubAuthBucketName;

    public CouchBaseConfiguration(CouchbaseProperties couchbaseProperties,
                                  @Value("${spring.couchbase.bucket.username}") String bucketUsername,
                                  @Value("${couchbase.club.bucket.name}") String clubBucketName,
                                  @Value("${couchbase.club.auth.bucket.name}") String clubAuthBucketName) {
        this.couchbaseProperties = couchbaseProperties;
        this.username = bucketUsername;
        this.clubBucketName = clubBucketName;
        this.clubAuthBucketName = clubAuthBucketName;
    }

    @Override
    protected List<String> getBootstrapHosts() {
        return couchbaseProperties.getBootstrapHosts();
    }

    @Override
    protected String getBucketName() {
        return clubBucketName;
    }

    @Override
    protected String getBucketPassword() {
        return couchbaseProperties.getBucket().getPassword();
    }

    @Override
    protected String getUsername() {
        return username;
    }

    private Bucket clubAuthBucket() throws Exception {
        return couchbaseCluster().openBucket(clubAuthBucketName);
    }

    private RxJavaCouchbaseTemplate crewAuthTemplate() throws Exception {
        RxJavaCouchbaseTemplate template = new RxJavaCouchbaseTemplate(
                couchbaseClusterInfo(), clubAuthBucket(),
                mappingCouchbaseConverter(), translationService());
        template.setDefaultConsistency(getDefaultConsistency());
        return template;
    }

    @Override
    public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
        try {
            baseMapping.mapEntity(CrewChatProfile.class, crewAuthTemplate());
        } catch (Exception ex) {
            log.error("Error in creating mapping for {} bucket", clubAuthBucketName, ex);
        }
    }

}

application.properties:

#couchbase config
spring.couchbase.bootstrap-hosts=${COUCHBASE_BOOTSTRAP_HOSTS:localhost:8091}
spring.couchbase.bucket.username=${COUCHBASE_USERNAME:admin}
spring.couchbase.bucket.password=${COUCHBASE_PASSWORD:123456}
spring.couchbase.env.timeouts.connect=${COUCHBASE_CONNECTION_TIMEOUT:30000}
couchbase.club.bucket.name=${COUCHBASE_CLUB_BUCKET:club}
couchbase.club.auth.bucket.name=${COUCHBASE_CLUB_AUTH_BUCKET:club_auth}

That’s it. I have kept this blog short and just provided only the required information.

I hope, it will be helpful for you.

About Rishi Khandelwal

Rishi is a tech enthusiast with having around 10 years of experience who loves to solve complex problems with pure quality. He is a functional programmer and loves to learn new trending technologies. His leadership skill is well prooven and has delivered multiple distributed applications with high scalability and availability by keeping the Reactive principles in mind. He is well versed with Scala, Akka, Akka HTTP, Akka Streams, Java8, Reactive principles, Microservice architecture, Async programming, functional programming, distributed systems, AWS, docker.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment