配置
集群模式配置
# Redis Cluster集群节点配置spring.redis.cluster.nodes[0]=ip:端口spring.redis.cluster.nodes[1]=ip:端口spring.redis.cluster.nodes[2]=ip:端口
# 获取失败最大重定向次数spring.redis.cluster.max-redirects=2
哨兵模式配置
# Redis 哨兵节点配置spring.redis.sentinel.nodes[0]=ip:端口spring.redis.sentinel.nodes[1]=ip:端口spring.redis.sentinel.nodes[2]=ip:端口# 密码
spring.redis.sentinel.password=
# 主节点名称
spring.redis.sentinel.master=
公共配置
# 连接池最大连接数默认值为8
spring.redis.lettuce.pool.max-active=20
# 连接池最大阻塞时间(使用负值表示没有限制)默认值为-1
spring.redis.lettuce.pool.max-wait=15s
# 连接池中最大空闲连接数默认值为8
spring.redis.lettuce.pool.max-idle=20
# 连接池中的最小空闲连接数,默认值为0
spring.redis.lettuce.pool.min-idle=1
# 空闲连接回收间隔时间
spring.redis.lettuce.pool.time-between-eviction-runs=10s
# 开启cluster自适应刷新周期60秒
spring.redis.lettuce.cluster.refresh.adaptive=true
spring.redis.lettuce.cluster.refresh.period=60000
# 连接超时时间(毫秒)
spring.redis.timeout=5000
#redis配置结束
spring.redis.block-when-exhausted=true#redis key添加前缀,以满足不同应用使用同一套redis集群的需求
spring.redis.key.prefix=
spring.redis.block-when-exhausted
spring.redis.block-when-exhausted 是SpringFramework中用于配置Redis连接池行为的属性之一。当Redis连接池中的连接数达到上限并且没有可用连接时,这个属性决定了获取连接的行为。如果设置为true,则会阻塞并等待,直到有连接空闲为止。这个模式被称为阻塞模式。如果设置为false,则会立即返回一个错误,表示无法获取到连接。这个模式被称为非阻塞模式。如果不配置该属性,默认情况下它是true,即阻塞模式。在高并发的情况下,如果设置为阻塞模式,会导致线程阻塞和等待,降低系统的吞吐量。但是,如果设置为非阻塞模式,可能会因为无法获取连接而导致请求失败。因此,需要根据具体的业务场景来选择合适的模式。如果在短时间内需要处理大量的并发请求,可以选择阻塞模式,以避免请求失败。如果需要高吞吐量并且可以接受请求失败的情况,则可以选择非阻塞模式。
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.0</version>
</dependency>
使用样例
https://github.com/fudai/spring-redis