記錄:442
場景:在Spring Boot微服務(wù)使用RedisTemplate操作Redis集群的緩存和隊列等數(shù)據(jù)類型。
版本:JDK 1.8,Spring?Boot 2.6.3,redis-6.2.5。
1.微服務(wù)中配置Redis信息
1.1在pom.xml添加依賴
pom.xml文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.3</version>
</dependency>
解析:spring-boot-starter-data-redis和spring-boot版本保持一致。
1.2在application.yml中配置Redis集群信息
(1)application.yml配置內(nèi)容
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)解析
配置內(nèi)容來源。
jar包:spring-boot-autoconfigure-2.6.3.jar。
類:org.springframework.boot.autoconfigure.data.redis.RedisProperties。
當引入spring-boot-starter時,此包已經(jīng)引入。
當需要配置集群其它信息時,在RedisProperties類中查找并配置到application.yml就能生效。
1.3加載簡要邏輯
Spring Boot微服務(wù)在啟動時,自動注解機制會讀取application.yml的配置信息注入到RedisProperties對象的對應(yīng)屬性。因此,在Spring環(huán)境中就能取到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<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
// 1.創(chuàng)建RedisTemplate對象
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
// 2.加載Redis配置
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
// 3.配置key序列化
RedisSerializer<?> stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// 4.配置Value序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
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環(huán)境中,可以使用@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設(shè)置超時(方式一)
redisTemplate.boundValueOps("D:2023060801:02").set("浙江-杭州");
redisTemplate.expire("D:2023060801:02", 5, TimeUnit.MINUTES);
// 5.2設(shè)置超時(方式二)
redisTemplate.boundValueOps("D:2023060801:03").set("浙江-寧波", 5, TimeUnit.MINUTES);
log.info("RedisTemplate操作Redis集群結(jié)束...");
return "執(zhí)行成功";
}
}
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設(shè)置超時(方式一)
template.boundValueOps("D:2023060801:05").set("浙江-杭州");
template.expire("D:2023060801:05", 5, TimeUnit.MINUTES);
// 5.2設(shè)置超時(方式二)
template.boundValueOps("D:2023060801:06").set("浙江-寧波", 10, TimeUnit.MINUTES);
log.info("RedisTemplate操作Redis集群結(jié)束...");
return "執(zhí)行成功";
}
}
4.3測試驗證
使用Postman測試。
請求RUL:http://127.0.0.1:18205/hub-205-redis/hub/example/operateCluster/f01_02
5.小結(jié)
使用RedisTemplate操作Redis集群和Redis單機版兩種場景。
差異:在application.yml配置中,使用集群時配置節(jié)點使用spring.redis.cluster.nodes配置IP和端口;單機版時,使用spring.redis.host配置IP和使用spring.redis.port配置端口。
共同:其它方面基本相同。
以上,感謝。文章來源:http://www.zghlxwxcb.cn/news/detail-525244.html
2023年6月8日文章來源地址http://www.zghlxwxcb.cn/news/detail-525244.html
到了這里,關(guān)于在Spring Boot微服務(wù)使用RedisTemplate操作Redis集群的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!