Spring?Boot2?整合連接?Redis的操作方法
1.在 springboot 中 , 整合 redis
可以通過(guò) RedisTemplate 完成對(duì) redis 的操作, 包括設(shè)置數(shù)據(jù)/獲取數(shù)據(jù)
比如添加和讀取數(shù)據(jù)
具體整合實(shí)現(xiàn):
創(chuàng)建 Maven 項(xiàng)目:
在 pom.xml 文件當(dāng)中導(dǎo)入相關(guān)的 jar 依賴。如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.rainbowsea</groupId> <artifactId>redis_springboot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <!-- 說(shuō)明: 如果這里是 spring-boot-start 就改成如下 spring-boot-start-web--> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring2.X 集成 redis 所需 common-pool--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <!--不要帶版本號(hào),防止沖突--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- json 轉(zhuǎn)換的 jar 包依賴--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.2.2</version> </dependency> </dependencies> <!-- 插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在 resources 目錄下創(chuàng)建 application.properties,完成 redis 的基本配置,如下所示:
#Redis 服務(wù)器地址 spring.redis.host=192.168.76.145 #Redis 服務(wù)器連接端口 spring.redis.port=6379 #Redis 如果有密碼,需要配置, 沒(méi)有密碼就不要寫(xiě) spring.redis.password=rainbowsea #Redis 數(shù)據(jù)庫(kù)索引(默認(rèn)為 0) spring.redis.database=0 #連接超時(shí)時(shí)間(毫秒) spring.redis.timeout=1800000 #連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制) spring.redis.lettuce.pool.max-active=20 #最大阻塞等待時(shí)間(負(fù)數(shù)表示沒(méi)限制) spring.redis.lettuce.pool.max-wait=-1 #連接池中的最大空閑連接 spring.redis.lettuce.pool.max-idle=5 #連接池中的最小空閑連接 spring.redis.lettuce.pool.min-idle=0
創(chuàng)建/定義一個(gè) Redis 配置類。
- 這個(gè) Redis 配置類是對(duì)要使用 RedisTemplate bean 對(duì)象的配置,可以理解成是一個(gè)常規(guī)配置。
- 和我們以前學(xué)過(guò)的一個(gè) JdbcTemplate 的設(shè)計(jì)理念類似。
- 如果不是配置,那么 Spring boot 會(huì)使用默認(rèn)配置,這個(gè)默認(rèn)配置,會(huì)出現(xiàn)一些問(wèn)題,比如:redisTemplate 的 key 序列化等問(wèn)題,所以通常我們需要配置這個(gè)。Redis 配置類.
創(chuàng)建:\redis_springboot\src\main\java\com\rainbowsea\redis\config\ RedisConfig.java 配置類
package com.rainbowsea.redis.config; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Configuration; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; @EnableCaching // 配置開(kāi)啟緩存 @Configuration // 定義配置類 public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); System.out.println("template=>" + template); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.activateDefaultTyping( LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY); jackson2JsonRedisSerializer.setObjectMapper(om); template.setConnectionFactory(factory); // key 序列化方式 template.setKeySerializer(redisSerializer); // value 序列化 template.setValueSerializer(jackson2JsonRedisSerializer); // value hashmap 序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解決查詢緩存轉(zhuǎn)換異常的問(wèn)題 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.activateDefaultTyping( LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解決亂碼的問(wèn)題),過(guò)期時(shí)間 600 秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(600)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } }
創(chuàng)建 controller 訪問(wèn)設(shè)置/獲取到 Redis 數(shù)據(jù)庫(kù)當(dāng)中的數(shù)據(jù)。
重點(diǎn): 我們這里的
RedisTemplate
模板對(duì)象,就是已經(jīng)配置好了 Jedis 的連接上 Redis的一個(gè)模板,該模板提供了很多,我們操作 Redis 數(shù)據(jù)庫(kù)的方法。就和我們前面學(xué)習(xí) MySQL ,操作連接 MySQL 當(dāng)中的 JdbcTemplate 模板是類似的。
如下所示:
package com.rainbowsea.redis.controller; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/redisTest") public class RedisTestController { // 裝配 RedisTemplate @Resource private RedisTemplate redisTemplate; // 編寫(xiě)一個(gè)測(cè)試方法 // 演示設(shè)置數(shù)據(jù)和獲取數(shù)據(jù) @GetMapping("/t1") public String t1() { // 設(shè)置值到 redis 當(dāng)中,opsForValue 是操作 string 字符串的 redisTemplate.opsForValue().set("book", "天龍八部"); // 從 redis 當(dāng)中獲取值 String book = (String) redisTemplate.opsForValue().get("book"); return book; } }
創(chuàng)建場(chǎng)景啟動(dòng)器。
package com.rainbowsea.redis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RedisSpringBootApplication { public static void main(String[] args) { SpringApplication.run(RedisSpringBootApplication.class, args); } }
啟動(dòng)程序run, 打開(kāi)瀏覽器地址欄上輸入:http://localhost:9090/redisTest/t1 。
**演示:如何操作 List **
package com.rainbowsea.redis.controller; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping("/redisTest") public class RedisTestController { // 裝配 RedisTemplate @Resource private RedisTemplate redisTemplate; // 演示如何操作 list 列表 @GetMapping("/t2") public String t2() { // list-存 redisTemplate.opsForList().leftPush("books", "笑傲江湖"); redisTemplate.opsForList().leftPush("books", "hello world"); // list - 取數(shù)據(jù) List books = redisTemplate.opsForList().range("books", 0, -1); String booksList = ""; for (Object book : books) { System.out.println("book->" + book.toString()); booksList += book.toString(); } return booksList; } // 編寫(xiě)一個(gè)測(cè)試方法 // 演示設(shè)置數(shù)據(jù)和獲取數(shù)據(jù) @GetMapping("/t1") public String t1() { // 設(shè)置值到 redis 當(dāng)中,opsForValue 是操作 string 字符串的 redisTemplate.opsForValue().set("book", "天龍八部"); // 從 redis 當(dāng)中獲取值 String book = (String) redisTemplate.opsForValue().get("book"); return book; } }
演示:如何操作 hash
@RestController @RequestMapping("/redisTest") public class RedisTestController { // 裝配 RedisTemplate @Resource private RedisTemplate redisTemplate; @GetMapping("/t3") public String t3() { // hash - 存數(shù)據(jù) redisTemplate.opsForHash(); // 操作 Zset 有序集合 redisTemplate.opsForZSet(); // 操作 set 集合 redisTemplate.opsForSet(); return null; }
2. 注意事項(xiàng)和細(xì)節(jié)
如果沒(méi)有提供 RedisConfig 配置類 , springboot 會(huì)使用默認(rèn)配置 也可以使用。但是會(huì)存在問(wèn)題。比如 redisTemplate 模糊查找 key 數(shù)據(jù)為空。
測(cè)試:
這里我們先將 我們配置的 RedisConfig 配置類注釋掉。
編寫(xiě)一個(gè)方法,獲取所有的 key:
*
,表示獲取所有的 key
@RestController @RequestMapping("/redisTest") public class RedisTestController { // 裝配 RedisTemplate @Resource private RedisTemplate redisTemplate; // 編寫(xiě)一個(gè)方法,獲取所有的 key @GetMapping("/t3") public String t3() { Set keys = redisTemplate.keys("*"); for (Object key : keys) { System.out.println("key -->" + key.toString()); } return "OK"; } }
**當(dāng)我們將我們的配置的 RedisConfig 類打開(kāi),不使用 Spring Boot 默認(rèn)的配置。則不會(huì)出現(xiàn)該空的情況。 **
Unrecognized token 'beijing': was expecting ('true', 'false' or 'null')
看報(bào)錯(cuò),是 jason 轉(zhuǎn)換異常,實(shí)際上是因?yàn)?redisTemplate 在做數(shù)據(jù)存儲(chǔ)的時(shí)候會(huì)把存儲(chǔ)的內(nèi)容序列化,所以,redisTemplate 讀取的時(shí)候也會(huì)反序列化,而在 redis 客戶端 set 的時(shí)候并不會(huì)做序列化,因此 set 的進(jìn)去的值在用 redisTemplate 讀的時(shí)候就會(huì)報(bào)類 型轉(zhuǎn)換異常了。
演示:
我們?cè)?Redis 命令行客戶端(不通過(guò)Java程序的方式),創(chuàng)建 一個(gè) k100的字符串。
然后,我們?cè)偻ㄟ^(guò)Java程序獲取到該(Redis命令行客戶端)所創(chuàng)建的 k100 字符串的數(shù)據(jù)。
解決這個(gè)
com.fasterxml.jackson.core.JsonParseException
也簡(jiǎn)單,既然我們 Resi 命令行客戶端(創(chuàng)建的 對(duì)象信息/值)不會(huì)被序列化,那我們就不用 Redis 命令行客戶端創(chuàng)建對(duì)象了,直接就是。我們想用Java程序當(dāng)中反序化的功能:我們就用 Java程序創(chuàng)建對(duì)象/值,同時(shí)也用Java程序獲取對(duì)象的數(shù)據(jù)。存和取都保持一致,都是在Java程序當(dāng)中即可(因?yàn)镴ava程序創(chuàng)建的對(duì)象會(huì)自行序列化的)。這里就不演示了,因?yàn)槲覀兩鲜龅乃胁僮鞫际牵篔ava程序連接 Redis 創(chuàng)建對(duì)象,也是Java程序獲取數(shù)據(jù)。都是沒(méi)問(wèn)題的。
3. 最后:
“在這個(gè)最后的篇章中,我要表達(dá)我對(duì)每一位讀者的感激之情。你們的關(guān)注和回復(fù)是我創(chuàng)作的動(dòng)力源泉,我從你們身上吸取了無(wú)盡的靈感與勇氣。我會(huì)將你們的鼓勵(lì)留在心底,繼續(xù)在其他的領(lǐng)域奮斗。感謝你們,我們總會(huì)在某個(gè)時(shí)刻再次相遇。”
到此這篇關(guān)于 Spring Boot2 整合連接 Redis的操作方法的文章就介紹到這了,更多相關(guān)Spring Boot2 連接 Redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)自定義Redis的連接的流程步驟
- SpringBoot無(wú)法連接redis的解決方案
- springBoot連接遠(yuǎn)程Redis連接失敗的問(wèn)題解決
- 關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
- springboot連接不上redis的三種解決辦法
- springboot連接redis并動(dòng)態(tài)切換database的實(shí)現(xiàn)方法
- springboot 如何使用jedis連接Redis數(shù)據(jù)庫(kù)
- springboot連接Redis的教程詳解
- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問(wèn)題)
- 基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作
- Springboot2.X集成redis集群(Lettuce)連接的方法
相關(guān)文章
java接口用戶上下文的設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要為大家介紹了接口用戶上下文的設(shè)計(jì)與實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java 中String StringBuilder 與 StringBuffer詳解及用法實(shí)例
這篇文章主要介紹了Java 中String StringBuilder 與 StringBuffer詳解及用法實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02微服務(wù)springcloud 03.Eureka實(shí)現(xiàn)高可用的過(guò)程
這篇文章主要介紹了微服務(wù)springcloud 03.Eureka實(shí)現(xiàn)高可用的相關(guān)資料,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07SSH框架網(wǎng)上商城項(xiàng)目第24戰(zhàn)之Struts2中處理多個(gè)Model請(qǐng)求的方法
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第24戰(zhàn)之Struts2中處理多個(gè)Model請(qǐng)求的方法,感興趣的小伙伴們可以參考一下2016-06-06Java?常量池詳解之字符串常量池實(shí)現(xiàn)代碼
這篇文章主要介紹了Java?常量池詳解之字符串常量池,本文結(jié)合示例代碼對(duì)java字符串常量池相關(guān)知識(shí)講解的非常詳細(xì),需要的朋友可以參考下2022-12-12Java如何接收XML格式參數(shù)并轉(zhuǎn)換為JSON
在 Java 應(yīng)用程序中,處理 XML 數(shù)據(jù)并將其轉(zhuǎn)換為 JSON 格式是很常見(jiàn)的任務(wù),這篇文章為大家整理了一下具體的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助2025-03-03Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序
最近在學(xué)習(xí)Java,然后上個(gè)月迷上了九宮格數(shù)獨(dú),玩了幾天,覺(jué)得實(shí)在有趣,就想著能不能用編程來(lái)解決,于是就自己寫(xiě)了個(gè),還真解決了。下面這篇文章就給大家主要介紹了Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序,需要的朋友可以參考借鑒。2017-01-01