windows版本Redis集群搭建

1.前言

  最近做的一个爬虫服务由于需要大量的存取key到Redis,之前搭建的是一个在windows10系统上(I7CPU,16G内存)单机版Redis,运行时间久了以后,Redis总是会莫名其妙连接不上了,这次趁有时间就研究了下windows版本的redis集群,下面开始讲述搭建的过程以及使用中需要注意的点。

      如果部署到多台电脑,就跟普通的集群一样;因为Redis是单线程处理的,多核CPU也只能使用一个核,所以部署在同一台电脑上,通过运行多个Redis实例组成集群,然后能提高CPU的利用率。

1.1 需要的环境以及工具

  在Windows系统下搭建Redis集群需要4个部件:

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

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

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

Rediswindows版本包下载地址

Ruby包下载地址 

Rubby驱动下载地址

1.2 开始安装redis

  搭建集群最少3个redis实例,生产环境最少6个,3主3从,本次我起了6个实例,将下载的压缩包复制6份

windows版本Redis集群搭建分别以对应的端口号命名,然后分别修改各自的配置文件

搭建集群需要修改的配置如下:

port 6380  #端口号     
loglevel notice    
logfile "D:/Redis/Logs/redis6380_log.txt"       
appendonly yes
appendfilename "appendonly.6380.aof"   
cluster-enabled yes     #是否开启集群                               
cluster-config-file nodes.6380.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

如果需要设置集群密码,则需要配置

requirepass 123456
masterauth 123456

 主要每个节点的密码必须一致,不然创建集群的时候会提示连接失败

修改对应的配置文件redis.windows.conf完成后,然后开始安装redis服务

命令如下:

安装服务:redis-server.exe --service-install redis.windows.conf --service-name redis-6379 --port 6379
启动服务:redis-server.exe --service-start --service-name redis-6379
停止服务:redis-server.exe --service-stop --service-name redis-6379
卸载服务:redis-server.exe --service-uninstall --service-name redis-6379

 分别安装对应的6个节点

windows版本Redis集群搭建

 

注意:这里有一个问题,如果当前redis之前是作为单机版本使用存档的一些数据,如

windows版本Redis集群搭建

 

 需要先删除文件夹下面的文件,否则启动的Redis实例会自动终止。

安装好对应的redis服务后,

然后运行入选命令:如果搭建3主3从,命令如下(1代表每个主库有1个从库)

ruby redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385

该命令需要ruby的环境,

windows版本Redis集群搭建

 

 至于怎么安装ruby的环境,请自行百度吧O(∩_∩)O

创建集群的环境中,会出现如下 Can I set the above configuration? (type ‘yes‘ to accept):   输入 yes

windows版本Redis集群搭建

 

 集群配置成功

二.可能遇到的问题

1.[ERR] Node 127.0.0.1:6380 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

原因:我之前先在6380启动的单例模式,并设置了一些数据

解决方案:删除数据和配置文件(请确保数据可以删除)

2.WARNING: redis-trib.rb is not longer available!

原因:redis-trib.rb文件有误或者版本不匹配

3.Endpoint 127.0.0.1:6381 serving hashslot 15078 is not reachable at this point of time. Please check connectTimeout value. If it is low, try increasing it to give the ConnectionMultiplexer a chance to recover from the network disconnect. IOCP: (Busy=0,Free=1000,Min=200,Max=1000), WORKER: (Busy=1,Free=32766,Min=200,Max=32767), Local-CPU: n/a”

StackExchange.Redis.RedisConnectionException: Endpoint 127.0.0.1:6381 serving hashslot 7982 is not reachable at this point of time. Please check connectTimeout value. If it is low, try increasing it to give the ConnectionMultiplexer a chance to recover from the network disconnect. IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=1,Free=32766,Min=4,Max=32767), Local-CPU: n/a
   at Abp.Runtime.Caching.Redis.AbpRedisCache.SetAsync(String key, Object value, Nullable`1 slidingExpireTime, Nullable`1 absoluteExpireTime)
   at Abp.Runtime.Caching.CacheBase.GetAsync(String key, Func`2 factory) || StackExchange.Redis.RedisConnectionException: Endpoint 127.0.0.1:6381 serving hashslot 7982 is not reachable at this point of time. Please check connectTimeout value. If it is low, try increasing it to give the ConnectionMultiplexer a chance to recover from the network disconnect. IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=1,Free=32766,Min=4,Max=32767), Local-CPU: n/a
   at Abp.Runtime.Caching.Redis.AbpRedisCache.SetAsync(String key, Object value, Nullable`1 slidingExpireTime, Nullable`1 absoluteExpireTime)
   at Abp.Runtime.Caching.CacheBase.GetAsync(String key, Func`2 factory)

解决方案:cmd模式命令进入任何一个节点 redis-cli.exe -c -p 6379

查看集群信息:cluster info

windows版本Redis集群搭建

 

 然后分别为每个端口执行以下命令(https://cloud.tencent.com/developer/section/1374001):

cluster meet 192.168.0.72 6380

 windows版本Redis集群搭建

 4.redis集群默认使用的单库,即只有一个db,如果需要做空间隔离,只能从代码层面解决

 3.代码测试

使用StackExchange.Redis包代码如下:

windows版本Redis集群搭建

 

windows版本Redis集群搭建

上一篇:微信jsApI及微信分享对应在手机浏览器的调用总结。


下一篇:WPF中DataGrid列标题和列内容居中