环境:基于第一节,克隆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]# 

     

 

如果构建集群的过程中出现了如下错误:
错误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"

 

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
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇