Java連接Redis connection timed out 報(bào)錯(cuò)的解決方法

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)
image-20201028114433063

當(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:

  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

百度時(shí)找到這篇博客:https://blog.csdn.net/a532672728/article/details/78035559癣防,

總結(jié)這篇博客就是:

修改 redis 配置文件

  1. 將 bind 注釋掉,允許非本機(jī)訪問(wèn)

  2. 關(guān)閉安全守護(hù)模式掌眠,將 protected-mode 設(shè)置為 no

  3. 加上安全認(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ò)連接模式

image-20201028163638728
  • 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

  1. 允許非本機(jī)訪問(wèn)

    將 Redis 配置文件中的 bind 注釋掉

    bind 127.0.0.1

    image-20201028161456889
  2. 關(guān)閉安全守護(hù)模式

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

    protected-mode no

    image-20201028161620128
  3. 開(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>

image-20201028163340095

第二種

查看 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)

image-20201028162146031

kill -s 9 3119 殺死 Redis 進(jìn)程

4. 測(cè)試

重新運(yùn)行代碼,發(fā)現(xiàn)運(yùn)行成功锌雀,報(bào)錯(cuò)解決

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蚂夕,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子腋逆,更是在濱河造成了極大的恐慌婿牍,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惩歉,死亡現(xiàn)場(chǎng)離奇詭異等脂,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)撑蚌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)上遥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人争涌,你說(shuō)我怎么就攤上這事粉楚。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵模软,是天一觀的道長(zhǎng)伟骨。 經(jīng)常有香客問(wèn)我,道長(zhǎng)燃异,這世上最難降的妖魔是什么携狭? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮回俐,結(jié)果婚禮上逛腿,老公的妹妹穿的比我還像新娘。我一直安慰自己仅颇,他們只是感情好单默,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著灵莲,像睡著了一般雕凹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上政冻,一...
    開(kāi)封第一講書(shū)人閱讀 51,708評(píng)論 1 305
  • 那天枚抵,我揣著相機(jī)與錄音,去河邊找鬼明场。 笑死汽摹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的苦锨。 我是一名探鬼主播逼泣,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼舟舒!你這毒婦竟也來(lái)了拉庶?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤秃励,失蹤者是張志新(化名)和其女友劉穎氏仗,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體夺鲜,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡皆尔,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了币励。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片慷蠕。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖食呻,靈堂內(nèi)的尸體忽然破棺而出流炕,到底是詐尸還是另有隱情澎现,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布每辟,位于F島的核電站昔头,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏影兽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一莱革、第九天 我趴在偏房一處隱蔽的房頂上張望峻堰。 院中可真熱鬧,春花似錦盅视、人聲如沸捐名。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)镶蹋。三九已至,卻和暖如春赏半,著一層夾襖步出監(jiān)牢的瞬間贺归,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工断箫, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拂酣,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓仲义,卻偏偏與公主長(zhǎng)得像婶熬,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子埃撵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355