如何在Spring?Boot微服務(wù)使用ValueOperations操作Redis集群String字符串
記錄:443
場(chǎng)景:在Spring Boot微服務(wù)使用RedisTemplate的ValueOperations操作Redis集群String字符串類型數(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。
當(dāng)引入spring-boot-starter時(shí),此包已經(jīng)引入。
當(dāng)需要配置集群其它信息時(shí),在RedisProperties類中查找并配置到application.yml就能生效。
1.3加載簡(jiǎn)要邏輯
Spring Boot微服務(wù)在啟動(dòng)時(shí),自動(dòng)注解機(jī)制會(huì)讀取application.yml的配置信息注入到RedisProperties對(duì)象的對(duì)應(yīng)屬性。因此,在Spring環(huán)境中就能取到Redis集群的配置信息。
Spring從RedisProperties對(duì)象中取配置注入到RedisTemplate客戶端中。因此,RedisTemplate客戶端就能對(duì)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對(duì)象
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;
}
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
}2.2解析
使用@Configuration和@Bean配置RedisTemplate后,使用@Autowired注解注入RedisTemplate和ValueOperations實(shí)例操作Redis集群。
3.使用ValueOperations操作Redis String字符串
3.1簡(jiǎn)要說明
使用ValueOperations操作字符串,常用操作:增、查、改、刪、設(shè)置超時(shí)等。
3.2操作示例
@RestController
@RequestMapping("/hub/example/operateCluster")
@Slf4j
public class OperateClusterController {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ValueOperations valueOperations;
/**
* 使用ValueOperations,操作String類型數(shù)據(jù)
*/
@GetMapping("/f02")
public Object f02() {
log.info("ValueOperations操作Redis集群開始...");
// 1.增
valueOperations.set("D:2023060802:01", "浙江-杭州");
valueOperations.set("D:2023060802:02", "浙江-蕭山");
// 2.查
Object result01 = valueOperations.get("D:2023060802:01");
log.info("查詢 D:2023060802:01 = " + result01.toString());
// 3.改
valueOperations.set("D:2023060802:01", "浙江-寧波");
result01 = valueOperations.get("D:2023060802:01");
log.info("修改后,查詢 D:2023060802:01 = " + result01.toString());
// 4.刪
long time = 5000;
log.info("{}秒后,刪除D:2023060802:01.", time / 1000);
ThreadUtil.sleep(time);
String result02 = (String) valueOperations.getAndDelete("D:2023060802:01");
redisTemplate.delete("D:2023060802:02");
// 5.1設(shè)置超時(shí)(方式一)
valueOperations.set("D:2023060802:03", "浙江-杭州");
valueOperations.getAndExpire("D:2023060802:03", 5, TimeUnit.MINUTES);
// 5.2設(shè)置超時(shí)(方式二)
valueOperations.set("D:2023060802:04", "浙江-杭州-西湖");
redisTemplate.expire("D:2023060802:04", 10, TimeUnit.MINUTES);
// 6.查詢Value的字節(jié)大小
Long size = valueOperations.size("D:2023060802:04");
log.info("緩存字節(jié)大小,size=" + size + " bytes");
log.info("ValueOperations操作Redis集群結(jié)束...");
return "執(zhí)行成功";
}
}3.3測(cè)試驗(yàn)證
使用Postman測(cè)試。
請(qǐng)求RUL:http://127.0.0.1:18205/hub-205-redis/hub/example/operateCluster/f02
以上,感謝。
2023年6月8日
到此這篇關(guān)于在Spring Boot微服務(wù)使用ValueOperations操作Redis集群String字符串的文章就介紹到這了,更多相關(guān)Spring Boot微服務(wù)操作Redis集群String字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 如何在Spring?Boot微服務(wù)使用ValueOperations操作Redis集群String字符串
- 微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn)UV 數(shù)據(jù)統(tǒng)計(jì)的詳細(xì)過程
- 微服務(wù)?Spring?Boot?整合?Redis?BitMap?實(shí)現(xiàn)?簽到與統(tǒng)計(jì)功能
- 微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn)好友關(guān)注功能
- 微服務(wù)Spring?Boot?整合Redis?阻塞隊(duì)列實(shí)現(xiàn)異步秒殺下單思路詳解
相關(guān)文章
SpringBoot整合Redis將對(duì)象寫入redis的實(shí)現(xiàn)
本文主要介紹了SpringBoot整合Redis將對(duì)象寫入redis的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
spring cloud實(shí)現(xiàn)前端跨域問題的解決方案
這篇文章主要介紹了 spring cloud實(shí)現(xiàn)前端跨域問題的解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
SpringBoot集成企業(yè)微信開發(fā)的實(shí)現(xiàn)
本文將詳細(xì)介紹如何使用?Spring?Boot?集成企業(yè)微信開發(fā),通過企業(yè)微信?API?可以實(shí)現(xiàn)企業(yè)內(nèi)部的一些自動(dòng)化業(yè)務(wù)流程,提高工作效率,感興趣的可以了解一下2023-07-07
Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
idea創(chuàng)建SpringBoot項(xiàng)目時(shí)Type選maven?project和maven?pom有何區(qū)別
Maven是一個(gè)Java工程的管理工具,跟其相同功能的工具如Gradle,下面這篇文章主要給大家介紹了關(guān)于idea創(chuàng)建SpringBoot項(xiàng)目時(shí)Type選maven?project和maven?pom有何區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-02-02
SpringBoot定制化Starter實(shí)現(xiàn)方法
小伙伴們?cè)?jīng)可能都經(jīng)歷過整天寫著CURD的業(yè)務(wù),都沒寫過一些組件相關(guān)的東西,這篇文章記錄一下SpringBoot如何自定義一個(gè)Starter。原理和理論就不用多說了,可以在網(wǎng)上找到很多關(guān)于該方面的資料,這里主要分享如何自定義2023-01-01
Servlet3.0學(xué)習(xí)總結(jié)之基于Servlet3.0的文件上傳實(shí)例
本篇文章主要介紹了Servlet3.0學(xué)習(xí)總結(jié)之基于Servlet3.0的文件上傳實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Java并發(fā)編程之阻塞隊(duì)列(BlockingQueue)詳解
這篇文章主要介紹了詳解Java阻塞隊(duì)列(BlockingQueue)的實(shí)現(xiàn)原理,阻塞隊(duì)列是Java util.concurrent包下重要的數(shù)據(jù)結(jié)構(gòu),有興趣的可以了解一下2021-09-09

