【问题标题】:io.lettuce.core.RedisCommandTimeoutException: Command timed outio.lettuce.core.RedisCommandTimeoutException:命令超时
【发布时间】:2019-12-06 14:02:03
【问题描述】:
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;
    }

【问题讨论】:

  • 你发现了吗?我也面临同样的问题?

标签: amazon-elasticache redis-cluster lettuce


【解决方案1】:

Redis 响应速度应该非常快,比如毫秒,你也可以尝试从application.yml 配置连接超时。

spring.redis.timeout: 5000

PS:这个值是为了测试,通常5sec对于Redis连接来说是一个巨大的数字,还要首先检查你的应用程序是否可以访问Redis

注意:你真的不需要那些 java 连接方法。 Spring Boot 开箱即用,几乎没有任何编码,只需配置您的 application.yml

看:Spring Boot Configs for Redis

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 2013-04-03
    • 2013-09-30
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多