redis集群

Posted claireyuancy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis集群相关的知识,希望对你有一定的参考价值。

准备工作:

首先必须安装redis,安装完后,安装例如以下内容:

</pre><pre name="code" class="html"> sudo yum install ruby
 sudo yum install rubygem
 yum install -y ruby-devel ruby-docs ruby-ri ruby-rdoc  
 yum install -y rubygems  
 gem install redis


创建集群须要的文件夹

mkdir -p /usr.local/cluster
cd /usr.local/cluster
mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005

cp /home/sq/redis.conf /usr.local/cluster改动配置文件redis.conf

vi redis.conf
##改动配置文件里的以下选项
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
##改动完redis.conf配置文件里的这些配置项之后把这个配置文件分别复制到7000/7001/7002/7003/7004/7005文件夹以下
cp /usr/local/cluster/redis.conf /usr/local/cluster/7000
cp /usr/local/cluster/redis.conf /usr/local/cluster/7001
cp /usr/local/cluster/redis.conf /usr/local/cluster/7002
cp /usr/local/cluster/redis.conf /usr/local/cluster/7003
cp /usr/local/cluster/redis.conf /usr/local/cluster/7004
cp /usr/local/cluster/redis.conf /usr/local/cluster/7005
 
##注意:拷贝完毕之后要改动7001/7002/7003/7004/7005文件夹以下redis.conf文件里的port參数,分别改为相应的文件夹的名称


cd /usr/local/cluster/7000分别启动这6redis实例

redis-server redis.conf
cd /usr/local/cluster/7001
redis-server redis.conf
cd /usr/local/cluster/7002
redis-server redis.conf
cd /usr/local/cluster/7003
redis-server redis.conf
cd /usr/local/cluster/7004
redis-server redis.conf
cd /usr/local/cluster/7005
redis-server redis.conf



创建集群

./redis-trib.rb create --replicas  1 127.0.0.1:7000  127.0.0.1:7001  127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005


报错,例如以下所看到的

Connecting to node 127.0.0.1:7001: [ERR] Sorry, can‘t connect to node 127.0.0.1:7001


下载最新的配置文件。反复上面集群命令。成功。

>>> Creating cluster
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: d8bf7d3d9f38c7616e6347a5a2d054d8fa978397 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 6fc3ec10d8c2f472509c470faff2b111f37f7eea 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 889968778bf2780e9fe9f1ea89057f3bad0f5675 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: 0584aa26b3fd35bbe8b03a25b2f51e99c89ae82e 127.0.0.1:7003
   replicates d8bf7d3d9f38c7616e6347a5a2d054d8fa978397
S: a62f3805aa998b9721e66d36c62d2c0b50b74288 127.0.0.1:7004
   replicates 6fc3ec10d8c2f472509c470faff2b111f37f7eea
S: e430fb63fce30514d4c4534f0e3e32b3287925bb 127.0.0.1:7005
   replicates 889968778bf2780e9fe9f1ea89057f3bad0f5675
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 127.0.0.1:7000) M: d8bf7d3d9f38c7616e6347a5a2d054d8fa978397 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: 6fc3ec10d8c2f472509c470faff2b111f37f7eea 127.0.0.1:7001 slots:5461-10922 (5462 slots) master M: 889968778bf2780e9fe9f1ea89057f3bad0f5675 127.0.0.1:7002 slots:10923-16383 (5461 slots) master M: 0584aa26b3fd35bbe8b03a25b2f51e99c89ae82e 127.0.0.1:7003 slots: (0 slots) master replicates d8bf7d3d9f38c7616e6347a5a2d054d8fa978397 M: a62f3805aa998b9721e66d36c62d2c0b50b74288 127.0.0.1:7004 slots: (0 slots) master replicates 6fc3ec10d8c2f472509c470faff2b111f37f7eea M: e430fb63fce30514d4c4534f0e3e32b3287925bb 127.0.0.1:7005 slots: (0 slots) master replicates 889968778bf2780e9fe9f1ea89057f3bad0f5675 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.


測试:

redis-cli -c -p 7000
[[email protected] 7005]# redis-cli -c -p 7000

127.0.0.1:7000> sadd sq 123
-> Redirected to slot [13244] located at 127.0.0.1:7002
(integer) 1
127.0.0.1:7002> keys sq
1) "sq"
127.0.0.1:7002> keys *
1) "sq"
127.0.0.1:7002> smembers sq
1) "123"
















以上是关于redis集群的主要内容,如果未能解决你的问题,请参考以下文章

java怎么从多台redis集群取数据库

我在工作中遇到的redis集群使用

redis集群概念

python连接redis,redis集群

Python3 redis集群连接 (带密码验证)

c#程序怎么调用redis集群