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)
当时使用 Redis 的代码为:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RedisStudy01Application.class)
public class RedisStudy01ApplicationTests {
@Autowired
RedisTemplate<String,String> 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
百度时找到这篇博客:https://blog.csdn.net/a532672728/article/details/78035559,
总结这篇博客就是:
修改 redis 配置文件
将 bind 注释掉,允许非本机访问
关闭安全守护模式,将 protected-mode 设置为 no
加上安全认证,设置 requirepass 密码,但在我的测试中,不设置密码只执行上面两个步骤,也可以解决这个 bug
执行上面操作后,重启 Redis 服务端,报错就解决了
解决步骤
1. 测试主机和Linux服务器是否能连通
互相 ping 主机和 Linux 服务器的 ip,查看是否能 ping 通连接
ping 不通的话得解决网络问题,或者关闭 Linux 防火墙试试
如果是虚拟机的话,在虚拟机的设置中切换网络连接模式
虚拟机 --> 设置 --> 网络适配器 --> 修改网络连接模式
- 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
-
关闭安全守护模式
将 Redis 配置文件中的 protected-mode 修改成 no
protected-mode no
-
开启安全认证
我测试过不开启安全认证也可以解决这个报错,如果不行的话可以尝试
将 Redis 配置文件中添加 requirepass
requirepass 密码
3. 重启 Redis 服务端
有两种方式重启 Redis 服务端
第一种
在 Redis 客户端关闭 Redis 服务端
启动 Redis 客户端
redis-cli
关闭 Redis 服务端
shutdown
<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="bash" cid="n256" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> [admin@localhost ~]$ redis-cli
127.0.0.1:6379> shutdown
not connected></pre>
第二种
查看 Redis 进程
ps -ef|grep redis
杀死 Redis 进程
kill -s 9 进程号
ps -ef|grep redis 查看 Redis 进程号
kill -s 9 3119 杀死 Redis 进程
4. 测试
重新运行代码,发现运行成功,报错解决