reids3.0安装文档
安装文档
1. 安装依赖
yum -y install gcc
yum -y install gcc-c++
yum -y install ruby
yum -y install rubygem*
yum -y install ruby-rdoc
yum -y install ruby-devel rubygems rpm-build
gem install redis
2. 部署方案
1. 安装实例 :准备三台虚拟机 :
192.168.56.200
192.168.56.201
192.168.56.202
2. 在每个虚拟机上部署两个redis :
192.168.56. 200:6379
192.168.56. 200:6378
192.168.56. 201: 6379
192.168.56. 201: 6378
192.168.56. 202: 6379
192.168.56. 202: 6378
3. 安装redis
3.1. 下载
wget http://download.redis.io/releases/redis-3.0.6.tar.gz
3.2. 上传
略
3.3. 解压
略
3.4. 重命名
略
3.5. 修改环境变量
export REDIS_HOME=/usr/local/redis export PATH=$PATH:$REDIS_HOME/bin |
3.6. 编译
cd ./redis/src
make
3.7. 将生成的文件copy到bin下
mkdir ../bin
mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server redis-trib.rb ../bin
cd ..
3.8. 修改配置文件
vi redis.conf
port 6378 cluster-enabled yes cluster-config-file nodes-6378.conf cluster-node-timeout 5000 appendonly yes |
3.9. 复制一份redis在当前目录,修改redis.conf
vi redis.conf
port 6379 cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 5000 appendonly yes |
3.10. 下发
下发到其他机器两份,因为要做主备
3.11. 启动
启动6台redis
./redis-server ./redis.conf
3.12. 运行集群
./redis-trib.rb create --replicas 1 192.168.56.200:6378 192.168.56.200:6379 192.168.56.201:6378 192.168.56.201:6379 192.168.56.202:6378 192.168.56.202:6379 |
3.13. 启动命令行
redis-cli -c
4. 动态添加节点
4.1. 准备两个redis做主备
./redis-server ./redis.conf
4.2. 添加新master 节点
输入命令redis-trib.rb add-node [需要添加节点] [原集群节点]
# ./redis-trib.rb add-node 192.168.118.147:7003 192.168.118.148:7000
4.3. 添加备份节点
./redis-trib.rb add-node --slave --master-id 4841d83e2b037c49bb078fff4b00385bf4927071 192.168.118.148:7003 192.168.118.136:7000
此处的 4841d83e2b037c49bb078fff4b00385bf4927071 为刚刚添加的节点 的 Node id
节点ID(node id) 可在 任意redis bin目录下查看nodes.conf中有记录 还有很多方式 下面有使用到
5. 查看集群节点
redis-cli –c
cluster nodes
6. 给新节点分配 hash 槽
# ./redis-trib.rb reshard [任意节点地址:port]
How many slots do you want to move (from 1 to 16384)? 4096 //由于这又4队主从节点16384/4 [按照情况分配数量]
.......
What is the receiving node ID? 4841d83e2b037c49bb078fff4b00385bf4927071 //注意此处的ID
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: all //也可以是某个节点的ID
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes //此处输入yes 执行操作,输入no 取消分配
分配好之后输入命令 检查 集群状态
# ./redis-trib.rb check [任意节点地址:port]
[root@master addm]# ./redis-trib.rb check 192.168.118.148:7001
Connecting to node 192.168.118.136:7000: OK
Connecting to node 192.168.118.147:7003: OK
Connecting to node 192.168.118.148:7001: OK
Connecting to node 192.168.118.136:7001: OK
Connecting to node 192.168.118.148:7000: OK
Connecting to node 192.168.118.147:7001: OK
Connecting to node 192.168.118.147:7000: OK
>>> Performing Cluster Check (using node 192.168.118.136:7000)
M: 6fb72d5d6f14c67840348c0d14767f2541ae128a 192.168.118.136:7000
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: 4841d83e2b037c49bb078fff4b00385bf4927071 192.168.118.147:7003
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
S: 142ef0fd250be201a4f682163245a98d58d230c3 192.168.118.148:7001
slots: (0 slots) slave
replicates cde6ec4b98a8e2c5d4020a5e545a7b95c533737d
S: efce5470e3de0a2ba08211c7fb0d1a860051ba4a 192.168.118.136:7001
slots: (0 slots) slave
replicates 6fb72d5d6f14c67840348c0d14767f2541ae128a
M: d11986f2ccfad296c503684ac372ece29c587f14 192.168.118.148:7000
slots:1365-5460 (4096 slots) master
1 additional replica(s)
S: 0cc4699066987d99391b0faccddf25e8be44c62c 192.168.118.147:7001
slots: (0 slots) slave
replicates d11986f2ccfad296c503684ac372ece29c587f14
M: cde6ec4b98a8e2c5d4020a5e545a7b95c533737d 192.168.118.147:7000
slots:6827-10922 (4096 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.
结果中
M: 4841d83e2b037c49bb078fff4b00385bf4927071 192.168.118.147:7003
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
7. 集群相关命令
集群
CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。
节点
CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。
CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。
CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。
槽(slot)
CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。
CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。
CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。
CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。
CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。
键
CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。
8. 删除节点
5):删除一个slave节点
Java代码 收藏代码
#redis-trib del-node ip:port '<node-id>'
redis-trib.rb del-node 10.10.34.14:7386 'c7ee2fca17cb79fe3c9822ced1d4f6c5e169e378'
6):删除一个master节点
a):删除master节点之前首先要使用reshard移除master的全部slot,然后再删除当前节点(目前只能把被删除
master的slot迁移到一个节点上)
Java代码 收藏代码
#把10.10.34.14:6386当前master迁移到10.10.34.14:6380上
redis-trib.rb reshard 10.10.34.14:6380
#根据提示选择要迁移的slot数量(ps:这里选择500)
How many slots do you want to move (from 1 to 16384)? 500(被删除master的所有slot数量)
#选择要接受这些slot的node-id(10.10.34.14:6380)
What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2 (ps:10.10.34.14:6380的node-id)
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:f51e26b5d5ff74f85341f06f28f125b7254e61bf(被删除master的node-id)
Source node #2:done
#打印被移动的slot后,输入yes开始移动slot以及对应的数据.
#Do you want to proceed with the proposed reshard plan (yes/no)? yes
b):删除空master节点
Java代码 收藏代码
redis-trib.rb del-node 10.10.34.14:6386 'f51e26b5d5ff74f85341f06f28f125b7254e61bf'
9. 客户端
private static BinaryJedisCluster jc;
static {
//只给集群里一个实例就可以
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6380));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6381));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6382));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6383));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6384));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7380));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7381));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7382));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7383));
jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7384));
jc = new BinaryJedisCluster(jedisClusterNodes);
}
@Test
public void testBenchRedisSet() throws Exception {
final Stopwatch stopwatch = new Stopwatch();
List list = buildBlogVideos();
for (int i = 0; i < 1000; i++) {
String key = "key:" + i;
stopwatch.start();
byte[] bytes1 = protostuffSerializer.serialize(list);
jc.setex(key, 60 * 60, bytes1);
stopwatch.stop();
}
System.out.println("time=" + stopwatch.toString());
}