一、問題:
本人在阿里雲上模擬了一個Redis集群(使用8001-8006),使用linux連接,所有都正常,但是使用Java客戶端連接時(Java客戶端與Redis集群不在同一服務器上),就出現了異常,說不能訪問。
集群安裝:https://www.cnblogs.com/liconglong/p/14327430.html
二、原因:
排查過程就不說了,斷斷續續排查了半天,最后排查到原因:
1、bind配置錯誤
首先說一下bind的作用,其是用於限制哪些ip可以連接服務,redis.conf默認的bind值是127.0.0.1,即本機,因此導致Java客戶端不能連接
2、redis沒有放開允許外部訪問
在redis.conf文件中有 protected-mode 參數,該參數表示是否允許外部訪問,默認為no,就是不允許外部訪問,這也是導致Java客戶端不能訪問的一個關鍵點
3、集群創建
創建集群時,使用的下面命令創建(使用了內網ip:127.0.0.1),導致創建時也默認的是本機訪問
./bin/redis-cli --cluster create 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005 127.0.0.1:8006 --cluster-replicas 1
4、阿里雲安全組配置
阿里雲只開放了8001-8006這6個端口,沒有開放18001-18006這6個端口
三、解決:
針對以上問題,逐一解決
1、bind配置錯誤
配置 bind 0.0.0.0 ,這樣配置,說明可以允許所有ip訪問
2、redis沒有放開允許外部訪問
在redis.conf文件中有 protected-mode 參數設置為 yes ,允許外部訪問
3、集群創建
創建集群時,使用外部IP+端口(./bin/redis-cli --cluster create 8.131.245.111:8001 8.131.245.111:8002 8.131.245.111:8003 8.131.245.111:8004 8.131.245.111:8005 8.131.245.111:8006 --cluster-replicas 1)
./bin/redis-cli --cluster create 8.131.245.111:8001 8.131.245.111:8002 8.131.245.111:8003 8.131.245.111:8004 8.131.245.111:8005 8.131.245.111:8006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 8.131.245.111:8005 to 8.131.245.111:8001 Adding replica 8.131.245.111:8006 to 8.131.245.111:8002 Adding replica 8.131.245.111:8004 to 8.131.245.111:8003 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: c8d8397a94e420fcd34d6f3cdec09c0df7858829 8.131.245.111:8001 slots:[0-5460] (5461 slots) master M: 6c8cb2e6ed25da78d52c6c56afac4912f5c2f2fe 8.131.245.111:8002 slots:[5461-10922] (5462 slots) master M: 3549ac50dc141dbd0a697e72cd1ab3ea02360976 8.131.245.111:8003 slots:[10923-16383] (5461 slots) master S: 0c2c0b4db9610c1343980de1763199e0e69ab0aa 8.131.245.111:8004 replicates c8d8397a94e420fcd34d6f3cdec09c0df7858829 S: 6a73b5f7fe830607e0c6e4744b437a1ff3626b43 8.131.245.111:8005 replicates 6c8cb2e6ed25da78d52c6c56afac4912f5c2f2fe S: 602009707eb9aa1867450d1da7ae7a67548aeedc 8.131.245.111:8006 replicates 3549ac50dc141dbd0a697e72cd1ab3ea02360976 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join ...... >>> Performing Cluster Check (using node 8.131.245.111:8001) M: c8d8397a94e420fcd34d6f3cdec09c0df7858829 8.131.245.111:8001 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 0c2c0b4db9610c1343980de1763199e0e69ab0aa 8.131.245.111:8004 slots: (0 slots) slave replicates c8d8397a94e420fcd34d6f3cdec09c0df7858829 S: 6a73b5f7fe830607e0c6e4744b437a1ff3626b43 8.131.245.111:8005 slots: (0 slots) slave replicates 6c8cb2e6ed25da78d52c6c56afac4912f5c2f2fe S: 602009707eb9aa1867450d1da7ae7a67548aeedc 8.131.245.111:8006 slots: (0 slots) slave replicates 3549ac50dc141dbd0a697e72cd1ab3ea02360976 M: 3549ac50dc141dbd0a697e72cd1ab3ea02360976 8.131.245.111:8003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 6c8cb2e6ed25da78d52c6c56afac4912f5c2f2fe 8.131.245.111:8002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
4、阿里雲安全組配置
開放集群的所有端口(8001-8006),以及集群端口+10000的端口(18001-18006),這里我為了以后方便,索性直接將1-19999端口全部開放了
四、驗證
重新連接客戶端,大功告成
說明:如果之前是按照錯誤方式創建過集群,那么需要先將之前的AOF(appendonly.aof)、RDB(dump.rdb)和集群部署文件(nodes.conf)