In this post, I will explain how to create cluster for Redis 3.2 on Windows.
- Create 3 master Redis Nodes on the same server, port: 7000, 7001, 7002
- Create 3 slave Redis Nodes on the same server, port: 7003, 7004, 7005
- Master Slave: 7000 – 7003; 7001-7004; 7002-7005.
Redis 3.2 on Windows Resource
- Redis Official Site: https://redis.io/
- Redis Cluster Tutorial: https://redis.io/topics/cluster-tutorial
- Redis replication: https://redis.io/topics/replication
- Redis on Windows GitHub Project: https://github.com/MicrosoftArchive/redis
- Installing Redis: https://www.javacodegeeks.com/2015/09/redis-clustering.html#InstallingRedis
Configure Redis
There are several minimum configuration we need to do for Cluster. See https://redis.io/topics/cluster-tutorial
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
配置项前面不能有#号(此为注释符),也不能有空格
Create Redis Windows Services
Follow my last post to create 6 Redis Nodes with ports: 7000, 7001, 7002, 7003, 7004, 7005
Start the 6 Redis nodes.
redis-server --service-install redis.windows-service.conf --service-name Redis7000 --port 7000
redis-server --service-start --service-name Redis7000
NOTE THAT YOU SHOULD CONFIG redis.windows-service.conf FILE RATHER THAN redis.windows.conf
服务的卸载
使用sc delete redis7000
然后重启即可。(上面的命令只是将该服务标记为卸载状态,需要重启将其真正卸载)
Configure Master Nodes
此部分参考
https://blog.csdn.net/hao495430759/article/details/80540407
http://www.redis.cn/topics/cluster-tutorial.html
https://www.cnblogs.com/super-chao/p/9329018.html
-
下载Ruby并安装:
下载地址:http://railsinstaller.org/en -
完成后进行配置
ruby -v
gem -v
gem sources
gem install redis
-
下载对应版本的redis发行版源码,拷贝src文件夹下的redis-trib.rb文件至7000的平行目录
-
执行
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
Test Cluster
Please use -c option, which means enabling cluster. Otherwise, you will get (ERROR) MOVED to ……
$ redis-cli -c -p 7000
redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"
常见错误:
- 错误一:
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
解决方案
原因是redis-trib.rb的链接指向官网最新的版本。从对应版本(redis3.2.0即可)的源码压缩包中src文件夹下找到对应的redis-trib.rb文件使用,即可解决问题。
下载redis源码版压缩包:http://download.redis.io/releases/- 错误二:
(error) MOVED
解决方案:
这种情况一般是因为启动redis-cli时没有设置集群模式所导致。
redis-cli -c -p 7000- 错误三:
Node is not empty
1)通过sc delete将服务卸载并重启服务器
2)删除结点下的aof、rdb、nodes.conf文件
3)重新安装服务
命令相关:
- 查看redis版本信息
redis-cli -p 7000
输入info- 查看集群主从情况
redis-cli -c -p 7000 cluster nodes