环境:基于第一节,克隆6台主机(干净的环境)
- 分别将6个redis的redis.conf就行配置:
#cluster-enabled yes cluster-enabled yes #开启集群 #cluster-config-file nodes-6379.conf cluster-config-file nodes-6379.conf ##开放注释,cluster自动管理节点 #cluster-node-timeout 5000 cluster-node-timeout 5000 #集群超时时间,如果超时, 则切换主备 appendonly yes #默认yes,如果是no请修改
- 分别启动6台主机的Redis服务
[root@redis1 ~]# /etc/init.d/redis_init_script start
- 构建集群
[root@redis3 redis]# redis-cli -a "123456" --cluster create 10.0.0.43:6379 10.0.0.44:6379 10.0.0.45:6379 10.0.0.46:6379 10.0.0.47:6379 10.0.0.48:6379 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> 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.0.0.47:6379 to 10.0.0.43:6379 Adding replica 10.0.0.48:6379 to 10.0.0.44:6379 Adding replica 10.0.0.46:6379 to 10.0.0.45:6379 ###cluster自动分配,三主三从 M: 5d012cc78f80276b23762db8336f7b14a9fecb86 10.0.0.43:6379 slots:[0-5460] (5461 slots) master M: 34cac491a62a12e7427e7d54184d54d4d3d61281 10.0.0.44:6379 slots:[5461-10922] (5462 slots) master M: b0f4dfc00f1a0a5d3e0c06b97737ca7d824da7c7 10.0.0.45:6379 slots:[10923-16383] (5461 slots) master S: f91096e9502b9dc0b688248eeb64f80963b110c7 10.0.0.46:6379 replicates b0f4dfc00f1a0a5d3e0c06b97737ca7d824da7c7 S: 32724100791e5444e0179136c2556c3d5792b936 10.0.0.47:6379 replicates 5d012cc78f80276b23762db8336f7b14a9fecb86 S: d1cfc38c26704e4ae4bad5473f360c18fbd98c6d 10.0.0.48:6379 replicates 34cac491a62a12e7427e7d54184d54d4d3d61281 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.0.0.43:6379) M: 5d012cc78f80276b23762db8336f7b14a9fecb86 10.0.0.43:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: b0f4dfc00f1a0a5d3e0c06b97737ca7d824da7c7 10.0.0.45:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: f91096e9502b9dc0b688248eeb64f80963b110c7 10.0.0.46:6379 slots: (0 slots) slave replicates b0f4dfc00f1a0a5d3e0c06b97737ca7d824da7c7 S: d1cfc38c26704e4ae4bad5473f360c18fbd98c6d 10.0.0.48:6379 slots: (0 slots) slave replicates 34cac491a62a12e7427e7d54184d54d4d3d61281 S: 32724100791e5444e0179136c2556c3d5792b936 10.0.0.47:6379 slots: (0 slots) slave replicates 5d012cc78f80276b23762db8336f7b14a9fecb86 M: 34cac491a62a12e7427e7d54184d54d4d3d61281 10.0.0.44:6379 slots:[5461-10922] (5462 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. [root@redis3 redis]#
-
检查集群信息:redis-cli -a 123456 --cluster check 10.0.0.43:6379
[root@redis3 redis]# redis-cli -a 123456 --cluster check 10.0.0.43:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.0.0.43:6379 (5d012cc7...) -> 0 keys | 5461 slots | 1 slaves. 10.0.0.45:6379 (b0f4dfc0...) -> 0 keys | 5461 slots | 1 slaves. 10.0.0.44:6379 (34cac491...) -> 0 keys | 5462 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 10.0.0.43:6379) M: 5d012cc78f80276b23762db8336f7b14a9fecb86 10.0.0.43:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: b0f4dfc00f1a0a5d3e0c06b97737ca7d824da7c7 10.0.0.45:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: f91096e9502b9dc0b688248eeb64f80963b110c7 10.0.0.46:6379 slots: (0 slots) slave replicates b0f4dfc00f1a0a5d3e0c06b97737ca7d824da7c7 S: d1cfc38c26704e4ae4bad5473f360c18fbd98c6d 10.0.0.48:6379 slots: (0 slots) slave replicates 34cac491a62a12e7427e7d54184d54d4d3d61281 S: 32724100791e5444e0179136c2556c3d5792b936 10.0.0.47:6379 slots: (0 slots) slave replicates 5d012cc78f80276b23762db8336f7b14a9fecb86 M: 34cac491a62a12e7427e7d54184d54d4d3d61281 10.0.0.44:6379 slots:[5461-10922] (5462 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. [root@redis3 redis]#
-
通过集群模式连接redis
[root@redis3 redis-cluster-slave]# bin/redis-cli -h 172.16.0.163 -p 6379 -a '123456' -c
如果构建集群的过程中出现了如下错误:
错误1:
[root@redis3 redis]# redis-cli -a "123456" --cluster create 10.0.0.43:6379 10.0.0.44:6379 10.0.0.45:6379 10.0.0.46:6379 10.0.0.47:6379 10.0.0.48:6379 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[ERR] Node 10.0.0.43:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
### 可能以前构建过,需要将redis中的 appendonly.aof 、 nodes-6379.conf 删除即可。
错误2:
[root@redis3 ~]# redis-cli --cluster create 10.0.0.43:6379 10.0.0.44:6379 10.0.0.45:6379 10.0.0.46:6379 10.0.0.47:6379 10.0.0.48:6379 --cluster-replicas 1
[ERR] Node 10.0.0.43:6379 NOAUTH Authentication required.
### 需要添加密码进行授权,-a "123456"
错误3:(大量日志占用磁盘的情况)
29132:S 19 May 2021 16:59:32.828 # MASTER aborted replication with an error: NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.828 * Reconnecting to MASTER 172.16.0.163:6379 after failure
29132:S 19 May 2021 16:59:32.829 * MASTER <-> REPLICA sync started
29132:S 19 May 2021 16:59:32.829 * Non blocking connect for SYNC fired the event.
29132:S 19 May 2021 16:59:32.829 * Master replied to PING, replication can continue...
29132:S 19 May 2021 16:59:32.830 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.830 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.830 * Partial resynchronization not possible (no cached master)
29132:S 19 May 2021 16:59:32.831 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.831 * Retrying with SYNC...
29132:S 19 May 2021 16:59:32.832 # MASTER aborted replication with an error: NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.832 * Reconnecting to MASTER 172.16.0.163:6379 after failure
29132:S 19 May 2021 16:59:32.832 * MASTER <-> REPLICA sync started
29132:S 19 May 2021 16:59:32.833 * Non blocking connect for SYNC fired the event.
29132:S 19 May 2021 16:59:32.833 * Master replied to PING, replication can continue...
29132:S 19 May 2021 16:59:32.834 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.834 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.834 * Partial resynchronization not possible (no cached master)
29132:S 19 May 2021 16:59:32.835 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.835 * Retrying with SYNC...
29132:S 19 May 2021 16:59:32.835 # MASTER aborted replication with an error: NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.835 * Reconnecting to MASTER 172.16.0.163:6379 after failure
29132:S 19 May 2021 16:59:32.836 * MASTER <-> REPLICA sync started
29132:S 19 May 2021 16:59:32.836 * Non blocking connect for SYNC fired the event.
29132:S 19 May 2021 16:59:32.837 * Master replied to PING, replication can continue...
29132:S 19 May 2021 16:59:32.838 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.838 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
29132:S 19 May 2021 16:59:32.838 * Partial resynchronization not possible (no cached master)
29132:S 19 May 2021 16:59:32.839 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
如果出现大量这种日志,以上这种情况,则是主从之间需要密码认证才能数据同步,在主从redi.conf中设置masgterauth 123456即可。
SpringBoot 集成cluster集群-配置
spring:
redis:
# database: 6 #不写默认为6
password: 123456
cluster:
nodes: 10.0.0.43:6379,10.0.0.44:6379,10.0.0.45:6379,10.0.0.46:6379,10.0.0.47:6379,10.0.0.48:6379
最后修改于 2020-01-10 16:10:54
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

