我们在使用 lettuce 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 个线程可能正在使用这个共享连接
我们收到的异常如下
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)
最佳答案
我也有同样的错误,是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;
}
关于java - 生菜中的 RedisCommandTimeoutException 过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49811076/