转载请注明出处:https://oldnoop.tech/c/177.html
安装redis
下载redis的windows版本
https://github.com/MSOpenTech/redis/releases
创建集群文件夹及文件
创建文件夹redis-cluster
在下面创建6个文件夹
redis7001
redis7002
redis7003
redis7004
redis7005
redis7006
解压缩下载的redis压缩包,分别放到上面6个文件夹中
然后在6个文件夹中,均创建redis.conf文件,编写内容如下,
每个文件的端口分别为7001,7002,7003,7004,7005,7006
--------------------------------------------------------
port 7001
loglevel notice
logfile "log"
appendonly yes
cluster-enabled yes
cluster-node-timeout 5000
cluster-config-file nodes.conf
至此,文件目录结构如下
redis-cluster
|-------redis7001
|------redis.conf
|-------redis7002
|------redis.conf
|-------redis7003
|------redis.conf
|-------redis7004
|------redis.conf
|-------redis7005
|------redis.conf
|-------redis7006
|------redis.conf
创建redis服务
打开windows命令行cmd,分别进入上述6个redis文件夹的目录,执行如下命令
redis-server --service-install redis.conf --service-name redis7001
redis-server --service-install redis.conf --service-name redis7002
redis-server --service-install redis.conf --service-name redis7003
redis-server --service-install redis.conf --service-name redis7004
redis-server --service-install redis.conf --service-name redis7005
redis-server --service-install redis.conf --service-name redis7006
启动redis服务
在windows的服务管理中,启动上述创建的redis的服务
搭建集群
安装依赖软件
安装Ruby环境
https://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.2.6-x64.exe
需要使用Ruby Gems 中的redis-trib.rb来创建和操作集群。
安装Ruby Gems
https://rubygems.org/rubygems/rubygems-2.6.12.zip
通过此Ruby包管理来获取操作Redis集群的redis-trib.rb
gem install redis
安装redis依赖
安装redis的驱动
打开命令行cmd, 运行如下命令
gem install redis
安装集群脚本redis-trib
打开链接
https://raw.githubusercontent.com/antirez/redis/unstable/src/redis-trib.rb
另存为
redis-trib.rb
或者
https://github.com/antirez/redis/tree/unstable/src 目录下下载redis-trib.rb
搭建集群
将脚本redis-trib.rb放到任一一个redis目录下,比如redis7001
打开windows命令行,执行如下命令
ruby redis-trib.rb create --replicas 1 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 127.0.0.1:7006
说明:
--replicas 1 表示每个主数据库拥有从数据库个数为1。master节点不能少于3个,所以用了6个redis
由于redis-trib.rb 对域名或主机名(如localhost)支持不好,故在创建集群的时候要使用ip:port的方式
创建集群时报某个err slot 0 is already busy (redis::commanderror)
这是由于之间创建集群没有成功,需要将nodes.conf和dir里面的文件全部删除(注意不要删除了redis.conf)
重新启动redis节点,再次执行redis-trib.rb
提示是否满意集群配置
>ruby redis-trib.rb create --replicas
1 localhost:7001 localhost:7002 localhost:7003 localhost:7004 localhost:7005 localhost:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
localhost:7001
localhost:7002
localhost:7003
Adding replica localhost:7005 to localhost:7001
Adding replica localhost:7006 to localhost:7002
Adding replica localhost:7004 to localhost:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 3a7fbacb4a19ce2650c09e1857d84cddc3941002 localhost:7001
slots:0-5460 (5461 slots) master
M: 3282f3123109c841f564db5376eebdde42554850 localhost:7002
slots:5461-10922 (5462 slots) master
M: 85edcdc2d1b83ea1dec50c08ce56f0cec4611128 localhost:7003
slots:10923-16383 (5461 slots) master
S: 45a964b55607454061d3f8e0097179981fb75ffe localhost:7004
replicates 85edcdc2d1b83ea1dec50c08ce56f0cec4611128
S: f4a100b1d7a298e6cb4181dd5150d27e34aefaee localhost:7005
replicates 3a7fbacb4a19ce2650c09e1857d84cddc3941002
S: f4a100b1d7a298e6cb4181dd5150d27e34aefaee localhost:7006
replicates 3282f3123109c841f564db5376eebdde42554850
说明:
上面的M 表示 主服务器,S表示从服务器,每个主服务器都有1个从服务器
从服务器的信息里的 replicates 指明了用的是哪个主
这里是提示信息: 输入yes
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:7001)
M: 06fa9f941315a380817513adc9960d0f418abbb7 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 51d77e39842b59bc3b2ece262d4d8ebbff27d6e8 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 993d2d48a928c5b6f9e16dbe863121d44f7affd0 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: ece490c440e706de873de05b0fc4ee4aa7867067 127.0.0.1:7005
slots: (0 slots) slave
replicates 51d77e39842b59bc3b2ece262d4d8ebbff27d6e8
S: 78afaa127c978cde6388105711525769073b6a9d 127.0.0.1:7004
slots: (0 slots) slave
replicates 06fa9f941315a380817513adc9960d0f418abbb7
S: 35246694a7fb0957f583823436c01d1791966308 127.0.0.1:7006
slots: (0 slots) slave
replicates 993d2d48a928c5b6f9e16dbe863121d44f7affd0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>
说明:
All 16384 slots covered. redis集群对存储的数据进行了分片,划分了16384个槽,
测试集群
可以从任一一个redis节点登录,进行操作,
如果添加或者查询数据不在当前操作节点,redis集群返回moved信息