redis5.0.0叢集搭建
一、安裝redis
下載所需安裝包redis-5.0.0.tar.gz,通過xftp6上傳,指定目錄安裝,或者線上wget自行安裝
# wget http://download.redis.io/releases/redis-5.0.0.tar.gz
# tar xzf redis-5.0.0.tar.gz
# cd redis-5.0.0
# make
二、拷貝檔案
一般安裝軟體在data目錄下的tool-install工具目錄中,安裝make後,在src裡會生成 redis-server redis-cli redis-trib.rb(叢集不用)
我們在/usr/local目錄下新建redis目錄,拷貝這2個檔案至此
# mkdir /usr/local/redis
# cd /data/tool-install/redis-5.0.0/src
# cp -r redis-server /usr/local/redis/
# cp -r redis-cli /usr/local/redis/
# cd .. 退到上一級目錄複製 redis.conf
# cp -r redis.conf /usr/local/redis/
三、修改redis.conf
首先,建立目錄cluster,複製redis.conf 三份至cluster目錄裡,並分別命名redis7000.conf redis7001.conf redis7002.conf
其次,根據網上查詢相關資料,修改相關內容:
port 7000
#所在內網ip,不可寫127.0.0.1
bind 192.168.199.177
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file /usr/local/redis/cluster/nodes-7000.conf
cluster-node-timeout 15000
三份都要分別修改7000 7001 7002
四、開啟服務
修改配置完畢,退回上級目錄,然後開啟redis服務
# ./redis-server ./cluster/redis7000.conf # ./redis-server ./cluster/redis7000.conf # ./redis-server ./cluster/redis7000.conf # ps -ef | grep redis
五、第二臺redis伺服器
再找一臺內網伺服器192.168.199.132 按照 一(安裝 redis)二(拷貝檔案)三(修改配置) 四(服務開啟)步驟
六、建立叢集
# ./redis-cli --cluster create 192.168.199.177:7000 192.168.199.177:7001 192.168.199.177:7002 192.168.199.132:7003 192.168.199.132:7004 192.168.199.132:7005 --cluster-replicas 1
1、執行成功如下:
[[email protected] redis]# ./redis-cli --cluster create 192.168.199.177:7000 192.168.199.177:7001 192.168.199.177:7002 192.168.199.132:7003 192.168.199.132:7004 192.168.199.132:7005 --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 192.168.199.132:7004 to 192.168.199.177:7000
Adding replica 192.168.199.177:7002 to 192.168.199.132:7003
Adding replica 192.168.199.132:7005 to 192.168.199.177:7001
M: 9e6f2de4215a6c3854a5aa9263e28bfabeea957b 192.168.199.177:7000
slots:[0-5460] (5461 slots) master
M: 84007ea2683606af23736e1f5e2fd4f544a0c74f 192.168.199.177:7001
slots:[10923-16383] (5461 slots) master
S: d26ad952c3f33f3de67b1ca3afbf43d92875d731 192.168.199.177:7002
replicates 68f24ce9b20941ed21e4ff9b9ed205581aff9248
M: 68f24ce9b20941ed21e4ff9b9ed205581aff9248 192.168.199.132:7003
slots:[5461-10922] (5462 slots) master
S: 4586bbae81e6337217809127f053e8b85f6cf1bc 192.168.199.132:7004
replicates 9e6f2de4215a6c3854a5aa9263e28bfabeea957b
S: d8f09c04ab52f6de00fafa4031b668f3433f8192 192.168.199.132:7005
replicates 84007ea2683606af23736e1f5e2fd4f544a0c74f
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 192.168.199.177:7000)
M: 9e6f2de4215a6c3854a5aa9263e28bfabeea957b 192.168.199.177:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: d26ad952c3f33f3de67b1ca3afbf43d92875d731 192.168.199.177:7002
slots: (0 slots) slave
replicates 68f24ce9b20941ed21e4ff9b9ed205581aff9248
M: 68f24ce9b20941ed21e4ff9b9ed205581aff9248 192.168.199.132:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: d8f09c04ab52f6de00fafa4031b668f3433f8192 192.168.199.132:7005
slots: (0 slots) slave
replicates 84007ea2683606af23736e1f5e2fd4f544a0c74f
S: 4586bbae81e6337217809127f053e8b85f6cf1bc 192.168.199.132:7004
slots: (0 slots) slave
replicates 9e6f2de4215a6c3854a5aa9263e28bfabeea957b
M: 84007ea2683606af23736e1f5e2fd4f544a0c74f 192.168.199.177:7001
slots:[10923-16383] (5461 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.
2、實驗如下
3、報錯如下:
1、[ERR] Node 192.168.199.177:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
解決方法:1、停止服務刪除nodes-*.conf 比如工程上cluster目錄下的nodes-7000.conf nodes-7001.conf
2、flushdb redis服務:./redis-cli -h 192.168.199.177 -p 7000 => 192.168.199.177:7000>flushdb
2、(error) MOVED 6918 192.168.199.132:7003
# ./redis-cli -h 192.168.199.177 -p 7001 => ./redis-cli -c -h 192.168.199.177 -p 7001
實驗要用-c引數來 叢集模式
3、Could not connect to Redis No route to host
開啟: chkconfig iptables on
關閉: chkconfig iptables off
開啟: service iptables start
關閉: service iptables stop
部分埠
vim /etc/sysconfig/iptables
新增想要開啟的相關埠
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8082 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
service iptables restart
4、其他錯誤(因為還不熟,今天剛剛搭建)
解決方法:重啟所有redis服務再執行命令
七、C 使用叢集
[[email protected] redis]# ./redis-cli -c -h 192.168.199.177 -p 7000 script load "$(cat test.lua)"
"4f3566522508785adb19875070f7138600d56f41"
192.168.199.132:7003> set [email protected]_DATA "4f3566522508785adb19875070f7138600d56f41"
-> Redirected to slot [5404] located at 192.168.199.177:7000
OK
1、搭建專案程式碼整合hiredis
[[email protected] log]# more GameServer_0.log
< 6>[20181116-15:39:50] pid[16855]: RedisClient.cpp(70)[ConnectRedisServer]: connect redis server ok,ip[192.168.199.177],port[7000]
< 6>[20181116-15:39:50] pid[16855]: RedisUserData.cpp(66)[ConnectRedisServer]: connect redis success... ip: 192.168.199.177 , port: 7000
< 6>[20181116-15:39:50] pid[16855]: RedisClient.cpp(178)[ExcuteCommand]: Excute cmd success! cmd[GET [email protected]_DATA],str[4f3566522508785ad
b19875070f7138600d56f41]
< 6>[20181116-15:39:50] pid[16855]: RedisUserData.cpp(45)[Init]: show EVALSHA: 4f3566522508785adb19875070f7138600d56f41
< 6>[20181116-15:39:50] pid[16855]: RedisUserData.cpp(47)[Init]: RedisClient Init success...
< 6>[20181116-15:40:49] pid[16858]: RedisClient.cpp(70)[ConnectRedisServer]: connect redis server ok,ip[192.168.199.177],port[7000]
< 6>[20181116-15:40:49] pid[16858]: RedisUserData.cpp(66)[ConnectRedisServer]: connect redis success... ip: 192.168.199.177 , port: 7000
< 6>[20181116-15:40:49] pid[16858]: RedisClient.cpp(178)[ExcuteCommand]: Excute cmd success! cmd[GET [email protected]_DATA],str[4f3566522508785ad
b19875070f7138600d56f41]
< 6>[20181116-15:40:49] pid[16858]: RedisUserData.cpp(45)[Init]: show EVALSHA: 4f3566522508785adb19875070f7138600d56f41
< 6>[20181116-15:40:49] pid[16858]: RedisUserData.cpp(47)[Init]: RedisClient Init success...
< 6>[20181116-15:41:04] pid[16861]: RedisClient.cpp(70)[ConnectRedisServer]: connect redis server ok,ip[192.168.199.177],port[7000]
< 6>[20181116-15:41:04] pid[16861]: RedisUserData.cpp(66)[ConnectRedisServer]: connect redis success... ip: 192.168.199.177 , port: 7000
< 6>[20181116-15:41:04] pid[16861]: RedisClient.cpp(178)[ExcuteCommand]: Excute cmd success! cmd[GET [email protected]_DATA],str[4f3566522508785ad
b19875070f7138600d56f41]
< 6>[20181116-15:41:04] pid[16861]: RedisUserData.cpp(45)[Init]: show EVALSHA: 4f3566522508785adb19875070f7138600d56f41
< 6>[20181116-15:41:04] pid[16861]: RedisUserData.cpp(47)[Init]: RedisClient Init success...
2、使用錯誤
1)evalsha錯誤 (error) CROSSSLOT Keys in request don't hash to the same slot
192.168.199.177:7000> hmset user vip 1 gold 100
-> Redirected to slot [5474] located at 192.168.199.132:7003
OK
192.168.199.132:7003> script load "return redis.call('hget', KEYS[1], KEYS[2])"
"6d9eb8fba24c97e16f2a77d479073016ffe05396"
192.168.199.132:7003> evalsha "6d9eb8fba24c97e16f2a77d479073016ffe05396" 2 user vip
(error) CROSSSLOT Keys in request don't hash to the same slot
解決:給key加上{},field用{key}.field來訪問
192.168.199.132:7003> flushdb
OK
192.168.199.132:7003> hgetall user
(empty list or set)
192.168.199.132:7003> hmset user {user}.vip 1 {user}.gold 100
OK
192.168.199.132:7003> script load "return redis.call('hget', KEYS[1], KEYS[2])"
"6d9eb8fba24c97e16f2a77d479073016ffe05396"
192.168.199.132:7003> evalsha "6d9eb8fba24c97e16f2a77d479073016ffe05396" 2 user {user}.vip
"1"
192.168.199.132:7003> evalsha "6d9eb8fba24c97e16f2a77d479073016ffe05396" 2 user {user}.gold
"100"