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的解决办法

  1. 引入依赖:
  compile('com.fasterxml.jackson.core:jackson-annotations')
    compile('com.fasterxml.jackson.core:jackson-databind')
    compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
  1. 在字段上增加注解
  @Column
    @CreationTimestamp
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime createTime;

上面的方法我试过无效。 具体的解决办法 我还在探索中。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐