有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的RedisCommandTimeoutException太多

我们正在使用redis库来面对这个特定的问题。我们收到的RedisCommandTimeoutException太多了。我们在redis cli中设置了2秒的超时,在redis慢速日志中设置了10毫秒的超时。虽然slowlogs中没有任何内容被登录,但我们的应用程序一直会收到这个超时

我们使用的代码如下

Duration timeout = 
Duration.ofMillis(applicationProperties.redisTimeOut);
RedisClient client = RedisClient.create(RedisURI.create(applicationProperties.redisUrl));
client.setDefaultTimeout(timeout);
RedisCommands<String, String> commands = client.connect().sync();

我们的应用程序中有大约100个线程,wgich可能正在使用这个共享连接

我们收到的例外情况如下

io.lettuce.core.RedisCommandTimeoutException: Command timed out
at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114)
at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:62)
at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80)
at com.sun.proxy.$Proxy11.hmget(Unknown Source)

共 (1) 个答案

  1. # 1 楼答案

    我也犯了同样的错误,就是redis突然掉了下来,在进行查询时花了很长时间,进行了几次重试,我发现了这种方法,我认为有更好的方法,但它对我有效

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
            RedisStandaloneConfiguration redisConf = new RedisStandaloneConfiguration();
            redisConf.setHostName(env.getProperty("spring.redis.host"));
            redisConf.setPort(Integer.parseInt(env.getProperty("spring.redis.port")));
            redisConf.setPassword(RedisPassword.of(env.getProperty("spring.redis.password")));
    
            LettuceConnectionFactory factory = new LettuceConnectionFactory(redisConf);
            factory.setTimeout(500L); //timeout to redis
    
            return factory;
        }