默认情况下,SpringBoot中配置的Redis序列化器为JdkSerializationRedisSerializer,这一点我们可以从SpringBoot的默认配置代码中查看获得:
同步模式的AutoConfiguration
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
Reactive模式的AutoConfiguration
@Configuration
@ConditionalOnClass({ ReactiveRedisConnectionFactory.class, ReactiveRedisTemplate.class,
Flux.class })
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisReactiveAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "reactiveRedisTemplate")
@ConditionalOnBean(ReactiveRedisConnectionFactory.class)
public ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(
ReactiveRedisConnectionFactory reactiveRedisConnectionFactory,
ResourceLoader resourceLoader) {
JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(
resourceLoader.getClassLoader());
RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext
.newSerializationContext().key(jdkSerializer).value(jdkSerializer)
.hashKey(jdkSerializer).hashValue(jdkSerializer).build();
return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory,
serializationContext);
}
}
JdkSerializationRedisSerializer的特点是在Redis中Key的值会带一个前缀,一般如下:
127.0.0.1:6379> keys *
1) "\xac\xed\x00\x05t\x00\nJwKjKWCTmf"
这个前缀在一些查询工具中会以乱码的形式展现,自然也没法用查询工具查的其中的值。因此,我们需要做默认的配置做一些变更。
其实SpringBoot已经提供了一个String方式序列化的模板,就如上面所示的配置
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
不过如果我们只需要Key进行String序列化,Value进行不同方式的序列化,那么我们可以覆盖这个默认配置,如下:
@Configuration
public class RedisConfig {
// Reactive 模板
@Bean(name = "reactiveRedisTemplate")
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(
ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext.<String, Object>
newSerializationContext()
.key(new StringRedisSerializer())
.value(new GenericJackson2JsonRedisSerializer()) // GenericJackson2JsonRedisSerializer
.hashKey(new StringRedisSerializer())
.hashValue(new GenericJackson2JsonRedisSerializer()) // GenericJackson2JsonRedisSerializer
.build();
return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
// 同步模板
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(@Autowired RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer()); // GenericJackson2JsonRedisSerializer
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // GenericJackson2JsonRedisSerializer
template.setEnableTransactionSupport(true);
return template;
}
}
网友评论