本次配置分为两种情况:
一是本机部署Redis集群,同时本机进行Java项目测试
二是Redis集群部署在其他服务器,Java项目客户端和Redis服务器各自为独立设备
1.Redis.conf几处修改点:
maxclients 10000
 masterauth 123456
 requirepass 123456 port 6379
 cluster-enabled yes
 cluster-config-file nodes-6379.conf
 cluster-node-timeout 15000
 appendonly yes
 protected-mode no(取消保护模式)#bind 0.0.0.0(取消本地绑定)
2.bat脚本启动文件
title redis-6379
 redis-server.exe redis.windows.conf(指定redis配置文件)3.集群模式CMD 问题类型
(1).Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(2).6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
(1).redis-cli -a 123456 --cluster create 127.0.0.1:6379 127.0.0.1:6378 127.0.0.1:6377 127.0.0.1:6376 127.0.0.1:6375 127.0.0.1:6374 --cluster-replicas 1 --no-auth-warning(忽略密码不安全问题)
(2).需要先关服务,随后删除appendonly.aof/dump.rdb/nodes-6379三文件
######12.19更新#####
问题报错信息(最开始以为是依赖问题,尝试各种Redis依赖,使用不同依赖,报错信息也不同)
Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource 
Could not retrieve cluster information. CLUSTER NODES returned with error.
Could not get a resource from the pool.
Unable to connect to [172.20.70.40:6378]: Connection refused: no further information: /172.20.70.40:6378
Redis connection failed; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to [RedisURI [host='172.20.70.40', port=6373], RedisURI [host='172.20.70.40', port=6378], RedisURI [host='172.20.70.40', port=6377]]
No more cluster attempts left.
解决方法:
如果Redis集群需部署在服务器上,创建集群时使用127.0.0.1的本机IP,会导致其他非本机客户端连接失败,即使关闭防火墙或者开放端口也会连接被拒,所以此情况在构建集群时,需要指定IP的真实地址,即
redis-cli -a 123456 --cluster create 172.20.70.40:6378 172.20.70.40:6377 172.20.70.40:6376 172.20.70.40:6375 172.20.70.40:6374 172.20.70.40:6373 --cluster-replicas 1 --no-auth-warning
如果只是本机部署本机项目测试无需考虑该点,127.0.0.1组建集群也可用
4.SpringBoot配置文件  问题类型(1).redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 6377 127.0.0.1:6379
(2).Could not get a resource from the pool
 
(1)
spring:
  redis:
    password: 123456
    cluster: #集群方式,注释host和port
      nodes: localhost:6378,localhost:6377,localhost:6376......(此处省略其他节点)
 
(2)
spring:
  redis: #springboot框架redis配置
    password: 123456
    cluster: #集群方式,注释host和port
      nodes: localhost:6378,localhost:6377,localhost:6376......(此处省略其他节点)
    lettuce:
      pool:
        maxActive: 50 #最大连接数,默认是8
        maxIdle: 50 #最大空闲数,默认是8
        maxWait: -1 #最大阻塞等待时间,默认-1没有限制
 
(3)
cache:
  provider:
    defaultDb: true #后台为true
  manager:
    useCache: true #是否使用缓存
    retryTime: 3 #访问缓存失败时,重试的次数
  local:
    name: local #本地缓存
    type: local
    defaultService: false #是否为默认缓存,local、mem、redis只能一个为true
    config:
      clusterSyncFlag: false
  mem:
    #name: mem #memcache缓存,取消注释则生效
    type: mem
    defaultService: false #是否为默认缓存,local、mem、redis只能一个为true
    config:
      servers: null #memcache服务器列表ip:port,多个服务之间用逗号分隔
  redis:
    #name: redis #redis缓存,取消注释则生效
    type: redis
    defaultService: true #是否为默认缓存,local、mem、redis只能一个为true
    clusterMode: true #redis是否为集群,集群情况下clusterNodes生效
    config:
      maxTotal: 100 #最大连接数
      maxIdle: 50 #最大空闲数
      minIdle: 10
      password: 123456
      clusterNodes: localhost:6378,localhost:6377,localhost:6376......(此处省略其他节点)
 
重点:
clusterMode: true 否则(MOVED 11750 127.0.0.1:6377)
cache模式local/mem/redis三选一
5.集群bat一键启动及停止脚本
(1)启动脚本
@echo off
REM 设置要处理的目录列表
 set "directories=redis6378 redis6377 redis6376 redis6375 redis6374 redis6373"REM 遍历目录列表
 for %%d in (%directories%) do (
     cd D:\BtSoft\RedisCLuster\%%d    REM 打开相应的.bat文件
     start %%d.bat
 )pause
 (2)终止脚本@echo off
REM 设置要处理的目录列表
 set "directories=redis6378 redis6377 redis6376 redis6375 redis6374 redis6373"REM 遍历目录列表
 for %%d in (%directories%) do (
     cd D:\BtSoft\RedisCLuster\%%d    REM 获取当前目录中的 redis-server 进程 ID
     for /f "tokens=2 delims= " %%p in ('tasklist /nh /fi "imagename eq redis-server.exe"') do (
         taskkill /F /PID %%p
         echo Redis服务器进程已关闭:PID=%%p
     )
 )pause