在springboot项目里配置ReactiveRedisTemplate

配置pom.xml:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>

配置reids连接:

spring.redis.key.default.expiration.minutes=30
spring.redis.cluster.max-redirects=3
spring.redis.client-type=lettuce
spring.redis.cluster.nodes=127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005,127.0.0.1:7006
spring.redis.password=myredis
spring.redis.connect-timeout=3000
#spring.redis.timeout=3000
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
redis.cache.redemption.rule.minutes=60

配置redis reactive template:

package com.my.config;

import com.my.model.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Joyce Zhu
 * @date: 2022-06-04
 */
@Slf4j
@Configuration
@Component
@RequiredArgsConstructor
public class ConfigRedisCacheForRedemptionRule {

    final ObjectMapper objectMapper;

    ////////////////////////////////////////////////////////////
    // config one model
    ////////////////////////////////////////////////////////////
    @Bean("productModelReactiveRedisTemplate")
    public ReactiveRedisTemplate<String, ProductModel> productModelReactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<ProductModel> valueSerializer = new Jackson2JsonRedisSerializer<>(ProductModel.class);
        valueSerializer.setObjectMapper(objectMapper);
        RedisSerializationContext<String, ProductModel> context = RedisSerializationContext.<String, ProductModel>newSerializationContext()
                .key(stringSerializer)
                .value(valueSerializer)
                .hashKey(stringSerializer)
                .hashValue(valueSerializer)
                .build();
        return new ReactiveRedisTemplate<>(factory, context);
    }

    ////////////////////////////////////////////////////////////
    // config model list
    ////////////////////////////////////////////////////////////

    @Bean("couponRedemptionProductWhitelistModelReactiveRedisTemplate")
    public ReactiveRedisTemplate<String, List<ProductWhitelistModel>> getProductWhitelistModelReactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<List<ProductWhitelistModel>> valueSerializer = new Jackson2JsonRedisSerializer(List.class);
        valueSerializer.setObjectMapper(objectMapper);
        RedisSerializationContext<String, List<ProductWhitelistModel>> context = RedisSerializationContext.<String, List<ProductWhitelistModel>>newSerializationContext()
                .key(stringSerializer)
                .value(valueSerializer)
                .hashKey(stringSerializer)
                .hashValue(valueSerializer)
                .build();
        return new ReactiveRedisTemplate<>(factory, context);
    }

    
    @Bean("reactiveRedisTemplate")
    public ReactiveRedisTemplate<String, Object> objectReactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        valueSerializer.setObjectMapper(objectMapper);

        RedisSerializationContext<String, Object> context = RedisSerializationContext.<String, Object>newSerializationContext()
                .key(stringSerializer)
                .value(valueSerializer)
                .hashKey(stringSerializer)
                .hashValue(valueSerializer)
                .build();
        return new ReactiveRedisTemplate<>(factory, context);
    }

    @Bean("stringReactiveRedisTemplate")
    public ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        RedisSerializationContext<String, String> context = RedisSerializationContext.<String, String>newSerializationContext()
                .key(stringSerializer)
                .value(stringSerializer)
                .hashKey(stringSerializer)
                .hashValue(stringSerializer)
                .build();
        return new ReactiveRedisTemplate<>(factory, context);
    }

    @Bean("redisReactiveHealthIndicator")
    public RedisReactiveHealthIndicator redisReactiveHealthIndicator(ReactiveRedisConnectionFactory factory) {
        return new RedisReactiveHealthIndicator(factory);
    }
}

配置redis template (普通模式,非reactive):

package com.my.config;

import com.my.OrderRequestDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.redis.RedisReactiveHealthIndicator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @author: Joyce Zhu
 * @date: 2022-06-04
 */
@Slf4j
@Configuration
@Component
@RequiredArgsConstructor
public class ConfigRedisCluster {

    final ObjectMapper objectMapper;

    @Primary
    @Bean("redisTemplate")
    public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory) {
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        valueSerializer.setObjectMapper(objectMapper);

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(valueSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(valueSerializer);
        //redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Primary
    @Bean("stringRedisTemplate")
    public RedisTemplate<String, String> getStringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<String> valueSerializer = new Jackson2JsonRedisSerializer<>(String.class);
        valueSerializer.setObjectMapper(objectMapper);

        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(valueSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(valueSerializer);
        //redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}

 

 

end.

posted on 2022-12-17 19:43  梦幻朵颜  阅读(1752)  评论(0编辑  收藏  举报