Redis Cluster搭建使用

要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下(为了简单演示都在两台机器上面进行搭建)

10.38.30.136:7000
10.38.30.136:7001
10.38.30.136:7002

10.38.30.137:7000
10.38.30.137:7001
10.38.30.137:7002

注意: 即使是在同一台机器上搭建,ip也不要写成127.0.0.1,写成本地实际ip地址

1.下载redis。

# 最新版本下载地址: https://download.redis.io/releases
wget http://download.redis.io/releases/redis-7.4.5.tar.gz

2. 解压,安装

tar zxvf redis-7.4.5.tar.gz                   
cd redis-7.4.5
make && make PREFIX=/home/redis/redis install

# 配置环境变量
# 如果使用 root 用户则可以将环境变量配置到全局配置文件中
# 添加到 /etc/profile
echo 'export PATH=/home/redis/redis/bin:$PATH' >> /etc/profile
source /etc/profile
# 如果使用 普通用户时,则配置到家目录的配置文件中
# echo 'export PATH=/home/redis/redis/bin:$PATH' >> ~/.bashrc
# source ~/.bashrc

3.创建存放多个实例的目录

mkdir /home/redis/redis/data/cluster/700{0,1,2} -p

4.修改配置文件

修改redis.conf配置文件中下面选项(为保险起见,建议找到每个配置项后,进行修改)

bind 0.0.0.0
port 7000
protected-mode no
daemonize yes
##开实例的集群模式
cluster-enabled yes
##设定了保存节点配置文件的路径, 默认值为nodes.conf
cluster-config-file nodes.conf

cluster-node-timeout 15000
appendonly yes

pidfile redis.pid
logfile redis.log
dbfilename dump.rdb
# redis密码,根据实际密码进行调整
requirepass Password@2025
masterauth PasswordP@2025

dir ./

修改完成后,把修改完成的redis.conf复制到7000/7001/7002目录下,并且端口修改成和文件夹对应。

echo /home/redis/redis/data/cluster/{7000,7001,7002}/ | xargs -n 1 cp redis-7.4.5/redis.conf 

5.分别启动6个redis实例。

创建 start.sh 启动脚本,确保启动脚本放到和redis.conf 同一目录

cd $(dirname $0)
pwd
redis-server redis.conf

启动两台服务上的redis服务

sh /home/redis/redis/data/cluster/7000/start.sh
sh /home/redis/redis/data/cluster/7001/start.sh
sh /home/redis/redis/data/cluster/7002/start.sh

查看进程否存在。

[root@redis-server 7005]# ps -ef | grep redis
root      4168     1  0 11:49 ?        00:00:00 redis-server *:7000 [cluster]
root      4176     1  0 11:49 ?        00:00:00 redis-server *:7001 [cluster]
root      4189     1  0 11:49 ?        00:00:00 redis-server *:7002 [cluster]
root      4219  4075  0 11:50 pts/2    00:00:00 grep redis

6.执行命令创建集群

利用redis-cli客户端创建集群配置文件

创建三主三从的集群

redis-cli --cluster create 10.38.30.136:7000 10.38.30.136:7001 10.38.30.136:7002 10.38.30.137:7000 10.38.30.137:7001 10.38.30.137:7002 -a Password@2025 --cluster-replicas 1

输入 yes 并按下回车确认之后, 集群就会将配置应用到各个节点, 并连接起(join)各个节点 —— 也即是, 让各个节点开始互相通讯
有类似如下输出,则说明创建集群成功:

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.38.30.136:7002 to 10.38.30.137:7000
Adding replica 10.38.30.137:7002 to 10.38.30.136:7000
Adding replica 10.38.30.136:7001 to 10.38.30.137:7001
M: 2b95d5e70b6d8dd6e105259f0ce5527e7ab1546e 10.38.30.136:7000
   slots:[0-5460] (5461 slots) master
M: 94e645d032bd06df182e3eb5b7652bba16cf93d0 10.38.30.136:7001
   slots:[10923-16383] (5461 slots) master
S: 32dd2d7f83ca5c8bddf60d3ea8f03b92b261def9 10.38.30.136:7002
   replicates 7acc0ecc82784caabb449728a3ff30101cdaf412
M: 7acc0ecc82784caabb449728a3ff30101cdaf412 10.38.30.137:7000
   slots:[5461-10922] (5462 slots) master
S: 611b6f2799a4f7d0522500bcf434f8cdf5685747 10.38.30.137:7001
   replicates 94e645d032bd06df182e3eb5b7652bba16cf93d0
S: 777ad910d98bfbc94f741153020165584410d3bd 10.38.30.137:7002
   replicates 2b95d5e70b6d8dd6e105259f0ce5527e7ab1546e
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 10.38.30.137:7000)
M: 2b95d5e70b6d8dd6e105259f0ce5527e7ab1546e 10.38.30.137:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 7acc0ecc82784caabb449728a3ff30101cdaf412 10.38.30.136:7000
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 777ad910d98bfbc94f741153020165584410d3bd 10.38.30.136:7002
   slots: (0 slots) slave
   replicates 2b95d5e70b6d8dd6e105259f0ce5527e7ab1546e
S: 611b6f2799a4f7d0522500bcf434f8cdf5685747 10.38.30.136:7001
   slots: (0 slots) slave
   replicates 94e645d032bd06df182e3eb5b7652bba16cf93d0
S: 32dd2d7f83ca5c8bddf60d3ea8f03b92b261def9 10.38.30.137:7002
   slots: (0 slots) slave
   replicates 7acc0ecc82784caabb449728a3ff30101cdaf412
M: 94e645d032bd06df182e3eb5b7652bba16cf93d0 10.38.30.137:7001
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

7.查看节点信息

redis-cli -c -h 10.38.30.136 -p 7000 -a Password@2025  cluster nodes

8.检查集群状态

redis-cli --cluster check 10.38.30.136:7000  -a Password@2025

如果一切正常,您应该会看到类似于以下内容的输出:

[OK] All nodes agree about slots configuration.
[OK] All 6 nodes are online.

9. springboot 配置redis-cluster配置

修改 workflowsso 目录下的 application-dev.yml 中redis 的配置信息

yaml配置

spring:
  redis:
    database: 0
    password: Passwordt@2025
    cluster:
	  # 把节点和ip替换为实际的
      nodes: <node1>:<port1>,<node2>:<port2>,<node3>:<port3>,<node4>:<port4>,<node5>:<port5>,<node6>:<port6>
      max-redirects: 3

Q.E.D.


寻门而入,破门而出