首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >io.lettuce.core.RedisCommandTimeoutException:命令超时

io.lettuce.core.RedisCommandTimeoutException:命令超时
EN

Stack Overflow用户
提问于 2019-07-29 16:54:47
回答 1查看 4.3K关注 0票数 6
代码语言:javascript
复制
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) ~[?:?]

我正在使用以下示例代码通过连接池获取连接,请检查并提示我做错了什么

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

发布于 2021-02-23 02:32:07

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

代码语言:javascript
复制
spring.redis.timeout: 5000

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

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

查看:Spring Boot Configs for Redis

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57250297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档