【问题标题】:How to set couchbase operation timeout in spring data couchbase?如何在 Spring Data Couchbase 中设置 Couchbase 操作超时?
【发布时间】:2015-07-09 19:22:58
【问题描述】:
我有一个简单的 spring 项目,它尝试使用 spring-data-couchbase 从 couchbase 检索文档。我已经通过扩展 AbstractCouchbaseConfiguration 来配置配置。一切正常。
由于我使用 couchbase 作为缓存,现在我需要将操作超时设置为较低的值。任何人都可以阐明如何做到这一点?
【问题讨论】:
标签:
spring-data
couchbase
spring-data-couchbase
【解决方案2】:
根据文档 (https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html),
这是 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.
【解决方案3】:
要为 CouchbaseClient 定义超时,您必须使用 ConnectionFactory 来提供它。遗憾的是,当前版本的 spring-data-couchbase 并没有提供简单的方法来做到这一点。
负责创建连接工厂的类是ConnectionFactoryBean,它有一个设置操作超时的方法,但我找不到@Configuration 类的任何东西。
由于您正在扩展 AbstractCouchbaseConfiguration,您可能想要覆盖 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);
}
}
此外,您可以直接调用 CouchbaseFactoryBean,但如果您不使用 XML bean 定义配置您的应用程序,这不是一个好习惯。
这是 XML 配置以防万一:
<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"/>
【解决方案4】:
对于 Spring Data Couchbase 2,在 application.properties 中添加以下属性就可以了
spring.couchbase.env.timeouts.connect=20000