
在Spring框架中集成Redis时,选择合适的序列化方式对于性能和数据的正确处理至关重要。本文将详细介绍三种常用的Redis序列化方式:StringRedisSerializer、FastJsonRedisSerializer 和 KryoRedisSerializer,并探讨它们的优缺点及适用场景。
StringRedisSerializer 是Spring Data Redis提供的一个默认的序列化器,主要用于字符串类型的序列化。它将对象转换为字节数组,并且在反序列化时将字节数组转换回字符串。
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
1.3 优点
FastJsonRedisSerializer 是基于阿里巴巴的FastJSON库实现的序列化器,可以将Java对象转换为JSON字符串,再将JSON字符串转换为字节数组存储到Redis中。
首先,需要引入FastJSON的依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>然后配置RedisTemplate:
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new FastJsonRedisSerializer<>(Object.class));
return template;
}
}KryoRedisSerializer 是基于Kryo库实现的序列化器,Kryo是一个高效的二进制序列化库,特别适合于对象图的序列化和反序列化。
首先,需要引入Kryo的依赖:
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>5.3.0</version>
</dependency>然后配置RedisTemplate:
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.pool.KryoFactory;
import com.esotericsoftware.kryo.pool.KryoPool;
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;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.nio.ByteBuffer;
@Configuration
public class RedisConfig {
private final KryoPool kryoPool = new KryoPool.Builder(new KryoFactory() {
@Override
public Kryo create() {
Kryo kryo = new Kryo();
kryo.setReferences(true);
kryo.setRegistrationRequired(false);
return kryo;
}
}).build();
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new KryoRedisSerializer<>(kryoPool));
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new KryoRedisSerializer<>(kryoPool));
return template;
}
public static class KryoRedisSerializer<T> implements RedisSerializer<T> {
private final KryoPool pool;
public KryoRedisSerializer(KryoPool pool) {
this.pool = pool;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
try (Kryo kryo = pool.borrow()) {
ByteBufferOutput output = new ByteBufferOutput(1024);
Output out = new Output(output);
kryo.writeClassAndObject(out, t);
out.flush();
return output.toBytes().array();
} catch (Exception e) {
throw new SerializationException("Serialization failed", e);
}
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
return null;
}
try (Kryo kryo = pool.borrow()) {
Input input = new Input(bytes);
return (T) kryo.readClassAndObject(input);
} catch (Exception e) {
throw new SerializationException("Deserialization failed", e);
}
}
}
}
下面我将分别介绍如何在Spring Boot应用中使用StringRedisSerializer、FastJsonRedisSerializer和KryoRedisSerializer来配置Redis的序列化方式,并提供相应的示例代码。
StringRedisSerializerStringRedisSerializer 是最简单的序列化方式,它将对象转换为字符串存储。适合存储简单的字符串数据。
首先,在你的Spring Boot项目中添加Redis依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>然后,配置RedisTemplate使用 StringRedisSerializer:
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new StringRedisSerializer());
return template;
}
}FastJsonRedisSerializerFastJsonRedisSerializer 使用阿里巴巴的FastJSON库进行序列化,适合存储复杂的Java对象。
首先,添加FastJSON依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>然后,配置RedisTemplate使用 FastJsonRedisSerializer:
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
// 设置键(key)的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
// 设置值(value)的序列化采用FastJsonRedisSerializer
template.setValueSerializer(fastJsonRedisSerializer);
template.setHashValueSerializer(fastJsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}KryoRedisSerializerKryoRedisSerializer 使用Kryo库进行序列化,性能更高,但不支持所有类型的对象。
首先,添加Kryo和Kryo-serializer依赖:
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<version>0.46</version>
</dependency>然后,创建一个自定义的 KryoRedisSerializer 类:
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.pool.KryoFactory;
import com.esotericsoftware.kryo.pool.KryoPool;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.ByteBuffer;
public class KryoRedisSerializer<T> implements RedisSerializer<T> {
private final ThreadLocal<Kryo> kryoThreadLocal = ThreadLocal.withInitial(() -> {
Kryo kryo = new Kryo();
kryo.setReferences(false);
kryo.setRegistrationRequired(false);
return kryo;
});
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
try (Output output = new ByteBufferOutput(1024)) {
kryoThreadLocal.get().writeClassAndObject(output, t);
return output.toBytes();
} catch (Exception e) {
throw new SerializationException("Serialization failed", e);
}
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
return null;
}
try (Input input = new Input(bytes)) {
return (T) kryoThreadLocal.get().readClassAndObject(input);
} catch (Exception e) {
throw new SerializationException("Deserialization failed", e);
}
}
}最后,配置RedisTemplate使用 KryoRedisSerializer:
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
KryoRedisSerializer<Object> kryoRedisSerializer = new KryoRedisSerializer<>(Object.class);
// 设置键(key)的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
// 设置值(value)的序列化采用KryoRedisSerializer
template.setValueSerializer(kryoRedisSerializer);
template.setHashValueSerializer(kryoRedisSerializer);
template.afterPropertiesSet();
return template;
}
}以上就是三种不同的Redis序列化方式在Spring Boot中的配置示例。你可以根据实际需求选择合适的序列化方式。在Spring框架中整合Redis时,选择合适的序列化器对于性能和数据的正确处理至关重要。下面将详细介绍三种常见的Redis序列化方式:StringRedisSerializer、FastJsonRedisSerializer 和 KryoRedisSerializer,并提供相应的代码示例。
StringRedisSerializer 是最简单的序列化方式,它将对象转换为字符串。这种方式适用于简单的字符串存储需求。
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new StringRedisSerializer());
return template;
}
}FastJsonRedisSerializer 使用阿里巴巴的FastJSON库进行序列化和反序列化。FastJSON是一个非常高效的JSON处理库,适合处理复杂的对象结构。
首先,需要在项目的pom.xml文件中添加FastJSON的依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 设置key的序列化方式
template.setKeySerializer(new StringRedisSerializer());
// 设置value的序列化方式
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
template.setValueSerializer(fastJsonRedisSerializer);
// 设置hash key的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
// 设置hash value的序列化方式
template.setHashValueSerializer(fastJsonRedisSerializer);
return template;
}
@Bean
public FastJsonRedisSerializer<Object> fastJsonRedisSerializer() {
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.WriteMapNullValue, // 保留空的字段
SerializerFeature.WriteNullStringAsEmpty, // 字符类型如果为null则输出为""
SerializerFeature.WriteNullNumberAsZero // 数字类型如果为null则输出为0
);
return new FastJsonRedisSerializer<>(Object.class);
}
}KryoRedisSerializer 使用Kryo库进行序列化和反序列化。Kryo是一个高性能的序列化库,特别适合于对象图的高效序列化。

首先,需要在项目的pom.xml文件中添加Kryo的依赖:
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>5.3.0</version>
</dependency>import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.pool.KryoFactory;
import com.esotericsoftware.kryo.pool.KryoPool;
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;
import org.springframework.data.redis.serializer.SerializationException;
import java.util.concurrent.Executors;
@Configuration
public class RedisConfig {
private final KryoPool kryoPool = new KryoPool.Builder(new KryoFactory() {
@Override
public Kryo create() {
Kryo kryo = new Kryo();
kryo.setReferences(true);
kryo.setRegistrationRequired(false);
return kryo;
}
}).build();
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 设置key的序列化方式
template.setKeySerializer(new StringRedisSerializer());
// 设置value的序列化方式
template.setValueSerializer(kryoRedisSerializer());
// 设置hash key的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
// 设置hash value的序列化方式
template.setHashValueSerializer(kryoRedisSerializer());
return template;
}
@Bean
public RedisSerializer<Object> kryoRedisSerializer() {
return new RedisSerializer<Object>() {
@Override
public byte[] serialize(Object t) throws SerializationException {
if (t == null) {
return new byte[0];
}
try (Output output = new Output(Executors.newSingleThreadExecutor().submit(() -> {
byte[] buffer = new byte[1024];
return new Output(buffer, buffer.length);
}).get())) {
kryoPool.run(kryo -> kryo.writeClassAndObject(output, t));
return output.toBytes();
} catch (Exception e) {
throw new SerializationException("Serialization failed", e);
}
}
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
return null;
}
try (Input input = new Input(bytes)) {
return kryoPool.run(kryo -> kryo.readClassAndObject(input));
}
}
};
}
}
根据实际需求选择合适的序列化器可以显著提升应用的性能和稳定性。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。