redis之集群策略-集群搭建 redis-5.0.0版本

使用的是redis-5.0.0版本

本次搭建需要六台redis服务器,我通过在一台虚拟机上模拟6台机器。

一,准备工作

1. 在/usr/local目录下新建一个redis-cluster文件夹,并在redis-cluster文件夹下新建6个子文件夹,7001,7002,7003,7004,7005,7006。

2.

cd /usr/local/redis-4.0.0/
cp redis.conf /usr/local/redis-cluster/7001
cp redis.conf /usr/local/redis-cluster/7002
cp redis.conf /usr/local/redis-cluster/7003
cp redis.conf /usr/local/redis-cluster/7004
cp redis.conf /usr/local/redis-cluster/7005
cp redis.conf /usr/local/redis-cluster/7006

 

进入redis目录将redis.conf文件复制到7001,7002,7003,7004,7005,7006,各一份。

二,配置文件(7001,7002,7003,7004,7005,7006每个文件夹都需要做以下配置)

vim redis.conf

做一下修改这里以7001为例,其他几个步骤一样

1. daemonize yes      将redis设置为后台启动 

2. port 7001               设置端口(因为在一台机器上模拟必须为不同的端口,如果在6台机器不需要配置)

3. bind 127.0.0.1  改为bind 192.168.159.140        (绑定当前机器的ip地址,比如当前端口为192.168.159.140)

4. dir /usr/local/redis-sluster/7001               指定数据文件的存放位置,必须指定不同的存放位置,不然会丢失数据

5. cluster-enable yes         开启集群模式

6. cluster-config-file nodes7001.conf    

7.cluster-node-timeout  5000      5000也可以是别的值

8.appendonly yes                 开启aof持久化  也可以不开启  默认使用rdb

9.protected-mode no

以上(7001,7002,7003,7004,7005,7006)每个文件夹下的redis.conf都需要配置。

三,启动6个redis实例

/usr/local/redis-4.0.0/src/redis-server /usr/local/redis-cluster/7001/redis.conf
/usr/local/redis-4.0.0/src/redis-server /usr/local/redis-cluster/7002/redis.conf
/usr/local/redis-4.0.0/src/redis-server /usr/local/redis-cluster/7003/redis.conf
/usr/local/redis-4.0.0/src/redis-server /usr/local/redis-cluster/7004/redis.conf
/usr/local/redis-4.0.0/src/redis-server /usr/local/redis-cluster/7005/redis.conf
/usr/local/redis-4.0.0/src/redis-server /usr/local/redis-cluster/7006/redis.conf

查看状态

ps -ef | grep redis

出现

root      13809      1  0 12:12 ?        00:00:00 usr/local/redis-4.0.0/src/redis-server 192.168.159.140:7001 [cluster]          
root      13820      1  0 12:12 ?        00:00:00 usr/local/redis-4.0.0/src/redis-server 192.168.159.140:7002 [cluster]          
root      13825      1  0 12:12 ?        00:00:00 usr/local/redis-4.0.0/src/redis-server 192.168.159.140:7003 [cluster]          
root      13830      1  0 12:12 ?        00:00:00 usr/local/redis-4.0.0/src/redis-server 192.168.159.140:7004 [cluster]          
root      13835      1  0 12:12 ?        00:00:00 usr/local/redis-4.0.0/src/redis-server 192.168.159.140:7005 [cluster]          
root      13840      1  0 12:12 ?        00:00:00 usr/local/redis-4.0.0/src/redis-server 192.168.159.140:7006 [cluster]          
root      13868  10883  0 12:14 pts/0    00:00:00 grep redis

启动成功。

四, 进入redis/src下

cd /usr/local/redis-4.0.0/src

执行

redis-5.0.0版本集群使用redis-cli,redis-trib在这个版本停止使用。

# ./redis-cli --cluster create  192.168.159.140:7000 192.168.159.140:7001 192.168.159.140:7002 192.168.159.140:7003 192.168.159.140:7004 1192.168.159.140:7005 --cluster-replicas 1

1    表示  master:slave=1 ,意味着主从关系是一一对应的7001-7004,7002-7005,7003-7006,设立这种主从关系主要是为了数据的备份,防止服务器坏掉数据丢失。

会出现 

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.159.140:7001
192.168.159.140:7002
192.168.159.140:7003
Adding replica 192.168.159.140:7004 to 192.168.159.140:7001
Adding replica 192.168.159.140:7005 to 192.168.159.140:7002
Adding replica 192.168.159.140:7006 to 192.168.159.140:7003
M: daf29b7d0f4177c7572f8c9d189eddae3f0c2a42 192.168.159.140:7001
   slots:0-5460 (5461 slots) master
M: a52bc186d7415803ed3afb0f8437f787895a7b41 192.168.159.140:7002
   slots:5461-10922 (5462 slots) master
M: 4b698e721c6e41a21caa1a76ed594252e1be8745 192.168.159.140:7003
   slots:10923-16383 (5461 slots) master
S: 9840ed409fc40f12c03c559aaaeffa7e67780d8f 192.168.159.140:7004
   replicates daf29b7d0f4177c7572f8c9d189eddae3f0c2a42
S: f73a885380ee584e99a1be81c5b0a7f3dfc6c074 192.168.159.140:7005
   replicates a52bc186d7415803ed3afb0f8437f787895a7b41
S: a9d5642c4abe6b85511148e8226a7e7017a56dca 192.168.159.140:7006
   replicates 4b698e721c6e41a21caa1a76ed594252e1be8745
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.159.140:7001)
M: daf29b7d0f4177c7572f8c9d189eddae3f0c2a42 192.168.159.140:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 9840ed409fc40f12c03c559aaaeffa7e67780d8f 192.168.159.140:7004
   slots: (0 slots) slave
   replicates daf29b7d0f4177c7572f8c9d189eddae3f0c2a42
M: 4b698e721c6e41a21caa1a76ed594252e1be8745 192.168.159.140:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: f73a885380ee584e99a1be81c5b0a7f3dfc6c074 192.168.159.140:7005
   slots: (0 slots) slave
   replicates a52bc186d7415803ed3afb0f8437f787895a7b41
S: a9d5642c4abe6b85511148e8226a7e7017a56dca 192.168.159.140:7006
   slots: (0 slots) slave
   replicates 4b698e721c6e41a21caa1a76ed594252e1be8745
M: a52bc186d7415803ed3afb0f8437f787895a7b41 192.168.159.140:7002
   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.

此时集群已经搭建成功。可以看到redis是分槽存储一共16387个槽,现在显示7001:0-5460,7002:5461-10922,7003:10923-16387。从这我们也可以看到主从的身影。

我们来测试是怎么分的

 

/usr/local/redis-4.0.0/src/redis-cli -c -h 192.168.159.140 -p 7001

-c 表示集群 -h ip地址  -p 端口号

192.168.159.140:7001> set name cluster
-> Redirected to slot [5798] located at 192.168.159.140:7002
OK

Redirected to slot [5798] located at 192.168.159.140:7002 就是说数据被存储到了第6798个槽里 ,这个槽在7002上。

我们来测试7001有没有数据。

192.168.159.140:7001> keys *
(empty list or set)

空的,测试7002和7005 

192.168.159.140:7002> keys *
1) "name"
192.168.159.140:7005> keys *
1) "name"

7002和7005都有数据。

取数据在7001测试:

192.168.159.140:7001> get name
-> Redirected to slot [5798] located at 192.168.159.140:7002
"cluster"

虽然7001没有数据但因为集群了,会定位到7002然后取出数据。

其实是这样存储数据的,当拿到key时,利用hash算法target = hash(key)\%N,这里N=16387 为槽的数量,根据每个主服务器的所拥有槽的范围和target,就得到了在哪台服务器的哪个槽上,进而将数据存储进去。 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值