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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM