Connecting to couchbase through JAVA


#1

Hi Team,

Needed your help in opening connection with couchbase one time using Java.
and use every where in my application. Kind of opening connection one time and using every where.
Just similar to datasource connection using for MF DB2.

Appreciate your help if you provide example with Spring Boot.

below is already I have coded in configuration.

Have few questions on below.
1.) Didn’t mentioned anything about connecting cluster below.

Cluster cluster = CouchbaseCluster.create(env, "localhost" );

2.) Didn’t mentioned open bucket statement as well.

Bucket secureBucket = cluster.openBucket(“user id”, “psw”);

In which piece of below code above two things will happen.?
Or
Did completely missed above two operations in below code. Please confirm.

@Configuration
@EnableCouchbaseRepositories(basePackages = “org.aexp.springbootcouchbase.mvc”)
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {

@Value("#{'${spring.couchbase.bootstrap-hosts}'.split(',')}")
private List<String> couchbasehosts;
	
@Value("${spring.couchbase.bucket.name}")
private String couchbaseBucketName;

@Value("${spring.couchbase.bucket.password}")
private String couchbaseBucketPassword;

@Value("${spring.couchbase.env.timeouts.connect}")
private Long couchbaseConnectionTimeout;

@Autowired
private CouchbaseEnvironment couchbaseEnvironment;   

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

@Override
protected String getBucketPassword() {
    return couchbaseBucketPassword;
}

@Override
protected CouchbaseEnvironment getEnvironment() {
    CouchbaseEnvironment couchbaseEnvironment = DefaultCouchbaseEnvironment
            .builder()
            .defaultMetricsLoggingConsumer(true, CouchbaseLogLevel.TRACE, LoggingConsumer.OutputFormat.JSON_PRETTY)
            .runtimeMetricsCollectorConfig(DefaultMetricsCollectorConfig.create(30, TimeUnit.SECONDS ))
            .networkLatencyMetricsCollectorConfig(DefaultLatencyMetricsCollectorConfig.create(30, TimeUnit.SECONDS ))
            .dnsSrvEnabled(false)
            .connectTimeout(couchbaseConnectionTimeout)
            .build();
    return couchbaseEnvironment;
}

@PostConstruct
public void init(){
    couchbaseEnvironment.eventBus().get().subscribe(new Action1<CouchbaseEvent>() {
		@Override
		public void call(CouchbaseEvent event) {
			System.out.println(event);
		}
	}
    );
}

@Override
protected List<String> getBootstrapHosts() {
	// TODO Auto-generated method stub
	return null;
}

}

Regards,
Naganjaneyulu I.


#2

Hi @naganjaneyulu.yazali,

It happens through the couchbaseCluster & couchbaseClient methods in AbstractCouchbaseConfiguration. These methods will create the cluster and bucket instances based on the properties configured by extending the class. The spring container will instantiate all the bean definitions in configuration classes. Yes, there will be only one bean instance created.


#3

Thanks Subhashni for explanation.

If you look at my configuration class.
No where I am using the couchbaseCluster & couchbaseClient methods.

where I have use those methods in the congifuration class it’s self or out side of the configuration class where needed.

It would be great if you can able to share some sample examples.


#4

You don’t need to use them, the AbstractCouchbaseConfiguration has those methods and they will be invoked for bean creation. You are doing it the right way by extending the configuration class.