How to set couchbase operation timeout in spring data couchbase?

前提是你 提交于 2019-12-10 15:59:19

问题


I have a simple spring project which try to retrieve a document from couchbase using spring-data-couchbase. I have configured the config by extending AbstractCouchbaseConfiguration. Everything works perfectly fine.

Since I use couchbase as a cache, now I need to set the operation timeout to a lower value. Anybody can shed some light on how to do it?


回答1:


To define a timeout for the CouchbaseClient you have to provide it using the ConnectionFactory. Sadly, the current version of spring-data-couchbase doesn't provide a simple way to do that.

The class responsible to create connection factories is ConnectionFactoryBean, and it has a setter for the operations timeout, but I couldn't find anything for @Configuration classes.

Since you are extending AbstractCouchbaseConfiguration, you might want to override couchbaseClient():

public class MyCouchbaseConfiguration extends AbstractCouchbaseConfiguration {

     ...

     private final CouchbaseConnectionFactoryBuilder builder = new CouchbaseConnectionFactoryBuilder();
     private CouchbaseConnectionFactory connectionFactory;

     ...

     @Override
     @Bean(destroyMethod = "shutdown")
     public CouchbaseClient couchbaseClient() throws Exception {
           setLoggerProperty(couchbaseLogger());

           if(connectionFactory == null){
               builder.setOpTimeout(myTimeout);
               // Set another parameters.
               ...

               connectionFactory = builder.buildCouchbaseConnection(
                   bootstrapUris(bootstrapHosts()),
                   getBucketName(),
                   getBucketPassword()
               );
           }

           return new CouchbaseClient(connectionFactory);
     }
}

Also, you can call directly CouchbaseFactoryBean but it's not a good practice if you are not configuring your application using XML bean definitions.

Here is the XML configuration just in case:

<bean id="couchbase" class="org.springframework.data.couchbase.core.CouchbaseFactoryBean">
    <property name="opTimeout" value="1000"/> <!-- 1 sec -->
    <property name="bucket" value="myBucket"/>
    <property name="password" value="myPassword"/>
    <property name="host" value="myHost"/>
</bean>
<couchbase:template id="couchbaseTemplate"/>



回答2:


According to the docs, the correct answer is wrong. That's not the way it should be done...

When you extend from AbstractCouchbaseConfiguration

Default settings can be customized through the DefaultCouchbaseEnvironment.Builder or through the setting of system properties. Latter ones take always precedence and can be used to override builder settings at runtime too. http://docs.couchbase.com/sdk-api/couchbase-java-client-2.0.0/com/couchbase/client/java/env/DefaultCouchbaseEnvironment.html

For instance, to customize the timeout connection:

@Override
protected CouchbaseEnvironment getEnvironment() {
        DefaultCouchbaseEnvironment.builder().connectTimeout(15000);
        return super.getEnvironment();
}

There are other options that can be assigned this way.




回答3:


According the docs (https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html ),

Here is the application.properties :

spring.couchbase.env.timeouts.connect=5000ms # Bucket connections timeouts.
spring.couchbase.env.timeouts.key-value=2500ms # Blocking operations performed on a specific key timeout.
spring.couchbase.env.timeouts.query=7500ms # N1QL query operations timeout.
spring.couchbase.env.timeouts.socket-connect=1000ms # Socket connect connections timeout.
spring.couchbase.env.timeouts.view=7500ms # Regular and geospatial view operations timeout.



回答4:


For Spring Data Couchbase 2, adding the following property in application.properties did it

 spring.couchbase.env.timeouts.connect=20000



回答5:


I used queryTimeout to set the value of operation timeout as shown below:

CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
                    .connectTimeout(50000).socketConnectTimeout(50000)
                    .queryTimeout(1000)
                    .build();

this will ensure you get the response within 1s.

use this link to get different available options: https://docs.couchbase.com/java-sdk/2.7/client-settings.html#timeout-options



来源:https://stackoverflow.com/questions/31326482/how-to-set-couchbase-operation-timeout-in-spring-data-couchbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!