SpringBoot集成Redis之RedisTemplate實踐
1. 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>2. 配置
# ========================redis單機===================== spring.data.redis.database=0 spring.data.redis.host=192.168.229.102 spring.data.redis.port=6379 spring.data.redis.lettuce.pool.max-active=8 spring.data.redis.lettuce.pool.max-wait=-1ms spring.data.redis.lettuce.pool.max-idle=8 spring.data.redis.lettuce.pool.min-idle=0
3. 簡單使用
package com.example.redis;
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.UUID;
@SpringBootTest
class RedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
private static final String ORDER_KEY = "order: ";
@Test
public void setKey() {
int keyId = 1;
String key = ORDER_KEY + keyId;
String serialNo = UUID.randomUUID().toString();
String value = "訂單" + serialNo;
redisTemplate.opsForValue().set(key, value);
System.out.println("添加訂單成功,訂單編號:" + key);
System.out.println("訂單信息:" + value);
}
@Test
public void getKey() {
int keyId = 1;
String key = ORDER_KEY + keyId;
String value = (String) redisTemplate.opsForValue().get(key);
System.out.println("訂單信息:" + value);
}
}
4. 問題
上面代碼setKey得到結果如圖所示

在redis中查看這個key
![]()
可以看到出現(xiàn)了亂碼。
Redis中的key和value均是以二進制的形式存儲的,因此客戶端輸入的key和value都會經(jīng)過序列化之后才發(fā)往Redis服務端。而RedisTemplate所使用序列化方式和命令行客戶端采用序列化方式不相同,進而導致序列化之后的二進制數(shù)據(jù)不同,所以才會導致上述的現(xiàn)象。

5. 解決
方法一:使用StringRedisTemplate
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void setKey1() {
int keyId = 2;
String key = ORDER_KEY + keyId;
String serialNo = UUID.randomUUID().toString();
String value = "訂單" + serialNo;
stringRedisTemplate.opsForValue().set(key, value);
System.out.println("添加訂單成功,訂單編號:" + key);
System.out.println("訂單信息:" + value);
}
![]()
但此時發(fā)現(xiàn)中文仍是亂碼
![]()
此時啟動redis客戶端時加上 --raw 即可

方法二:自定義配置redis序列化器
RedisTemplate默認使用JDK序列化方式,StringRedisTemplate使用String序列化方式

因此我們可以仿照StringRedisTemplate自定義redis配置,將序列化方式設為合適類型
package com.example.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(RedisSerializer.string());
template.setValueSerializer(RedisSerializer.json());
template.setHashKeySerializer(RedisSerializer.string());
template.setHashValueSerializer(RedisSerializer.json());
return template;
}
}

6. 連接集群
改變配置
# ========================redis集群===================== # 獲取失敗 最大重定向次數(shù) spring.data.redis.cluster.max-redirects=3 spring.data.redis.lettuce.pool.max-active=8 spring.data.redis.lettuce.pool.max-wait=-1ms spring.data.redis.lettuce.pool.max-idle=8 spring.data.redis.lettuce.pool.min-idle=0 spring.data.redis.cluster.nodes=192.168.111.175:6381,192.168.111.175:6382,192.168.111.172:6383,192.168.111.172:6384,192.168.111.174:6385,192.168.111.174:6386
當集群中的一個主機意外宕機,集群能自動感知并自動完成主備切換,但SpringBoot客戶端沒有動態(tài)感知到集群的最新信息
解決方案:
- 調(diào)用RedisClusterClient.reloadPartitions
- 后臺基于時間間隔的周期刷新
- 后臺基于持續(xù)的 斷開 和 移動、重定向 的自適應更新
# ========================redis集群===================== # 獲取失敗 最大重定向次數(shù) spring.data.redis.cluster.max-redirects=3 spring.data.redis.lettuce.pool.max-active=8 spring.data.redis.lettuce.pool.max-wait=-1ms spring.data.redis.lettuce.pool.max-idle=8 spring.data.redis.lettuce.pool.min-idle=0 spring.data.redis.cluster.nodes=192.168.111.175:6381,192.168.111.175:6382,192.168.111.172:6383,192.168.111.172:6384,192.168.111.174:6385,192.168.111.174:6386 #支持集群拓撲動態(tài)感應刷新,自適應拓撲刷新是否使用所有可用的更新,默認false關閉 spring.data.redis.lettuce.cluster.refresh.adaptive=true #定時刷新 spring.data.redis.lettuce.cluster.refresh.period=2000
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot集成RedisTemplate的實現(xiàn)示例
- SpringBoot3.4.0無法找到StringRedisTemplate?bean的問題Consider?defining?a?bean?of?type?‘org.springframework
- SpringBoot整合RedisTemplate實現(xiàn)緩存信息監(jiān)控的基本操作
- SpringBoot引入Redis報org.springframework.data.redis.core.RedisTemplate類找不到錯誤問題
- Springboot中RedisTemplate設置String、Hash、List過期時間
相關文章
防止未登錄用戶操作—基于struts2攔截器的簡單實現(xiàn)
下面小編就為大家?guī)硪黄乐刮吹卿浻脩舨僮鳌趕truts2攔截器的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Java8新特性之lambda的作用_動力節(jié)點Java學院整理
我們期待了很久lambda為java帶來閉包的概念,但是如果我們不在集合中使用它的話,就損失了很大價值?,F(xiàn)有接口遷移成為lambda風格的問題已經(jīng)通過default methods解決了,在這篇文章將深入解析Java集合里面的批量數(shù)據(jù)操作解開lambda最強作用的神秘面紗。2017-06-06
Java如何導出數(shù)據(jù)庫中的所有數(shù)據(jù)表到指定文件夾
這篇文章主要介紹了Java導出數(shù)據(jù)庫中的所有數(shù)據(jù)表到指定文件夾,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
Springboot Mybatis-Plus數(shù)據(jù)庫單元測試實戰(zhàn)(三種方式)
這篇文章主要介紹了Springboot Mybatis-Plus數(shù)據(jù)庫單元測試實戰(zhàn)(三種方式),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
java開發(fā)分布式服務框架Dubbo服務引用過程詳解
這篇文章主要為大家介紹了java開發(fā)分布式服務框架Dubbo服務引用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11

