Springboot版本为2.7.6
redis pom.xml文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
properties配置
# Redis 服务器连接密码(默认为空,建议配上密码)
spring.redis.password=
#获取失败最大重定向次数
spring.redis.cluster.max-redirects=3
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.min-idle=0
spring.redis.cluster.nodes=公网ip:6379,公网ip:6380,公网ip:6381,公网ip:6382,公网ip:6383,公网ip:6384
集群配置文件
bind 0.0.0.0
daemonize yes
protected-mode no
port 6379
logfile "/myredis/cluster/cluster6379.log"
pidfile /myredis/cluster6379.pid
dir /myredis/cluster
dbfilename dumb6379.rdb
appendonly yes
appendfilename "appendonly6379.aof"
requirepass 自己指定密码
masterauth 自己指定密码
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
# 最主要是加上下面两条
# 云服务器上部署需指定公网ip
cluster-announce-ip 云服务器的ip
# Redis总线端口,用于与其它节点通信
cluster-announce-bus-port 16379
对于redis所需的端口都需要在云服务器上去开通端口(包括总线端口,即端口号加上10000)
启动各个redis服务器
redis-server /集群配置文件
创建集群模式
redis-cli -a 111 --cluster create --cluster-replicas 1 10.0.4.4:6379 10.0.4.4:6380 10.0.4.4:6381 10.0.4.4:6382 10.0.4.4:6383 10.0.4.4:6384
我使用的xshell连接云服务器,使用ifconfig得到的ip就是10.0.4.4
此时springboot连接redis集群就不会报超时问题了。