1. 程式人生 > >linux下的redis配置外網問題

linux下的redis配置外網問題

暑假期間一直好奇關於通過java的redis整合包jar訪問外網伺服器上面的redis快取,於是就嘗試在外網的linux進行安裝,鼓搗了有一天時間,大概終於弄好了。然後在eclipse上面寫了一個測試demo,大致的程式碼如下:
public static boolean existsUserApp(String sessionKey) {
        boolean flag = false;
        Jedis redis = null;
        try{
            redis=RedisConfig.redisPool.getResource();
            String key = "APP"
+ sessionKey; if (redis.exists(key)) { flag = true; } RedisConfig.redisPool.returnResource(redis); }catch(Exception e){ if(redis!=null){ RedisConfig.redisPool.returnBrokenResource(redis); flag=false
; } } return flag; }

結果一直顯示:連結失敗,找到原因大概是伺服器端沒有開啟外網訪問。於是查詢redis的redis.conf檔案
bind的屬性,有問題。
於是查找了下bind屬性的文件檔案:

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses. # # Examples: # # bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1 ::1 # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the # instance to everybody on the internet. So by default we uncomment the # following bind directive, that will force Redis to listen only into # the IPv4 lookback interface address (this means Redis will be able to # accept connections only from clients running into the same computer it # is running). # # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE.

大致的意思就是說:
1. 如果你在生產環境下的話,應該使用bind屬性繫結你的被訪問ip地址,確保安全。
2. 然後你如果執行環境下,需要將bind屬性進行註釋,然後redis預設可以接受所有的訪問ip。
於是我就註釋了,然後依舊有問題,發現linux的redis啟動不了服務了。一直報這個錯誤:

Creating Server TCP listening socket *:6379: unable to bind socket

我一直鬱悶,明明已經註釋了,為啥還是不行。於是我翻牆出去找了,發現外國友人大致劃分兩種情況。
redis的版本問題,就是在低於3.0.1時並未產生這個錯誤,高於就是產生這個問題

問題解決方法

1. 將IPv6的網絡卡進行關閉(具體方法進行百度,這裡不進行詳細敘述)
2. 新增在redis.conf中新增 bind 0.0.0.0(博主就是新增這個解決了問題)

具體原因

  • redis接受IPv4的訪問模式,IPv6它不會接受(額,這些都是我的猜測)
  • 至於加上bind 0.0.0.0 是可以得,我現在還不怎麼知道,請明白的小夥伴私信我,謝謝了。