Spring框架(SpringBoot)中redis报错(Could not get a resource from the pool、java.net.SocketTimeoutException)

时间:2023-01-19 17:28:10

Spring框架(SpringBoot)中redis报错

在使用SpringBoot框架的时候,Spring一直会报两个特别纠结特别的烦的错误。尝试了很多种方法,都是失败的,不能成功。
经过我坚持不懈的努力寻找,终于把问题给解决了。


一、第一个报错:

java.net.SocketTimeoutException: Read timed out; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out。

org.springframework.data.redis.RedisConnectionFailureException: java.net.SocketTimeoutException: Read timed out; nested exception is redis.clie
nts.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
        at org.springframework.data.redis.connection.jedis.JedisUtils.convertJedisAccessException(JedisUtils.java:75) ~[iam-service-4.0.37.jar:
4.0.37]
        at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:113) ~[iam-service-
4.0.37.jar:4.0.37]
        at org.springframework.data.redis.connection.jedis.JedisConnection.get(JedisConnection.java:789) ~[iam-service-4.0.37.jar:4.0.37]
        at org.springframework.data.redis.core.DefaultValueOperations$1.inRedis(DefaultValueOperations.java:45) ~[iam-service-4.0.37.jar:4.0.37
]
        at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:49) ~[iam-s
ervice-4.0.37.jar:4.0.37]

二、第二个报错

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:162)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:251)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:58)
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:178)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:86)
at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:169)
at com.itapp.ItAppApiApplicationTests.test(ItAppApiApplicationTests.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

三、解决方案

1.这个问题,排除了一段时间,知道我在这个问题中找到了答案。
正解答案区:
正解答案区
这里有个哥们说了,是Jedis的版本问题。
Spring框架(SpringBoot)中redis报错(Could not get a resource from the pool、java.net.SocketTimeoutException)

2.如果版本问题排除后,还是会出现上述情况,就需要考虑设置参数的问题了。
Spring框架(SpringBoot)中redis报错(Could not get a resource from the pool、java.net.SocketTimeoutException)
其中比较关键的几个参数:
3.如果上述两步仍然不能解决问题,那就是你的Redis所在的服务器内存不足,分配给Redis的内存太少了,这就是环境问题了。将redis单独部署到一台服务器上,不要和你的应用程序放在同一个服务器上,问题就完美解决了。

@Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWaitMillis(100000);
        return jedisPoolConfig;
    }

3

1、看一下我们的SpringBoot使用的版本

目前还是1.5的。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <relativePath /> <!-- lookup parent from repository -->
        <version>1.5.10.RELEASE</version>
    </parent>

2、看一下redis的版本

之前是没有指定版本的,那么默认的就是和SpringBoot框架一样的版本

<!-- Spring Boot Redis -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

3、找一个新的版本更新进去

Spring框架(SpringBoot)中redis报错(Could not get a resource from the pool、java.net.SocketTimeoutException)

<!-- Spring Boot Redis -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>1.5.13.RELEASE</version>
        </dependency>

以上都是亲测有效
好啦,然后在启动就没有问题了,那么说明,确实是因为Redis的版本问题导致的。