SpringBoot集成Redis之配置、序列化與持久化方式
一、簡介什么是Redis
Redis是一個開源的、基于內(nèi)存的高性能鍵值對存儲數(shù)據(jù)庫,支持多種數(shù)據(jù)結(jié)構(gòu)如字符串、哈希、列表、集合等。
它以其卓越的性能、高可用性和持久性而廣受歡迎。
為什么要使用Redis
Redis的使用可以解決以下問題:
- 低延遲讀寫:提升用戶體驗。
- 支撐海量數(shù)據(jù)和流量:適用于大數(shù)據(jù)和高流量的應用。
- 大規(guī)模集群管理:簡化分布式應用的部署和管理。
- 成本考量:降低硬件、軟件和人力成本。
二、常用Redis類型及操作
1. String類型
介紹:最基本的數(shù)據(jù)存儲類型,用于存儲單個數(shù)據(jù)。
基本操作:
- 添加/修改數(shù)據(jù):
set key value
- 獲取數(shù)據(jù):
get key
- 刪除:
del key
2. Hash類型
介紹:存儲鍵值對集合,適用于存儲對象。
基本操作:
- 添加/修改數(shù)據(jù):
hset key field value
- 獲取數(shù)據(jù):
hget key field
- 刪除數(shù)據(jù):
hdel key field
3. List類型
基本概念:存儲有序的數(shù)據(jù)集合。
基本操作:
- 添加/修改數(shù)據(jù):
lpush key value
(左側(cè)添加),rpush key value
(右側(cè)添加) - 獲取數(shù)據(jù):
lrange key start stop
- 獲取并移除數(shù)據(jù):
lpop key
(左側(cè)移除),rpop key
(右側(cè)移除)
4. Set類型
基本介紹:存儲不重復的集合。
基本操作:
- 添加數(shù)據(jù):
sadd key member
- 獲取數(shù)據(jù):
smembers key
- 刪除數(shù)據(jù):
srem key member
5. Sorted Set (zset)
概念:存儲可排序的集合。
基本操作:
- 添加數(shù)據(jù):
zadd key score member
- 獲取全部數(shù)據(jù):
zrange key start stop
- 刪除數(shù)據(jù):
zrem key member
三、Spring Boot整合Redis
引入依賴
在pom.xml
中添加以下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.0</version> </dependency>
配置yaml
在application.yml
中配置Redis連接信息:
spring: redis: host: 自己的ip port: 自己的端口號 # 默認 6379 database: 0 # Redis使用的數(shù)據(jù)庫 timeout: 18000 # 連接超時事件毫秒 password: yourpassword # 密碼(如果需要) lettuce: pool: max-active: 20 # 連接池最大連接數(shù) max-idle: 5 # 最大阻塞等待時間 min-idle: 0 # 連接池最小空閑連接
RedisTemplate模版序列化配置
配置RedisTemplate
以使用JSON序列化:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.Arrays; @Configuration public class RedisConfig { @Bean public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
Redis的Demo案例
以下是一個簡單的測試案例,展示如何使用RedisTemplate
:
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; @SpringBootTest public class RedisTest { @Autowired RedisTemplate<String, Object> redisTemplate; @Test public void testString() { String key = "name"; String name = "dession"; redisTemplate.opsForValue().set(key, name); redisTemplate.expire(key, 1, TimeUnit.DAYS); System.out.println("Retrieved from Redis: " + redisTemplate.opsForValue().get(key)); } @Test public void testHash() { String key = "users"; redisTemplate.opsForHash().put(key, "name", "zhangsan"); redisTemplate.expire(key, 1, TimeUnit.DAYS); Map<String, Object> map = new HashMap<>(); map.put("age", 12); map.put("sex", "男"); redisTemplate.opsForHash().putAll(key, map); Object name = redisTemplate.opsForHash().get(key, "name"); System.out.println("Name from Hash: " + name); } }
四、高級特性與安全性
高級特性
- 持久化:Redis支持RDB和AOF兩種持久化方式,確保數(shù)據(jù)的安全性。
- 復制:通過主從復制,可以提高數(shù)據(jù)的可用性和讀取性能。
- 哨兵模式:用于實現(xiàn)高可用性,自動故障轉(zhuǎn)移。
- 集群:支持數(shù)據(jù)分片,提高存儲和處理大規(guī)模數(shù)據(jù)集的能力。
安全性
- 密碼認證:配置
requirepass
指令設置密碼,確保只有授權(quán)用戶可以訪問Redis。 - 網(wǎng)絡隔離:將Redis部署在內(nèi)網(wǎng)中,避免直接暴露在公網(wǎng)上。
- TLS/SSL:使用TLS/SSL加密數(shù)據(jù)傳輸,保護數(shù)據(jù)在傳輸過程中的安全。
五、性能優(yōu)化
- 選擇合適的數(shù)據(jù)類型:根據(jù)使用場景選擇合適的數(shù)據(jù)類型,可以提高性能和降低內(nèi)存使用。
- 調(diào)整內(nèi)存使用策略:通過配置
maxmemory-policy
,可以控制內(nèi)存使用和數(shù)據(jù)淘汰策略。 - 監(jiān)控和調(diào)優(yōu):使用Redis自帶的監(jiān)控工具或第三方工具,如Redisson、Spring Boot Actuator等,監(jiān)控Redis性能并進行調(diào)優(yōu)。
六、測試與部署
- 測試:在開發(fā)和測試環(huán)境中充分測試Redis的集成,確保功能正確性和性能滿足要求。
- 部署:在生產(chǎn)環(huán)境中部署Redis時,考慮使用容器化技術如Docker,以便于管理和擴展。
七、數(shù)據(jù)一致性
- 緩存與數(shù)據(jù)庫同步:確保緩存數(shù)據(jù)與數(shù)據(jù)庫數(shù)據(jù)的一致性,可以使用延遲雙刪策略或消息隊列來實現(xiàn)。
八、版本更新
- 平滑升級:定期檢查Redis的新版本,按照官方文檔進行平滑升級,以利用新特性和性能改進。
你可以在Spring Boot項目中成功集成Redis,并使用其強大的數(shù)據(jù)結(jié)構(gòu)來優(yōu)化你的應用程序。Redis的高級特性和最佳實踐還需要你進一步探索和學習。希望這篇文章能幫助你更好地理解和使用Redis。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
如何在SpringBoot項目中使用Oracle11g數(shù)據(jù)庫
這篇文章主要介紹了在SpringBoot項目中使用Oracle11g數(shù)據(jù)庫的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06mybatis之嵌套查詢和嵌套結(jié)果有哪些區(qū)別
這篇文章主要介紹了mybatis之嵌套查詢和嵌套結(jié)果有哪些區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java中Comparable接口和Comparator接口的使用比較
Java中提供了兩種對集合或數(shù)組中元素進行排序的方法,一種是實現(xiàn)Comparable接口,另一種是實現(xiàn)Comparator接口,下面這篇文章主要給大家介紹了關于Java中Comparable接口和Comparator接口使用的相關資料,需要的朋友可以參考下2024-06-06Java中CountDownLatch進行多線程同步詳解及實例代碼
這篇文章主要介紹了Java中CountDownLatch進行多線程同步詳解及實例代碼的相關資料,需要的朋友可以參考下2017-03-03解決maven打包排除類不生效maven-compiler-plugin問題
總結(jié):在Spring Boot項目B中作為項目A的依賴時,排除啟動類不生效的原因是被其他類引用或父POM引入,解決方法是跳過test編譯或注釋掉@SpringBootTest(classes={BApplication.class})2024-11-11ThreadPoolExecutor線程池原理及其execute方法(詳解)
下面小編就為大家?guī)硪黄猅hreadPoolExecutor線程池原理及其execute方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06利用 filter 機制給靜態(tài)資源 url 加上時間戳,來防止js和css文件的緩存問題
這篇文章主要介紹了利用 filter 機制給靜態(tài)資源 url 加上時間戳,來防止js和css文件的緩存問題的相關資料,需要的朋友可以參考下2016-05-05