Java連接Redis connection timed out 報(bào)錯(cuò)的解決方法
踩坑場(chǎng)景
在使用 RedisTemplate 連接 Redis 進(jìn)行操作的時(shí)候,發(fā)生了如下報(bào)錯(cuò):
報(bào)錯(cuò)信息如下:
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)
當(dāng)時(shí)使用 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);
}
?
}
環(huán)境
報(bào)錯(cuò)時(shí)的環(huán)境為:
SpringBoot 2.1.4
Redis 6.0.8
CentOS 7
Windows 10
idea 2019.1
解決過(guò)程
看到這個(gè)報(bào)錯(cuò)的時(shí)候,我先想到的就是本機(jī)和 Redis 是否能 ping 得通。經(jīng)過(guò)測(cè)試,能 ping 通
ping ip
然后我就覺(jué)得可能是 Linux 防火墻在干壞事她渴,所以關(guān)閉了 Linux 防火墻
- 查看防火墻
systemctl status firewalld
service iptables status
- 暫時(shí)關(guān)閉防火墻
systemctl stop firewalld
service iptables stop
- 永久關(guān)閉防火墻
systemctl disable firewalld
chkconfig iptables off
但是關(guān)閉防火墻后,還是不能連接到遠(yuǎn)程Linux系統(tǒng)中的Redis,反而出現(xiàn)了一個(gè)新的報(bào)錯(cuò)
報(bào)錯(cuò)信息如下:
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
百度時(shí)找到這篇博客:https://blog.csdn.net/a532672728/article/details/78035559癣防,
總結(jié)這篇博客就是:
修改 redis 配置文件
將 bind 注釋掉,允許非本機(jī)訪問(wèn)
關(guān)閉安全守護(hù)模式掌眠,將 protected-mode 設(shè)置為 no
加上安全認(rèn)證蕾盯,設(shè)置 requirepass 密碼,但在我的測(cè)試中蓝丙,不設(shè)置密碼只執(zhí)行上面兩個(gè)步驟级遭,也可以解決這個(gè) bug
執(zhí)行上面操作后,重啟 Redis 服務(wù)端渺尘,報(bào)錯(cuò)就解決了
解決步驟
1. 測(cè)試主機(jī)和Linux服務(wù)器是否能連通
互相 ping 主機(jī)和 Linux 服務(wù)器的 ip挫鸽,查看是否能 ping 通連接
ping 不通的話得解決網(wǎng)絡(luò)問(wèn)題,或者關(guān)閉 Linux 防火墻試試
如果是虛擬機(jī)的話鸥跟,在虛擬機(jī)的設(shè)置中切換網(wǎng)絡(luò)連接模式
虛擬機(jī) --> 設(shè)置 --> 網(wǎng)絡(luò)適配器 --> 修改網(wǎng)絡(luò)連接模式
- windows 查看主機(jī) ip 的命令
ipconfig
- Linux 查看主機(jī) ip 的命令
ifconfig
2. 關(guān)閉 Linux 防火墻
- 查看防火墻
systemctl status firewalld
service iptables status
- 暫時(shí)關(guān)閉防火墻
systemctl stop firewalld
service iptables stop
- 永久關(guān)閉防火墻
systemctl disable firewalld
chkconfig iptables off
3. 修改 Redis 配置文件
使用 vim Reids配置文件
的方式修改 Redis 配置文件
vim redis.config
-
允許非本機(jī)訪問(wèn)
將 Redis 配置文件中的 bind 注釋掉
bind 127.0.0.1
image-20201028161456889 -
關(guān)閉安全守護(hù)模式
將 Redis 配置文件中的 protected-mode 修改成 no
protected-mode no
image-20201028161620128 -
開(kāi)啟安全認(rèn)證
我測(cè)試過(guò)不開(kāi)啟安全認(rèn)證也可以解決這個(gè)報(bào)錯(cuò)丢郊,如果不行的話可以嘗試
將 Redis 配置文件中添加 requirepass
requirepass 密碼
image-20201028161858615
3. 重啟 Redis 服務(wù)端
有兩種方式重啟 Redis 服務(wù)端
第一種
在 Redis 客戶端關(guān)閉 Redis 服務(wù)端
啟動(dòng) Redis 客戶端
redis-cli
關(guān)閉 Redis 服務(wù)端
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 進(jìn)程
ps -ef|grep redis
殺死 Redis 進(jìn)程
kill -s 9 進(jìn)程號(hào)
ps -ef|grep redis 查看 Redis 進(jìn)程號(hào)
kill -s 9 3119 殺死 Redis 進(jìn)程
4. 測(cè)試
重新運(yùn)行代碼,發(fā)現(xiàn)運(yùn)行成功锌雀,報(bào)錯(cuò)解決