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)

image-20201028114433063

当时使用 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:

  1. 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.

  2. 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

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

  4. 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 配置文件

  1. 将 bind 注释掉,允许非本机访问
  2. 关闭安全守护模式,将 protected-mode 设置为 no
  3. 加上安全认证,设置 requirepass 密码,但在我的测试中,不设置密码只执行上面两个步骤,也可以解决这个 bug

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

解决步骤

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

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

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

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

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

image-20201028163638728

  • 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

  1. 允许非本机访问

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

    # bind 127.0.0.1

    image-20201028161456889

  2. 关闭安全守护模式

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

    protected-mode no

    image-20201028161620128

  3. 开启安全认证

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

    将 Redis 配置文件中添加 requirepass

    requirepass 密码

    image-20201028161858615

3. 重启 Redis 服务端

有两种方式重启 Redis 服务端

第一种

在 Redis 客户端关闭 Redis 服务端

启动 Redis 客户端

redis-cli

关闭 Redis 服务端

shutdown

[admin@localhost ~]$ redis-cli
127.0.0.1:6379> shutdown
not connected>

image-20201028163340095

第二种

查看 Redis 进程

ps -ef|grep redis

杀死 Redis 进程

kill -s 9 进程号

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

image-20201028162146031

kill -s 9 3119 杀死 Redis 进程

4. 测试

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

image-20201028162908218

原文地址:https://www.cnblogs.com/windowsxpxp/p/13891902.html