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.

blog_footer

Written by 

Rishi is a Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!