Springboot2.X整合redis
一、添加pomredis配置依赖
- springboot 1.5.x版本的默认的Redis客户端是 Jedis实现的,springboot 2.x版本中默认客户端是用 lettuce实现的
- 使用jackson进行redis序列化
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
二、yml文件配置
redis:
database: 0
host: xxxx(主机名称)
port: 6379
password: root
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
timeout: 2000ms
三、redis序列化配置(对key和value都进行序列化)
xxxx:6379> keys *
¬
쳳tudent::123
xxx:6379> get student::16888866
¬첲com.test.domain.StudentZ²ǏDL
java.time.Ser]º²/time/LocalDateTime;LidtLjava/lang/String;Lnameq~xpsr
xpw
ᓑ516888866t小8
- 重写RedisTemplate类型以及对key,value进行序列化
- 配置全局序列化
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
Jackson2JsonRedisSerializer jacksonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSerializer.setObjectMapper(objectMapper);
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(jacksonSerializer);
template.setHashValueSerializer(jacksonSerializer);
template.afterPropertiesSet();
return template;
}
}
四、使用jackosn序列化redis存入时间参数不正确
- 存入时间参数格式与java日期格式不正确会出现序列化失败
Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
xxx:6379> get student::789
["com.test.domain.Student",{"id":"789","name":"小勇","createTime":{"date":{"year":2019,"month":"NOVEMBER","day":22,"era":["java.time.chrono.IsoEra","CE"],"dayOfMonth":22,"dayOfWeek":"FRIDAY","dayOfYear":326,"leapYear":false,"monthValue":11,"prolepticMonth":24238,"chronology":{"id":"ISO","calendarType":"iso8601"}},"time":{"hour":15,"minute":52,"second":57,"nano":716000000},"dayOfMonth":22,"dayOfWeek":"FRIDAY","dayOfYear":326,"month":"NOVEMBER","monthValue":11,"year":2019,"hour":15,"minute":52,"nano":716000000,"second":57,"chronology":["java.time.chrono.IsoChronology",{"id":"ISO","calendarType":"iso8601"}]}}]
- 在实体类上加上jackson注解或是编写jackson配置文件或者
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime createTime;
xxxx:6379> get student::123
["com.test.domain.Student",{"id":"123","name":"小化","createTime":[2019,11,22,15,30,54,437000000]}]
五、由用户去进行序列化
- 使用StringRedis进行key的序列化
- 使用fastJSON进行反序列化
@Autowired
private StringRedisTemplate redisTemplate;
redisTemplate.opsForValue().set(string + student.getId(), JSON.toJSONString(student));
String s = redisTemplate.opsForValue().get(string + student.getId());
Student stu = JSON.parseObject(s, Student.class);
六、测试用例
@Autowired
private RedisTemplate redisTemplate;
private final String string = "student::";
@Test
public void redisTest() {
Student student = new Student();
student.setId("123");
student.setName("小化");
student.setCreateTime(LocalDateTime.now());
redisTemplate.opsForValue().set(string + student.getId(), student);
Student stu = (Student) redisTemplate.opsForValue().get(string + student.getId());
log.info(stu.toString());
}