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
image-20201028161456889 -
關閉安全守護模式
將 Redis 配置文件中的 protected-mode 修改成 no
protected-mode no
image-20201028161620128 -
開啟安全認證
我測試過不開啟安全認證也可以解決這個報錯,如果不行的話可以嘗試
將 Redis 配置文件中添加 requirepass
requirepass 密碼
image-20201028161858615
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. 測試
重新運行代碼,發現運行成功,報錯解決