在window上搭建redis cluster并在.net core下使用csrediscore访问

x33g5p2x  于2021-11-21 转载在 .NET  
字(4.3k)|赞(0)|评价(0)|浏览(312)

在redis 5.0之前,redis cluster的搭建需要借助Ruby环境,使用redis-trib指令进行集群的管理,redis5.0以后,redis已经内置了集群管理,使用redis-cli客户端指令就可以进行集群的搭建和管理了。

因为是在window下使用,最高版本是3.2.1版本的,所以需要先安装Ruby。

 一、安装Ruby与Redis驱动

ruby下载地址:https://rubyinstaller.org/downloads/

下载后直接安装,然后下载redis驱动。驱动下载地址: https://rubygems.org/gems/redis/versions/3.3.2,选择对应的版本点击下载

将下载下来的gem文件复制到ruby的安装路径下,默认的安装路径在C:\Ruby30-x64,在安装路径下运行cmd安装驱动gem,如下所示为成功

C:\Users\Xu>cd c:\Ruby30-x64
c:\Ruby30-x64>gem install --local C:/Ruby30-x64/redis-4.4.0.gem
Successfully installed redis-4.4.0Parsing documentation for redis-4.4.0Installing ri documentation for redis-4.4.0Done installing documentation for redis after 5seconds
1 gem installed

 二、配置并安装节点服务启动集群

复制六个redis运行文件夹,分别修改其中的redis.windows.conf文件内容如下:

1、修改端口 port 6380,端口自定义

2、将appendonly设置为yes

3、开启集群模式cluster-enabled yes

4、保存节点配置cluster-config-file nodes-6380.conf,自动创建自动更新,加上对应端口

5、设置集群节点超时时间cluster-node-timeout 15000

之后构建集群脚本redis-trib.rb,点击此处下载。最后目录结构如下:

分别启动六个redis,大家可以创建成window服务,我这里直接cmd在redis文件夹下启动:

redis-server redis.windows.conf

之后运行cmd在redis-trib.rb所在目录运行如下命令启动集群:

redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6390 127.0.0.1:6391 127.0.0.1:6392

如下的信息表示运行成功,redis cluster 默认分配了 16384 个slot,并且将slot(槽)分配给了各个redis。分别是:

1、127.0.0.1:6380分配0-5460

2、127.0.0.1:6381分配5461-10922

3、127.0.0.1:6382分配10923-16383

>>>Creating cluster
Connecting to node 127.0.0.1:6380: OK
Connecting to node 127.0.0.1:6381: OK
Connecting to node 127.0.0.1:6382: OK
Connecting to node 127.0.0.1:6390: OK
Connecting to node 127.0.0.1:6391: OK
Connecting to node 127.0.0.1:6392: OK
>>> Performing hash slots allocation on 6nodes...
Using 3masters:
127.0.0.1:6380
127.0.0.1:6381
127.0.0.1:6382Adding replica 127.0.0.1:6390 to 127.0.0.1:6380Adding replica 127.0.0.1:6391 to 127.0.0.1:6381Adding replica 127.0.0.1:6392 to 127.0.0.1:6382M: a3a99540b358a6dbd1e421d1feb3c0d9c8de0207 127.0.0.1:6380slots:0-5460 (5461slots) master
M: 0c1dfe04b14143c7b790efa0f1b2bee72af8c7cc 127.0.0.1:6381slots:5461-10922 (5462slots) master
M: 4dc03ba5e34003fe648d213f5c0cc37802fc31d4 127.0.0.1:6382slots:10923-16383 (5461slots) master
S: 873ec67ae3dbc2ea8f67d8062bad46c76d6b4462 127.0.0.1:6390replicates a3a99540b358a6dbd1e421d1feb3c0d9c8de0207
S: 1182560131583d2ff590e99e5fe2afb8d2a3e134 127.0.0.1:6391replicates 0c1dfe04b14143c7b790efa0f1b2bee72af8c7cc
S: 0228dec64f82712f43d09458ffc7f69601d70e23 127.0.0.1:6392replicates 4dc03ba5e34003fe648d213f5c0cc37802fc31d4
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 forthe cluster to join.....
>>> Performing Cluster Check (using node 127.0.0.1:6380)
M: a3a99540b358a6dbd1e421d1feb3c0d9c8de0207 127.0.0.1:6380slots:0-5460 (5461slots) master
M: 0c1dfe04b14143c7b790efa0f1b2bee72af8c7cc 127.0.0.1:6381slots:5461-10922 (5462slots) master
M: 4dc03ba5e34003fe648d213f5c0cc37802fc31d4 127.0.0.1:6382slots:10923-16383 (5461slots) master
M: 873ec67ae3dbc2ea8f67d8062bad46c76d6b4462 127.0.0.1:6390slots: (0slots) master
   replicates a3a99540b358a6dbd1e421d1feb3c0d9c8de0207
M: 1182560131583d2ff590e99e5fe2afb8d2a3e134 127.0.0.1:6391slots: (0slots) master
   replicates 0c1dfe04b14143c7b790efa0f1b2bee72af8c7cc
M: 0228dec64f82712f43d09458ffc7f69601d70e23 127.0.0.1:6392slots: (0slots) master
   replicates 4dc03ba5e34003fe648d213f5c0cc37802fc31d4
[OK] All nodes agree about slots configuration.
>>> Check foropen slots...
>>>Check slots coverage...
[OK] All 16384 slots covered.

我们可以进入redis执行set和get看到如下情况

C:\Users\xu>redis-cli.exe  -c -p 6381
127.0.0.1:6381> setmyname xu
-> Redirected to slot [12807] located at 127.0.0.1:6382OK
127.0.0.1:6382> getmyname xu
(error) ERR wrong number of arguments for 'get'command
127.0.0.1:6382> getmyname
"xu"

可以看到集群会用CRC16算法来取模得到所属的slot,然后将这个key分到哈希槽区间的节点上CRC16(key) % 16384,通过slot来对需要操作的key进行定位。

所以,可以看到我们set的key计算之后被分配到了slot-12807 上, 而slot-12807处在节点6382上,因此redis自动redirect到了6382节点上。

在使用集群的时候有如下限制:

1、key批量操作支持有限。如mset、mget,目前只支持具有相同slot值的key执行批量操作。对弈映射为不同slot值的key由于执行mget。mset等操作可能存在多个节点上,因此不被支持。

2、key事务操作支持有限。同理只支持多key在统一节点上的事务操作,当多个key分布在不同的节点上时无法使用事务功能。

3、key作为数据分区的最小粒度读,因此不能将一个大的键值对象如hash、list等映射到不同的节点。

4、不支持多数据空间。单机下的Redis可以支持16个数据库,集群模式下只能使用一个数据库空间,即db0.

5、复制结构值支持一层,从节点只能复制主节点,不支持嵌套梳妆复制结构

三、.net core使用redis cluster

使用在nuget上安装CSRedisCore,之后写任意一个地址即可,其它节点在运行过程中自动增加,如有密码确保每个节点密码一致。

publicIActionResult Index()
{
    CSRedisClient client = new CSRedisClient("127.0.0.1:6380");
    return Content(client.Get("db_number"));
}

相关文章