redis 学习问题总结 | http://aperise.iteye.com/blog/2310639 |
ehcache memcached redis 缓存技术总结 | http://aperise.iteye.com/blog/2296219 |
redis-stat 离线安装 | http://aperise.iteye.com/blog/2310254 |
redis cluster 非ruby方式启动 | http://aperise.iteye.com/blog/2310254 |
redis-sentinel安装部署 | http://aperise.iteye.com/blog/2342693 |
spring-data-redis使用 | http://aperise.iteye.com/blog/2342615 |
redis客户端redisson实战 | http://blog.csdn.net/zilong_zilong/article/details/78252037 |
redisson-2.10.4源代码分析 | http://blog.csdn.net/zilong_zilong/article/details/78609423 |
tcmalloc jemalloc libc选择 | http://blog.csdn.net/u010994304/article/details/49906819 |
redis cluster 非ruby方式启动
1.主要解决问题
redis cluster在redis官网介绍是用ruby脚本启动(我在之前博客有介绍ehcache memcached redis 缓存技术总结)。
ruby脚本干了啥??1)首先将集群节点进行握手 2)握手完毕后分配槽位 3)分配完槽位后就指定主从,ruby脚本的好处是这些细节我们不用去过多关注。
缺失了ruby脚本,我们如何玩转redis cluster?????
这里就是要解决此问题。
2.非ruby方式启动redis cluster
2.1 独立启动各个redis节点
#杀redis进程
pkill redis
#删除AOF RDB文件
rm -rf /home/hadoopmanage/rediscluster/conf/*/nodes.conf
rm -rf /home/hadoopmanage/rediscluster/conf/*/dump.rdb
rm -rf /home/hadoopmanage/rediscluster/conf/*/appendonly.aof
#独立启动各个redis节点
cd /home/hadoopmanage/rediscluster/conf/7000/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7001/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7002/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7003/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7004/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7005/
/opt/redis-3.2.0/src/redis-server redis.conf
pkill redis
#删除AOF RDB文件
rm -rf /home/hadoopmanage/rediscluster/conf/*/nodes.conf
rm -rf /home/hadoopmanage/rediscluster/conf/*/dump.rdb
rm -rf /home/hadoopmanage/rediscluster/conf/*/appendonly.aof
#独立启动各个redis节点
cd /home/hadoopmanage/rediscluster/conf/7000/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7001/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7002/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7003/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7004/
/opt/redis-3.2.0/src/redis-server redis.conf
cd /home/hadoopmanage/rediscluster/conf/7005/
/opt/redis-3.2.0/src/redis-server redis.conf
2.2 集群节点握手
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.202.131 -p 7000
CLUSTER MEET 192.168.202.131 7000
CLUSTER MEET 192.168.202.131 7001
CLUSTER MEET 192.168.202.131 7002
CLUSTER MEET 192.168.202.131 7003
CLUSTER MEET 192.168.202.131 7004
CLUSTER MEET 192.168.202.131 7005
redis-cli -h 192.168.202.131 -p 7000
CLUSTER MEET 192.168.202.131 7000
CLUSTER MEET 192.168.202.131 7001
CLUSTER MEET 192.168.202.131 7002
CLUSTER MEET 192.168.202.131 7003
CLUSTER MEET 192.168.202.131 7004
CLUSTER MEET 192.168.202.131 7005
2.3 集群节点槽位分配
#设置好主从关系之后,就可以用cluster addslots命令指派16384个槽的位置了。有点恶心的是,
#ADDSLOTS命令需要在参数中一个个指明槽的ID,而不能指定范围。
#这里用Bash 3.0的特性简化了,不然就得用Bash的循环来完成了:
redis-cli -h 192.168.202.131 -p 7000 cluster addslots {0..5461}
redis-cli -h 192.168.202.131 -p 7001 cluster addslots {5462..10922}
redis-cli -h 192.168.202.131 -p 7002 cluster addslots {10923..16383}
#ADDSLOTS命令需要在参数中一个个指明槽的ID,而不能指定范围。
#这里用Bash 3.0的特性简化了,不然就得用Bash的循环来完成了:
redis-cli -h 192.168.202.131 -p 7000 cluster addslots {0..5461}
redis-cli -h 192.168.202.131 -p 7001 cluster addslots {5462..10922}
redis-cli -h 192.168.202.131 -p 7002 cluster addslots {10923..16383}
2.4 查看集群节点ID,目的是后面分配主从会用到
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.202.131 -p 7000
cluster nodes
redis-cli -h 192.168.202.131 -p 7000
cluster nodes
查看的信息如下:
0ba73a42688039745f71adc4eb9bd33d96ec6067 192.168.202.131:7002 master - 0 1465648931751 2 connected 10923-16383
6815d0291ec49f0a96c5ab6bf9f8dd622c16a9e5 192.168.202.131:7000 myself,master - 0 0 5 connected 0-5461
12352c839c530280a41da265ac821bc1014e8162 192.168.202.131:7001 master - 0 1465648929712 1 connected 5462-10922
3b7c9618bf909f88247f171184ba24841d5a87f4 192.168.202.131:7004 master - 0 1465648927677 4 connected
081afc3f41c603e6364d77a9ec63840cdf90f4f3 192.168.202.131:7003 master - 0 1465648926652 3 connected
7250093127bc8bce64a5f47b2dd13b4ac8c3fa4c 192.168.202.131:7005 master - 0 1465648930733 0 connected
6815d0291ec49f0a96c5ab6bf9f8dd622c16a9e5 192.168.202.131:7000 myself,master - 0 0 5 connected 0-5461
12352c839c530280a41da265ac821bc1014e8162 192.168.202.131:7001 master - 0 1465648929712 1 connected 5462-10922
3b7c9618bf909f88247f171184ba24841d5a87f4 192.168.202.131:7004 master - 0 1465648927677 4 connected
081afc3f41c603e6364d77a9ec63840cdf90f4f3 192.168.202.131:7003 master - 0 1465648926652 3 connected
7250093127bc8bce64a5f47b2dd13b4ac8c3fa4c 192.168.202.131:7005 master - 0 1465648930733 0 connected
2.5 分配主从关系
#将节点192.168.202.131:7003设置为192.168.202.131:7000的从节点,6815d0291ec49f0a96c5ab6bf9f8dd622c16a9e5是192.168.202.131:7000的标识
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.202.131 -p 7003
cluster replicate 6815d0291ec49f0a96c5ab6bf9f8dd622c16a9e5
quit
#将节点192.168.202.131:7004设置为192.168.202.131:7001的从节点,12352c839c530280a41da265ac821bc1014e8162 是192.168.202.131:7001的标识
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.202.131 -p 7004
cluster replicate 12352c839c530280a41da265ac821bc1014e8162
quit
#将节点192.168.202.131:7005设置为192.168.202.131:7002的从节点,0ba73a42688039745f71adc4eb9bd33d96ec6067 是192.168.202.131:7002的标识
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.202.131 -p 7005
cluster replicate 0ba73a42688039745f71adc4eb9bd33d96ec6067
quit
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.202.131 -p 7003
cluster replicate 6815d0291ec49f0a96c5ab6bf9f8dd622c16a9e5
quit
#将节点192.168.202.131:7004设置为192.168.202.131:7001的从节点,12352c839c530280a41da265ac821bc1014e8162 是192.168.202.131:7001的标识
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.202.131 -p 7004
cluster replicate 12352c839c530280a41da265ac821bc1014e8162
quit
#将节点192.168.202.131:7005设置为192.168.202.131:7002的从节点,0ba73a42688039745f71adc4eb9bd33d96ec6067 是192.168.202.131:7002的标识
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.202.131 -p 7005
cluster replicate 0ba73a42688039745f71adc4eb9bd33d96ec6067
quit
3.redis命令
3.1 redis常见命令
集群
CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。
节点
CLUSTER MEET 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
CLUSTER FORGET 从集群中移除 node_id 指定的节点。
CLUSTER REPLICATE 将当前节点设置为 node_id 指定的节点的从节点。
CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。
槽(slot)
CLUSTER ADDSLOTS [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。
CLUSTER DELSLOTS [slot ...] 移除一个或多个槽对当前节点的指派。
CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
CLUSTER SETSLOT NODE 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
CLUSTER SETSLOT MIGRATING 将本节点的槽 slot 迁移到 node_id 指定的节点中。
CLUSTER SETSLOT IMPORTING 从 node_id 指定的节点中导入槽 slot 到本节点。
CLUSTER SETSLOT STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。
键
CLUSTER KEYSLOT 计算键 key 应该被放置在哪个槽上。
CLUSTER COUNTKEYSINSLOT 返回槽 slot 目前包含的键值对数量。
CLUSTER GETKEYSINSLOT 返回 count 个 slot 槽中的键。
CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。
节点
CLUSTER MEET
CLUSTER FORGET
CLUSTER REPLICATE
CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。
槽(slot)
CLUSTER ADDSLOTS
CLUSTER DELSLOTS
CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
CLUSTER SETSLOT
CLUSTER SETSLOT
CLUSTER SETSLOT
CLUSTER SETSLOT
键
CLUSTER KEYSLOT
CLUSTER COUNTKEYSINSLOT
CLUSTER GETKEYSINSLOT
3.2 客户端常用命令(待完善)
3.2.1 清理数据
flushdb // 清除当前数据库的所有keys
flushall // 清除所有数据库的所有keys
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.173.23 -p 6379
flushall
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.173.23 -p 6380
flushall
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.173.24 -p 6380
flushall
flushall // 清除所有数据库的所有keys
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.173.23 -p 6379
flushall
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.173.23 -p 6380
flushall
cd /opt/redis-3.2.0/src/
redis-cli -h 192.168.173.24 -p 6380
flushall