1

I am trying configuring multiple couchbase data source using springboot-data-couchbase.

This is a way I tried to attach two couchbase sources with 2 repositories.

@Configuration
@EnableCouchbaseRepositories("com.xyz.abc")
public class AbcDatasource extends AbstractCouchbaseConfiguration {
 @Override
    protected List<String> getBootstrapHosts() {
        return Collections.singletonList("ip_address_of_couchbase");
    }
   //bucket_name
    @Override
    protected String getBucketName() {
        return "bucket_name";
    }
   //password
    @Override
    protected String getBucketPassword() {
        return "user_password";
    }
@Override
@Bean(destroyMethod = "disconnect", name = "COUCHBASE_CLUSTER_2")
public Cluster couchbaseCluster() throws Exception {
    return CouchbaseCluster.create(couchbaseEnvironment(), "ip_address_of_couchbase");
}

@Bean( name = "BUCKET2")
public Bucket bucket2() throws Exception {
    return this.couchbaseCluster().openBucket("bucket2", "somepassword");

}

@Bean( name = "BUCKET2_TEMPLATE")
public CouchbaseTemplate newTemplateForBucket2() throws Exception {
    CouchbaseTemplate template = new CouchbaseTemplate(
            couchbaseClusterInfo(), //reuse the default bean
            bucket2(), //the bucket is non-default
            mappingCouchbaseConverter(), translationService() 
    );
    template.setDefaultConsistency(getDefaultConsistency());
    return template;
}

@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
        baseMapping 
                .mapEntity(SomeDAOUsedInSomeRepository.class, newTemplateForBucket2());
    }
}
similarly: 
@Configuration
@EnableCouchbaseRepositories("com.xyz.mln")
public class MlnDatasource extends AbstractCouchbaseConfiguration {...}

Now the problem is there is no straight forward way to specify namespace based datasource by attaching different beans to these configurations like in springdata-jpa as springdata-jpa support this feature do using entity-manager-factory-ref and transaction-manager-ref.

Due to which only one configuration is being picked whoever comes first.

Any suggestion is greatly appreciated.

Related question: Use Spring Data Couchbase to connect to different Couchbase clusters

Anshul Mohil
  • 105
  • 2
  • 7
  • Haven't worked with Spring Data Couchbase, but as for any other Spring Data module, you have to add multiple configurations, one for each database. You have to set different 'couchbaseTemplateRef' for each config and provide the bean in your configuration. – Nima Ajdari Jun 13 '19 at 07:56
  • @NimaAJ Yeah, I have override CouchbaseTemplate to provide new cluster and bucket information and marked it under new Bean. Than I override configureRepositoryOperationsMapping as mentioned in https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.repository.multibucket And I am able to get two couchbase instances registered but I am not able to attach this new instance configuration with repositories. – Anshul Mohil Jun 13 '19 at 08:46
  • Isn't it against single responsibility methodology to have 2 couchbase buckets in one service? Shouldn't you call another microservice for getting data from another bucket? Can you reconsider your design? – Manish Bansal Jun 14 '19 at 02:17
  • @ManishBansal In our use case we are not using multiple buckets as different databases but as different type of resource which therefore can be performance tuned independently. Since couchbase provide clustering replication, resource usage and much more on buckets, buckets would be very helpful to segregate data in this context. – Anshul Mohil Aug 14 '19 at 11:41
  • did you find solution ? if so how ? – Ravikumar Jun 09 '21 at 15:31

1 Answers1

0

@anshul you are almost there. Make one of the Data Source as @primary which will be used as by default bucket.

Wherever you want to use the other bucket .Just use specific bean in your service class with the qualifier below is the example:

@Qualifier(value = "BUCKET1_TEMPLATE")
@Autowired
CouchbaseTemplate couchbaseTemplate;

Now you can use this template to perform all couch related operations on the desired bucket.

sonilovesh
  • 11
  • 1