1.声明
当前内容用于本人复习和使用redis,主要用于配置redis的值的序列化方式jackson
这里借助spring
2.配置pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version><!-- 这里如果出现NoSuchMethod就表示可能当前的版本有问题 -->
</dependency>
这个在启动的时候出现了一个问题:NoSuchMethod(主要原因当前的spring-boot的配置与当前的spring-boot-starter-redis版本导致的错误
)
3.配置RedisConfig这个配置类
@Configuration
@PropertySource(value = "redis.properties")
@ConditionalOnClass({ RedisConnectionFactory.class, RedisTemplate.class })
public class RedisConfig {
@Value("${redis.maxIdle}")
private Integer maxIdle;
@Value("${redis.maxTotal}")
private Integer maxTotal;
@Value("${redis.maxWaitMillis}")
private Long maxWaitMillis;
@Value("${redis.testOnBorrow}")
private Boolean testOnBorrow;
private JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(maxIdle);
config.setMaxTotal(maxTotal);
config.setMaxWaitMillis(maxWaitMillis);
config.setTestOnBorrow(testOnBorrow);
return config;
}
@Value("${redis.host}")
private String redisHost;
@Value("${redis.port}")
private String redisPort;
@Value("${redis.password}")
private String password;
@Value("${redis.timeout}")
private Integer timeout;
/**
* @description 创建redis连接工厂
*/
@SuppressWarnings("deprecation")
private JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory(new JedisShardInfo(redisHost, redisPort));
factory.setPassword(password);
factory.setTimeout(timeout);
factory.setPoolConfig(jedisPoolConfig());
factory.setUsePool(true);
return factory;
}
/**
* @description 创建RedisTemplate 的操作类
*/
@Bean
public RedisTemplate<String, Object> getRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(keySerializer());
redisTemplate.setValueSerializer(valueSerializer());
redisTemplate.setHashKeySerializer(keySerializer());
redisTemplate.setHashValueSerializer(valueSerializer());
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
// 创建redis的key序列化规则
private RedisSerializer<?> keySerializer() {
return new StringRedisSerializer();
}
// 值使用jackson进行序列化
private RedisSerializer<?> valueSerializer() {
return new GenericJackson2JsonRedisSerializer();
// return new JdkSerializationRedisSerializer();
}
}
这里通过spring-data-redis的GenericJackson2JsonRedisSerializer实现序列化
配置文件:
redis.maxTotal=10
redis.maxIdle=5
redis.maxWaitMillis=2000
redis.testOnBorrow=true
redis.host=localhost
redis.port=6379
redis.timeout=0
redis.password=
redis.usePool=true
redis.testWhileIdle=true
4.启动测试写入
使用jdk序列化方式的结果
查看的结果就是一堆乱码
使用spring-data-redis 提供的jackson序列化方式
这个结果就是标准的json格式,可以很容易的查看
5.总结
1.使用Spring自带的RedisTemplate,可以简化开发,但是使用的时候需要注意序列化方式(一般只配置值得序列化方式
)
2.建议使用json格式的序列化方式,这个方式容易查看
以上纯属个人见解,如有问题请联系本人!