org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 300 millisecond(s)

原因:

1、测试连接超时导致;

解决办法:修改spring中配置文件 spring.redis.timeout=800  ,调大超时时间

同时引入了lettuce和jedis的依赖导致的,项目中排出jedis的依赖并加入apache的

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.7.0</version>
</dependency>

修改:

1、

redis异常:

[2020-06-07 09:54:40.339][pool-8-thread-64][ERROR][com.sms.proxyee.ippool.schedule.ChannelRegisterIpTask][93]:
org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 300 millisecond(s)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:70) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:269) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.connection.lettuce.LettuceZSetCommands.convertLettuceAccessException(LettuceZSetCommands.java:906) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.connection.lettuce.LettuceZSetCommands.zAdd(LettuceZSetCommands.java:99) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.connection.DefaultedRedisConnection.zAdd(DefaultedRedisConnection.java:808) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.connection.DefaultStringRedisConnection.zAdd(DefaultStringRedisConnection.java:1328) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.core.DefaultZSetOperations.lambda$add$1(DefaultZSetOperations.java:65) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:228) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:188) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.redis.core.DefaultZSetOperations.add(DefaultZSetOperations.java:65) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at com.sms.proxyee.ippool.service.IpPoolService.registerIps(IpPoolService.java:73) ~[classes/:?]
at com.sms.proxyee.ippool.schedule.ChannelRegisterIpTask.lambda$null$0(ChannelRegisterIpTask.java:89) ~[classes/:?]
at java.util.HashMap.forEach(HashMap.java:1289) ~[?:1.8.0_211]
at com.sms.proxyee.ippool.schedule.ChannelRegisterIpTask.lambda$run1$1(ChannelRegisterIpTask.java:88) ~[classes/:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_211]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_211]
Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out after 300 millisecond(s)
at io.lettuce.core.ExceptionFactory.createTimeoutException(ExceptionFactory.java:51) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:69) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at com.sun.proxy.$Proxy105.zadd(Unknown Source) ~[?:?]
at org.springframework.data.redis.connection.lettuce.LettuceZSetCommands.zAdd(LettuceZSetCommands.java:97) ~[spring-data-redis-2.2.1.RELEASE.jar:2.2.1.RELEASE]
... 14 more

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class LettuceConfiguration {
    @Autowired
    private RedisConfig redisConfig;

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }


    @Bean
//    @Deprecated
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        return new StringRedisTemplate(redisConnectionFactory);
    }

    @Bean
    LettuceConnectionFactory lettuceConnectionFactory(GenericObjectPoolConfig genericObjectPoolConfig) {
        // 单机版配置
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(redisConfig.getDatabase());
        redisStandaloneConfiguration.setHostName(redisConfig.getHost());
        redisStandaloneConfiguration.setPort(redisConfig.getPort());
        redisStandaloneConfiguration.setPassword(RedisPassword.of(redisConfig.getPassword()));

        // 集群版配置
//        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
//        String[] serverArray = clusterNodes.split(",");
//        Set<RedisNode> nodes = new HashSet<RedisNode>();
//        for (String ipPort : serverArray) {
//            String[] ipAndPort = ipPort.split(":");
//            nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.valueOf(ipAndPort[1])));
//        }
//        redisClusterConfiguration.setPassword(RedisPassword.of(password));
//        redisClusterConfiguration.setClusterNodes(nodes);
//        redisClusterConfiguration.setMaxRedirects(maxRedirects);

        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofMillis(redisConfig.getTimeout()))
                .poolConfig(genericObjectPoolConfig)
                .build();
        return new LettuceConnectionFactory(redisStandaloneConfiguration,clientConfig);
    }


    /**
     * GenericObjectPoolConfig 连接池配置
     *
     * @return
     */
    @Bean
    public GenericObjectPoolConfig genericObjectPoolConfig() {
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(redisConfig.getMaxIdle());
        genericObjectPoolConfig.setMinIdle(redisConfig.getMinIdle());
        genericObjectPoolConfig.setMaxTotal(redisConfig.getMaxActive());
        genericObjectPoolConfig.setMaxWaitMillis(redisConfig.getMaxWait());
        return genericObjectPoolConfig;
    }
}
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 生菜redis配置
 */
@Data
@Component
@ConfigurationProperties(prefix="spring.redis")
public class RedisConfig {
    private String host;
    private int port;
    private String password;
    private int database;
    private int maxIdle;
    private int minIdle;
    private int maxActive;
    private long maxWait;
    private long timeout;

}
##### database config #####
##### redis config #####
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# 连接超时时间(毫秒)
# Redis服务器连接密码(默认为空)
spring.redis.password=test
#测试环境网络差调大
spring.redis.timeout=300
##连接池最大连接数(使用负值表示没有限制) 默认8
spring.redis.max-active=500
## 连接池中的最大空闲连接 默认8
spring.redis.max-idle=8
## 连接池中的最小空闲连接 默认0
spring.redis.min-idle=0
## 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.max-wait=-1
原文地址:https://www.cnblogs.com/zhangshiwen/p/13059276.html