这里介绍SpringBoot下如何连接Redis Cluster集群进行访问操作
POM
这里Redis Cluster集群为3主3从的架构模式,其中Redis版本为7.0。SpringBoot及其相关依赖的版本如下所示。Spring Boot 2.X版本开始,其使用的Redis客户端由Jedis变为Lettuce。Lettuce提供了对Redis Cluster集群中MOVED、ASK重定向的支持
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.6</version> <relativePath/> </parent>
<properties> <java.version>8</java.version> </properties>
<dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
... </dependencies>
|
配置文件
SpringBoot下application.properties配置文件中关于Redis的配置如下所示。由于Lettuce客户端默认不开启集群拓扑刷新功能。故连接Redis集群时,我们需要开启Lettuce客户端对集群拓扑的刷新功能。以便在集群节点进行主从切换后,Lettuce客户端可以感知到集群节点的变化。具体地,Lettuce提供了三种刷新集群拓扑的方式:
- 通过主动调用 RedisClusterClient.reloadPartitions 方式进行手动刷新
- 后台定时刷新
- 后台自适应刷新(基于链接断开、MOVED/ASK重定向)
在SpringBoot2.3.x后,可直接通过spring.redis.lettuce.cluster.refresh.adaptive、spring.redis.lettuce.cluster.refresh.period配置项开启自适应刷新、定时刷新功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
spring.redis.database=0
spring.redis.password=52996
spring.redis.timeout=1000
spring.redis.cluster.nodes=120.120.120.11:6379,120.120.120.12:6379,120.120.120.13:6379,120.120.120.14:6379,120.120.120.15:6379,120.120.120.16:6379
spring.redis.cluster.max-redirects=3
spring.redis.lettuce.cluster.refresh.adaptive=true
spring.redis.lettuce.cluster.refresh.period=30000
spring.redis.lettuce.pool.max-active=-1
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
|
配置类
SpringBoot中Redis配置类如下所示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| @Slf4j @Configuration public class RedisConfig extends CachingConfigurerSupport{ @Bean(name = "redisTemplate") public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, String> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer( getKeySerializer() ); template.setValueSerializer( getValueSerializer() ); template.setHashKeySerializer( getKeySerializer() ); template.setHashValueSerializer( getValueSerializer() ); log.info("自定义RedisTemplate配置完成 ... "); return template; }
private RedisSerializer<String> getKeySerializer() { return new StringRedisSerializer(); }
private RedisSerializer<Object> getValueSerializer() { return new GenericJackson2JsonRedisSerializer(); } }
|
使用
这里我们直接通过Controller来验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @RestController @RequestMapping("test") public class TestController {
@Autowired private RedisTemplate redisTemplate;
@GetMapping("/setRedisData") public void setRedisData(@RequestParam String key, @RequestParam Object value) { redisTemplate.opsForValue().set(key, value); }
@GetMapping("/getRedisData") public String getRedisData(@RequestParam String key) { Object value = redisTemplate.opsForValue().get(key); return key + " --->>> " + value; } }
|
测试结果如下,符合预期