Windows Redis集群 RedisCluster

Windows下搭建 Redis 集群

 Redis集群

 如果部署到多台电脑,就跟普通的集群一样;因为Redis是单线程处理的,多核CPU也只能使用一个核,

所以部署在同一台电脑上,通过运行多个Redis实例组成集群,然后能提高CPU的利用率。

在Windows系统下搭建Redis集群:

    需要3个部件:

  Redis、Ruby语言运行环境、创建Redis集群的工具redis-trib.rb

     安装Redis,并运行3个实例(Redis集群需要至少3个以上节点,低于3个无法创建);

     使用redis-trib.rb工具来创建Redis集群,由于该文件是用ruby语言写的,所以需要安装Ruby开发环境

1.下载并安装Redis

     如下路径:https://github.com/MicrosoftArchive/redis/releases

  1. 将下载到的Redis-x64-3.2.100.zip解压即可,放在根目录下,并修改目录名为Redis,如:C:\Redis。
  2. 通过配置文件来启动3个不同的Redis实例,这里使用了6379、6380、6381来运行3个Redis实例。
  3. 打开目录6379下有一个文件 redis.windows.conf,修改里面的端口号,以及集群支持配置。

注意:为了避免不必要的错误,配置文件尽量保存为utf8格式; 

修改redis.windows.conf配置端口,三个实例分别改成6379、6380、6381。如:

port 6379

修改其他配置支持集群,找到对应的参数去掉注释:

cluster-enabled yes                       #是否开启集群
cluster-config-file nodes-6379.conf       #是为该节点的配置信息。服务启动后会在目录生成该文件。
cluster-node-timeout 15000                #那么在创建集群的时候,不会超时。
appendonly yes                            #数据的保存为aof格式

编写一个 bat 来启动 redis,在每个节点目录下建立 start.bat,内容如下:

title redis-6379
redis-server.exe redis.windows.conf

如此一个Redis实例配置完成了,复制该实例,并分别修改端口为6380、6381。就获得了三个实例。

2.下载并安装ruby

  2.1. 下载路径如下:http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.3.3.exe

      下载后,双击安装即可,同样,为了操作方便,也是建议安装在盘符根目录下,如: C:\Ruby22-x64 ,安装时这里选中后两个选项,

  意思是将ruby添加到系统的环境变量中,在cmd命令中能直接使用ruby的命令

   2.2.下载Redis官方提供的创建Redis集群的ruby脚本文件redis-trib.rb,路径如下:

    https://raw.githubusercontent.com/MSOpenTech/redis/3.0/src/redis-trib.rb

           打开该链接如果没有下载,而是打开一个页面,那么将该页面保存为redis-trib.rb

           保存到C:\Redis的目录下。

  3.创建Redis集群  

     cmd切换到C:\Redis目录,使用redis-trib.rb来创建Redis集群:

redis-trib.rb create --replicas 0 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 

  执行结果:

C:\Redis>redis-trib.rb create --replicas 0 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381
>>> Creating cluster
Connecting to node 127.0.0.1:6379: OK
Connecting to node 127.0.0.1:6380: OK
Connecting to node 127.0.0.1:6381: OK
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:6379
127.0.0.1:6380
127.0.0.1:6381
M: 22349aa7569a82d1e6a34e9e639bccd8c43181b9 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: 73f9e74d98b6e9c875628dc74b686f9a22218fb5 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
M: 6a52b024e3cc9e8ba307dc41f21522287bbc7703 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
Can I set the above configuration? (type 'yes' to accept):

     当出现提示时,需要手动输入yes,输入后,当出现以下内容,说明已经创建了Redis集群

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:6379)
M: 22349aa7569a82d1e6a34e9e639bccd8c43181b9 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: 73f9e74d98b6e9c875628dc74b686f9a22218fb5 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
M: 6a52b024e3cc9e8ba307dc41f21522287bbc7703 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

C:\Redis>

测试    

 检验是否真的创建成功,输入以下命令:

命令 redis-cli –c –h ”地址” –p "端口号" ;  c 表示集群

C:\Redis\6379>redis-cli -c -h 127.0.0.1 -p 6379
127.0.0.1:6379>

 

输入dbsize查询 记录总数

127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379>

输入cluster info可以从客户端的查看集群的信息

127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:355
cluster_stats_messages_received:355
127.0.0.1:6379>

参考:

https://blog.csdn.net/zsg88/article/details/73715947

https://www.cnblogs.com/tommy-huang/p/6240083.html

https://www.cnblogs.com/yaozb/p/6911395.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章