SpringDate的 ReactiveRedisTemplate使用教程
com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string.Spring Reactive Redis Template 使用
·
Spring cloud版本信息如下
引入Redis的依赖
我使用的是比较新的 reactive redis 中间踩了太多的坑了, 会在后面一一介绍的
dependencies {
implementation project(":common")
compile('org.springframework.boot:spring-boot-starter-data-redis-reactive')
compile('org.springframework.boot:spring-boot-starter-web')
implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.20'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.vladmihalcea:hibernate-types-52:2.10.3'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
Redis 配置
- 配置文件 :
## redis
spring.data.redis.repositories.enabled=true
spring.redis.host=redis-host
spring.redis.port=6379
spring.redis.password=123456
spring.redis.timeout=5000
基本配置, 没啥好说的
- 配置类
此处比较坑,官方文档中有问题
ReactiveRedisTemplate 的生成需要传入RedisSerializationContext序列化配置。
如果按照官方文档中的配置。 关于自定义对象就无法转换成String而报错。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfiguration {
@Bean
public ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) {
RedisSerializationContext.SerializationPair<String> stringSerializationPair = RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer.UTF_8);
RedisSerializationContext.SerializationPair<Object> objectSerializationPair = RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json());
RedisSerializationContext.RedisSerializationContextBuilder builder =
RedisSerializationContext.newSerializationContext();
builder.key(stringSerializationPair);
builder.value(objectSerializationPair);
builder.hashKey(stringSerializationPair);
builder.hashValue(objectSerializationPair);
builder.string(objectSerializationPair);
RedisSerializationContext build = builder.build();
ReactiveRedisTemplate reactiveRedisTemplate = new ReactiveRedisTemplate(connectionFactory, build);
return reactiveRedisTemplate;
}
}
使用RedisTemplate
熟练使用它需要前置知识Spring Reactor 编程
@Autowired
private ReactiveRedisTemplate reactiveRedisTemplate;
public void useRedisExample(){
ReactiveHashOperations<String, String, Object> reactiveHashOperations = reactiveRedisTemplate.opsForHash();
Mono<Long> runningPage = reactiveHashOperations.size("myHash");
Long count = runningPage.subscribeOn(Schedulers.elastic()).block();
Mono<Boolean> putResult = reactiveHashOperations.putIfAbsent("myHash", "myKey", new MyObject());
Boolean putBool = putResult.block();
ReactiveValueOperations<String, String> reactiveValueOperations = reactiveRedisTemplate.opsForValue();
Mono<Boolean> setMono = reactiveValueOperations.set("myKey", "MyValue", Duration.ofHours(1));
Boolean block = setMono.block();
}
Redis关于LocalDateTime序列化的问题
网络上搜索都是针对非Reactor的Redis的解决办法
- 引入依赖:
compile('com.fasterxml.jackson.core:jackson-annotations')
compile('com.fasterxml.jackson.core:jackson-databind')
compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
- 在字段上增加注解
@Column
@CreationTimestamp
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime createTime;
上面的方法我试过无效。 具体的解决办法 我还在探索中。
更多推荐
已为社区贡献3条内容
所有评论(0)