java jedis connect timed out_Java连接Redis connection timed out 报错的解决方法

Java连接Redis connection timed out 报错的解决方法

踩坑场景

在使用 RedisTemplate 连接 Redis 进行操作的时候,发生了如下报错:

报错信息如下:

Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.73.10:6379

at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:267)

at io.netty.util.concurrent.PromiseTask$RunnableAdapter.call(PromiseTask.java:38)

at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:127)

at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)

at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)

at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:495)

at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:905)

at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)

at java.lang.Thread.run(Thread.java:748)

2423465ad756880d612ff9ee569ce4ad.png

当时使用 Redis 的代码为:

@RunWith(SpringRunner.class)

@SpringBootTest(classes = RedisStudy01Application.class)

public class RedisStudy01ApplicationTests {

@Autowired

RedisTemplate redisTemplate;

@Test

public void contextLoads() {

redisTemplate.opsForValue().set("name","xp");

String name = redisTemplate.opsForValue().get("name");

System.out.println(name);

}

}

环境

报错时的环境为:

SpringBoot 2.1.4

Redis 6.0.8

CentOS 7

Windows 10

idea 2019.1

解决过程

看到这个报错的时候,我先想到的就是本机和 Redis 是否能 ping 得通。经过测试,能 ping 通

ping ip

然后我就觉得可能是 Linux 防火墙在干坏事,所以关闭了 Linux 防火墙

查看防火墙

systemctl status firewalld

service iptables status

暂时关闭防火墙

systemctl stop firewalld

service iptables stop

永久关闭防火墙

systemctl disable firewalld

chkconfig iptables off

但是关闭防火墙后,还是不能连接到远程Linux系统中的Redis,反而出现了一个新的报错

报错信息如下:

Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.73.10:6379

或者是

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:

Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.

Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server

If you started the server manually just for testing, restart it with the '--protected-mode no' option.

Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside

总结这篇博客就是:

修改 redis 配置文件

将 bind 注释掉,允许非本机访问

关闭安全守护模式,将 protected-mode 设置为 no

加上安全认证,设置 requirepass 密码,但在我的测试中,不设置密码只执行上面两个步骤,也可以解决这个 bug

执行上面操作后,重启 Redis 服务端,报错就解决了

解决步骤

1. 测试主机和Linux服务器是否能连通

互相 ping 主机和 Linux 服务器的 ip,查看是否能 ping 通连接

ping 不通的话得解决网络问题,或者关闭 Linux 防火墙试试

如果是虚拟机的话,在虚拟机的设置中切换网络连接模式

虚拟机 --> 设置 --> 网络适配器 --> 修改网络连接模式

a663120a20e265eac21fa9713defd696.png

windows 查看主机 ip 的命令

ipconfig

Linux 查看主机 ip 的命令

ifconfig

2. 关闭 Linux 防火墙

查看防火墙

systemctl status firewalld

service iptables status

暂时关闭防火墙

systemctl stop firewalld

service iptables stop

永久关闭防火墙

systemctl disable firewalld

chkconfig iptables off

3. 修改 Redis 配置文件

使用 vim Reids配置文件 的方式修改 Redis 配置文件

vim redis.config

允许非本机访问

将 Redis 配置文件中的 bind 注释掉

# bind 127.0.0.1

396cc929f55541cbc1dec39349841dcd.png

关闭安全守护模式

将 Redis 配置文件中的 protected-mode 修改成 no

protected-mode no

0aa2581ee9680dc00e14ec217143b7df.png

开启安全认证

我测试过不开启安全认证也可以解决这个报错,如果不行的话可以尝试

将 Redis 配置文件中添加 requirepass

requirepass 密码

bc9079c7122ee53a91747586283b2122.png

3. 重启 Redis 服务端

有两种方式重启 Redis 服务端

第一种

在 Redis 客户端关闭 Redis 服务端

启动 Redis 客户端

redis-cli

关闭 Redis 服务端

shutdown

[admin@localhost ~]$ redis-cli

127.0.0.1:6379> shutdown

not connected>

f9b15b42dcda5b53696c4b68023f849b.png

第二种

查看 Redis 进程

ps -ef|grep redis

杀死 Redis 进程

kill -s 9 进程号

ps -ef|grep redis 查看 Redis 进程号

804a8910ff1a93925ceb3422e1d2df92.png

kill -s 9 3119 杀死 Redis 进程

4. 测试

重新运行代码,发现运行成功,报错解决

fc17c65aec0ec7b74292be538fd40272.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值