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、运行成功如下:
[root@localhost 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 使用集群

[root@localhost redis]# ./redis-cli -c -h 192.168.199.177 -p 7000 script load "$(cat test.lua)" 
"4f3566522508785adb19875070f7138600d56f41"
192.168.199.132:7003> set EVALSHA@USER_DATA "4f3566522508785adb19875070f7138600d56f41"
-> Redirected to slot [5404] located at 192.168.199.177:7000
OK

1、搭建项目代码集成hiredis

[root@localhost 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 EVALSHA@USER_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 EVALSHA@USER_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 EVALSHA@USER_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"

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值