Redis集群配置
今天为大家介绍Redis集群的配置教程,比较基础,很容易懂的
1、准备三台电脑,我这里使用的三台ubuntu主机,redis集群至少需要三个主节点才能配置的(我下面配置集群的时候用的是两台电脑,创建6个节点 )。
# 三台主机,IP地址分别为
192.168.60.57
192.168.60.91
192.168.60.94
# 很明显,三个主机是在同一个局域网络里面,当然你可以设置在不同的网络里面,只要能互相访问就行了
2、安装好redis
sudo apt-get install redis-server
hb2@ubuntu:~$ ps -aux|grep redis
redis 5960 0.0 0.4 44044 4500 ? Ssl 18:18 0:00 /usr/bin/redis-server 127.0.0.1:6379
hb2 6247 0.0 0.0 15940 924 pts/0 S+ 18:25 0:00 grep --color=auto redis
# 三台设备都要安装好
3、在 192.168.60.57 主机上 创建conf文件夹,创建7000.conf、7001.conf、702.conf
4、了解下从属服务(先了解下,等下我们接着上面继续)
- 从服务是对主服务的备份
- 从服务只有读的权限,主服务有读写的权限
- 一个从服务只能有一个主服务
- 一个主服务可以有多个从服务
5、搭建从属服务器
配置 192.168.60.91
hb2@ubuntu:~$ sudo vim /etc/redis/redis.conf
这里也就是将bind 绑定为自己本机的IP地址(如果是同一台电脑上要修改端口)
,设置好主服务的IP地址
6、查看从属服务的信息
- role 表示角色 master 主服务 slave 从服务
- master-host 主服务的IP地址给
- master-port 主服务的端口
- connected-slaves 从服务连接数
- master_link_statue 主服务的连接状态
- …读者自己去查吧
7、项目中redis主从服务
8、编写conf文件(这里是接着上面的文章继续的)
在192.168.60.91这台电脑上创建三个文件 7001.conf 7002.conf 7003.conf这个名字无所谓,随便写
port 7001
bind 192.168.60.91
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
- bind绑定的是本机地址,注意每个文件对应的格式一样 端口和pidfile,cluster-config-file区分开
启动三个服务
sudo redis-server ./7001.conf
sudo redis-server ./7002.conf
sudo redis-server ./7003.conf
hb2@ubuntu:~/Desktop/conf$ ps -aux|grep redis
hb2 33057 0.0 0.7 57364 7712 ? Ssl 00:36 0:01 redis-server 192.168.60.91:7001 [cluster]
hb2 33069 0.0 0.7 57364 7716 ? Ssl 00:37 0:01 redis-server 192.168.60.91:7002 [cluster]
hb2 33074 0.0 0.4 57364 4620 ? Ssl 00:37 0:01 redis-server 192.168.60.91:7003 [cluster]
hb2 33139 0.0 0.0 15940 920 pts/23 S+ 01:06 0:00 grep --color=auto redis
同样,另外一台电脑也这样启动,使用ps -aux|grep redis看到服务已经启动就可以了
我们可以看到后面的大括号里面显示集群,同样另外一台电脑也要相同的配置好并运行起来
8 创建集群
介绍
下面的命令就是分别将两台电脑上的cluster联系起来,形成一个整体的集群系统
redis-cli --cluster create 192.168.60.91:7002 192.168.60.91:7003 192.168.60.91:7001 192.168.60.94:7000 192.168.60.94:7001 192.168.60.94:7002
这个命令是最新,很久之前是用ruby,现在可以不用他了
>>> Performing hash slots allocation on 6 nodes...
#这个是每个节点槽节点的范围,以后数据会根据一定条件存储在下面这些槽节点中
Master[0] -> Slots 0 - 2730
Master[1] -> Slots 2731 - 5460
Master[2] -> Slots 5461 - 8191
Master[3] -> Slots 8192 - 10922
Master[4] -> Slots 10923 - 13652
Master[5] -> Slots 13653 - 16383
M: 3ac33dd385d9c3293e298afd85813e649d6b4aa7 192.168.60.91:7002
slots:[0-2730] (2731 slots) master
M: 360bc13a876ffee58e673f7a119135e3977511e8 192.168.60.91:7003
slots:[5461-8191] (2731 slots) master
M: 2715502ab3b3b111f9538af8e856a03ceae32952 192.168.60.91:7001
slots:[10923-13652] (2730 slots) master
M: 8cb62ce72e09dd28cc0027a788a23cd12375d6f3 192.168.60.94:7000
slots:[2731-5460] (2730 slots) master
M: 3e94ccbed1a251059f328026300c79384fc940ab 192.168.60.94:7001
slots:[8192-10922] (2731 slots) master
M: 8c953a9cb0ada80e24190ccd56a6477bf2b8cc50 192.168.60.94:7002
slots:[13653-16383] (2731 slots) master
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 192.168.60.91:7002)
M: 3ac33dd385d9c3293e298afd85813e649d6b4aa7 192.168.60.91:7002
slots:[0-2730] (2731 slots) master
M: 2715502ab3b3b111f9538af8e856a03ceae32952 192.168.60.91:7001
slots:[10923-13652] (2730 slots) master
M: 3e94ccbed1a251059f328026300c79384fc940ab 192.168.60.94:7001
slots:[8192-10922] (2731 slots) master
M: 8c953a9cb0ada80e24190ccd56a6477bf2b8cc50 192.168.60.94:7002
slots:[13653-16383] (2731 slots) master
M: 8cb62ce72e09dd28cc0027a788a23cd12375d6f3 192.168.60.94:7000
slots:[2731-5460] (2730 slots) master
M: 360bc13a876ffee58e673f7a119135e3977511e8 192.168.60.91:7003
slots:[5461-8191] (2731 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
hb2@ubuntu:~/redis-5.0.3/src$ redis-cli -h 192.168.60.91 -c -p 7002
192.168.60.91:7002> set name "zhangsan"
-> Redirected to slot [5798] located at 192.168.60.91:7003
OK
192.168.60.91:7003> set age 500
-> Redirected to slot [741] located at 192.168.60.91:7002
OK
192.168.60.91:7002> set ag 90000
# 如果经过计算不是这个槽结点,会跳转到另外一个节点当中
-> Redirected to slot [9306] located at 192.168.60.94:7001
OK
192.168.60.94:7001>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)