io.lettuce.core.RedisCommandTimeoutException: Command timed out after 10 second(s)
at io.lettuce.core.ExceptionFactory.createTimeoutException(ExceptionFactory.java:51) ~[lettuce-core-5.1.6.RELEASE.jar:?]
at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114) ~[lettuce-core-5.1.6.RELEASE.jar:?]
at io.lettuce.core.cluster.ClusterFutureSyncInvocationHandler.handleInvocation(ClusterFutureSyncInvocationHandler.java:123) ~[lettuce-core-5.1.6.RELEASE.jar:?]
at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) ~[lettuce-core-5.1.6.RELEASE.jar:?]
at com.sun.proxy.$Proxy72.get(Unknown Source) ~[?:?]
at sun.reflect.GeneratedMethodAccessor138.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
at io.lettuce.core.support.ConnectionWrapping$DelegateCloseToConnectionInvocationHandler.handleInvocation(ConnectionWrapping.java:191) ~[lettuce-core-5.1.6.RELEASE.jar:?]
at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) ~[lettuce-core-5.1.6.RELEASE.jar:?]
at com.sun.proxy.$Proxy72.get(Unknown Source) ~[?:?]
我正在使用以下示例代码通过连接池获取连接,请检查并建议我做错了什么
public StatefulRedisClusterConnection<String, String> getRedisClient() {
log.debug("getRedisClient()");
if (pool == null) {
synchronized (this) {
pool = initializeConnectionPool();
}
}
try {
return pool.borrowObject();
} catch (Exception e) {
log.error("getting pool connection failed",e);
}
return null;
}
public GenericObjectPool<StatefulRedisClusterConnection<String, String>> initializeConnectionPool() {
RedisClusterClient clusterClient = RedisClusterClient.create(new RedisURI(clusterUrl, clusterPort,Duration.ofMillis(10000)));
ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
.enableAllAdaptiveRefreshTriggers()
.refreshTriggersReconnectAttempts(30)
.build();
clusterClient.setOptions(ClusterClientOptions.builder()
.topologyRefreshOptions(topologyRefreshOptions)
.build());
pool = ConnectionPoolSupport.createGenericObjectPool(() -> clusterClient.connect(), getLettucePoolConfig());
return pool;
}
private GenericObjectPoolConfig getLettucePoolConfig() {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(256);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setMaxWaitMillis(10000);
poolConfig.setMaxIdle(100);
poolConfig.setMinIdle(50);
poolConfig.setTestOnBorrow(false);
poolConfig.setTestWhileIdle(true);
return poolConfig;
}
最佳答案
Redis 应该在毫秒内响应非常快,您也可以尝试从 application.yml
配置连接超时.
spring.redis.timeout: 5000
PS:这个值是测试用的,通常5秒对于Redis连接来说是一个很大的数字,另外检查你的应用是否可以访问Redis
首先。注意:你真的不需要那些 java 连接方法。 Spring Boot 开箱即用,几乎没有编码,这只是配置您的 application.yml 的问题
看:Spring Boot Configs for Redis
关于amazon-elasticache - io.lettuce.core.RedisCommandTimeoutException : Command timed out,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57250297/