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

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的版本问题。
这里写图片描述

2.如果版本问题排除后,还是会出现上述情况,就需要考虑设置参数的问题了。
这里写图片描述
其中比较关键的几个参数:
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 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的版本问题导致的。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值