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

<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 進程

ps -ef|grep redis

殺死 Redis 進程

kill -s 9 進程號

ps -ef|grep redis 查看 Redis 進程號

image-20201028162146031

kill -s 9 3119 殺死 Redis 進程

4. 測試

重新運行代碼,發現運行成功,報錯解決

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,619評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,155評論 3 425
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,635評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,539評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,255評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,646評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,655評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,838評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,399評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,146評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,338評論 1 372
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,893評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,565評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,983評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,257評論 1 292
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,059評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,296評論 2 376