SpringBoot集成Redis之RedisTemplate实践
作者:当归. z Z
文章介绍了在使用SpringBoot与Redis进行交互时,遇到的中文乱码问题,并提供两种解决方法:1) 使用StringRedisTemplate;2) 自定义Redis序列化器,同时,还讨论了在集群模式下,若主机宕机,客户端如何动态感知集群状态
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
![]()
可以看到出现了乱码。
Redis中的key和value均是以二进制的形式存储的,因此客户端输入的key和value都会经过序列化之后才发往Redis服务端。而RedisTemplate所使用序列化方式和命令行客户端采用序列化方式不相同,进而导致序列化之后的二进制数据不同,所以才会导致上述的现象。

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);
}
![]()
但此时发现中文仍是乱码
![]()
此时启动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集群===================== # 获取失败 最大重定向次数 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客户端没有动态感知到集群的最新信息
解决方案:
- 调用RedisClusterClient.reloadPartitions
- 后台基于时间间隔的周期刷新
- 后台基于持续的 断开 和 移动、重定向 的自适应更新
# ========================redis集群===================== # 获取失败 最大重定向次数 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 #支持集群拓扑动态感应刷新,自适应拓扑刷新是否使用所有可用的更新,默认false关闭 spring.data.redis.lettuce.cluster.refresh.adaptive=true #定时刷新 spring.data.redis.lettuce.cluster.refresh.period=2000
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
