Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: 解决办法

项目开发的过程中,应业务需求将Redis作为缓存服务器整合进了Spring Boot的项目中,在初测试的时候,遇到连接超时的情况,错误信息如下:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.129:6379
    at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:198)
    at com.leyou.test.RedisTest.testRedis(RedisTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)


    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.129:6379
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:233)
    at io.lettuce.core.RedisClient.connectStandalone(RedisClient.java:253)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:202)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:56)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:959)
    ... 41 more
Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.43.129:6379

 

解决办法:

一、application.yml的redis配置中的spring.redis.timeout中连接超时时间(毫秒)中时间设置不能为0

二、找到redis的配置文件 redis.conf

vim redis.conf

修改 protected-mode  yes 改为:protected-mode no

注释掉 #bin 127.0.0.1

三、yml文件中配置参数
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.30.103
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000

 

 

 

  • 39
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 29
    评论
### 回答1: 无法连接到Redis;嵌套异常是org.springframework.data.redis.connection.poolexception:无法从池中获取资源;嵌套异常是io.lettuce.core.redisconnectionexception:无法连接到127...1:6379。 ### 回答2: 这个错误是指与 Redis 数据库连接时出现了问题。具体来说,问题可能是无法获取资源池中的资源,或者连接 Redis 数据库时出现了连接异常。 出现这个错误可能有多种原因。一种可能是由于 Redis 数据库本身出现了问题,比如它没有启动或者它的某些配置信息不正确。另一种可能是由于连接 Redis 数据库的应用程序出现了问题,比如它没有正确地配置连接池或者它的网络设置不正确。 为了解决这个问题,你可以尝试下列步骤: 1. 确保 Redis 数据库已经启动并且正常工作。你可以使用 Redis 自带的客户端工具 redis-cli 来测试一下 Redis 是否可以正常工作。 2. 检查应用程序的连接池配置是否正确。连接池通常会限制连接数量,当连接池达到极限时,就会出现无法获取资源的情况。你可以查看连接池配置文件,确认其中的参数是否合理。 3. 检查应用程序的网络设置是否正确。比如,应用程序需要能够访问 Redis 数据库所在的主机,并且必须与 Redis 数据库的端口相对应。 4. 如果问题仍然存在,可以尝试使用其它 Redis 客户端实现来连接 Redis 数据库,看看是否能够成功连接。你可以使用 jedis 或 Lettuce 客户端来测试一下。如果其它客户端可以正常连接,那么说明问题不在 Redis 数据库本身。反之,则需要进一步排查 Redis 数据库的问题。 5. 最后,你可以查看应用程序日志,看看是否有其它有用的信息可以提供帮助。日志通常会记录每次连接 Redis 数据库的详细信息,包括错误码和错误信息。 ### 回答3: 这个错误提示是出现在使用Redis缓存时连接错误的信息。其中“unable to connect to redis”意味着程序无法连接到Redis服务端,而“could not get a resource from the pool”则表示从连接池中无法获取资源,进而出现了“io.lettuce.core.redisconnectionexception: unable to connect to 127.0.0.1:6379”错误。 这个错误可能有多种原因,包括Redis服务器未启动、网络连接问题、Redis配置错误、Redis连接池满负荷或错误使用Redis连接池等。解决这个问题的方法可以包括以下几个方面。 首先需要确认Redis服务器是否已经正确地启动。如果Redis服务未启动,需要通过启动服务程序来启动Redis。如果Redis服务已经启动,那么需要进一步确认网络连接是否正常,以及Redis的配置是否正确,包括Redis的端口和密码是否正确设置。 另外,连接池满负荷或错误使用Redis连接池也可能导致连接失败。需要检查Redis连接池的设置,例如最大连接数、超时时间、等待时间,以及连接池中的连接是否被正确地释放。 当然,在出现连接失败的情况下,也可以通过调整程序中的Redis连接参数来解决问题。例如,可以尝试修改Redis连接超时时间或者重试次数等参数,以便更好地适应当前的网络环境和Redis服务器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值