记录:442
场景:在Spring Boot微服务使用RedisTemplate操作Redis集群的缓存和队列等数据类型。
版本:JDK 1.8,Spring Boot 2.6.3,redis-6.2.5。
1.微服务中配置Redis信息
1.1在pom.xml添加依赖
pom.xml文件:
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- <version>2.6.3version>
- dependency>
解析:spring-boot-starter-data-redis和spring-boot版本保持一致。
1.2在application.yml中配置Redis集群信息
(1)application.yml配置内容
- spring:
- redis:
- cluster:
- nodes:
- - 192.168.19.161:27001
- - 192.168.19.161:27002
- - 192.168.19.162:27001
- - 192.168.19.162:27002
- - 192.168.19.163:27001
- - 192.168.19.163:27002
- password: demo12345678
- timeout: 60000
(2)解析
配置内容来源。
jar包:spring-boot-autoconfigure-2.6.3.jar。
类:org.springframework.boot.autoconfigure.data.redis.RedisProperties。
当引入spring-boot-starter时,此包已经引入。
当需要配置集群其它信息时,在RedisProperties类中查找并配置到application.yml就能生效。
1.3加载简要逻辑
Spring Boot微服务在启动时,自动注解机制会读取application.yml的配置信息注入到RedisProperties对象的对应属性。因此,在Spring环境中就能取到Redis集群的配置信息。
Spring从RedisProperties对象中取配置注入到RedisTemplate客户端中。因此,RedisTemplate客户端就能对Redis集群做增、删、改、查等操作。
2.配置RedisTemplate
RedisTemplate是springframework框架中封装的操作Redis的客户端。
类全称:org.springframework.data.redis.core.RedisTemplate
2.1配置RedisTemplate
- @Configuration
- public class RedisConfig {
- @Bean("redisTemplate")
- public RedisTemplate
redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { - // 1.创建RedisTemplate对象
- RedisTemplate
redisTemplate = new RedisTemplate(); - // 2.加载Redis配置
- redisTemplate.setConnectionFactory(lettuceConnectionFactory);
- // 3.配置key序列化
- RedisSerializer> stringRedisSerializer = new StringRedisSerializer();
- redisTemplate.setKeySerializer(stringRedisSerializer);
- redisTemplate.setHashKeySerializer(stringRedisSerializer);
- // 4.配置Value序列化
- Jackson2JsonRedisSerializer
- ObjectMapper objMapper = new ObjectMapper();
- objMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
- objMapper.activateDefaultTyping(objMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
- jackson2JsonRedisSerializer.setObjectMapper(objMapper);
- redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
- redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
- // 5.初始化RedisTemplate
- redisTemplate.afterPropertiesSet();
- return redisTemplate;
- }
- }
2.2解析
配置RedisTemplate后,在Spring环境中,可以使用@Autowired注解注入RedisTemplate实例操作Redis集群。也可以使用cn.hutool.extra.spring.SpringUtil工具等从Spring 中获取RedisTemplate实例操作Redis集群。
3.使用@Autowired注入RedisTemplate的方式操作Redis集群
3.1简要说明
使用@Autowired注入RedisTemplate,操作Redis集群的增、查、改、删等操作。
3.2操作示例
- @RestController
- @RequestMapping("/hub/example/operateCluster")
- @Slf4j
- public class OperateClusterController {
- @Autowired
- private RedisTemplate redisTemplate;
- /**
- * 注入方式使用RedisTemplate
- */
- @GetMapping("/f01_01")
- public Object f01_01() {
- log.info("RedisTemplate操作Redis集群开始...");
- // 1.增
- redisTemplate.boundValueOps("D:2023060801:01").set("浙江-杭州");
- // 2.查
- Object result01 = redisTemplate.boundValueOps("D:2023060801:01").get();
- log.info("查询 D:2023060801:01 = " + result01.toString());
- // 3.改
- redisTemplate.boundValueOps("D:2023060801:01").set("浙江-宁波");
- result01 = redisTemplate.boundValueOps("D:2023060801:01").get();
- log.info("修改后,查询 D:2023060801:01 = " + result01.toString());
- // 4.删
- long time = 5000;
- log.info("{}秒后,删除D:2023060801:01.",time/1000);
- ThreadUtil.sleep(time);
- redisTemplate.delete("D:2023060801:01");
- // 5.1设置超时(方式一)
- redisTemplate.boundValueOps("D:2023060801:02").set("浙江-杭州");
- redisTemplate.expire("D:2023060801:02", 5, TimeUnit.MINUTES);
- // 5.2设置超时(方式二)
- redisTemplate.boundValueOps("D:2023060801:03").set("浙江-宁波", 5, TimeUnit.MINUTES);
- log.info("RedisTemplate操作Redis集群结束...");
- return "执行成功";
- }
- }
3.3测试验证
使用Postman测试。
请求RUL:http://127.0.0.1:18205/hub-205-redis/hub/example/operateCluster/f01_01
4.使用SpringUtil从Spring中获取RedisTemplate的方式操作Redis集群
4.1简要说明
使用SpringUtil从Spring中获取RedisTemplate的方式,操作Redis集群的增、查、改、删等操作。
jar包:hutool-all-5.8.12.jar。
类:cn.hutool.extra.spring.SpringUtil
4.2操作示例
- @RestController
- @RequestMapping("/hub/example/operateCluster")
- @Slf4j
- public class OperateClusterController {
- @Autowired
- private RedisTemplate redisTemplate;
- /**
- * 使用工具获取RedisTemplate
- */
- @GetMapping("/f01_02")
- public Object f01_02() {
- log.info("RedisTemplate操作Redis集群开始...");
- RedisTemplate template = SpringUtil.getBean("redisTemplate");
- // 1.增
- template.boundValueOps("D:2023060801:04").set("浙江-杭州");
- // 2.查
- Object result01 = template.boundValueOps("D:2023060801:04").get();
- log.info("查询 D:2023060801:04 = " + result01.toString());
- // 3.改
- template.boundValueOps("D:2023060801:04").set("浙江-宁波");
- result01 = template.boundValueOps("D:2023060801:04").get();
- log.info("修改后,查询 D:2023060801:04 = " + result01.toString());
- // 4.删
- long time = 5000;
- log.info("{}秒后,删除D:2023060801:04.",time/1000);
- ThreadUtil.sleep(time);
- template.delete("D:2023060801:04");
- // 5.1设置超时(方式一)
- template.boundValueOps("D:2023060801:05").set("浙江-杭州");
- template.expire("D:2023060801:05", 5, TimeUnit.MINUTES);
- // 5.2设置超时(方式二)
- template.boundValueOps("D:2023060801:06").set("浙江-宁波", 10, TimeUnit.MINUTES);
- log.info("RedisTemplate操作Redis集群结束...");
- return "执行成功";
- }
- }
4.3测试验证
使用Postman测试。
请求RUL:http://127.0.0.1:18205/hub-205-redis/hub/example/operateCluster/f01_02
5.小结
使用RedisTemplate操作Redis集群和Redis单机版两种场景。
差异:在application.yml配置中,使用集群时配置节点使用spring.redis.cluster.nodes配置IP和端口;单机版时,使用spring.redis.host配置IP和使用spring.redis.port配置端口。
共同:其它方面基本相同。
以上,感谢。
2023年6月8日