首页 > 数据库 >在springboot项目里配置ReactiveRedisTemplate

在springboot项目里配置ReactiveRedisTemplate

时间:2022-12-17 19:57:44浏览次数:56  
标签:ReactiveRedisTemplate stringSerializer 配置 redis springframework valueSerializer 

配置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.disney.wdpro.service.ecoupon.redemption.service.bean.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.

标签:ReactiveRedisTemplate,stringSerializer,配置,redis,springframework,valueSerializer,
From: https://www.cnblogs.com/zhuwenjoyce/p/16989421.html

相关文章

  • vscode配置
    VScode配置1.开发1.Ctrl+P:显示搜索栏以搜索文件(Mac:Cmd+P)我们经常在多个文件进行切换,这个快捷键可以帮助我们快速找到我们想到的文件。2.Ctrl+Shift+P......
  • 在springboot项目集成r2dbc,集成mysql的流式代码DAO层
    我引用的是springboot2.7.0版本。在pom.xml里引入r2dbc的包,和mysql的驱动包:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot......
  • springboot配置自定义objectMapper
      packagecom.my.config;importcom.fasterxml.jackson.annotation.JsonInclude;importcom.fasterxml.jackson.core.JsonGenerator;importcom.fasterxml.jackso......
  • 【SpringBoot】使用WebSocket做消息对话
    Http协议只能客户端发送---服务器回复,无法做到服务器主动向客户端发送消息,所以可以使用websocket来进行双向通道发消息 研究了一下抖音斗鱼的弹幕也是用的websocket,......
  • maven打包附带依赖的配置
    <build><plugins><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><de......
  • springboot启动读取配置文件过程&自定义配置文件处理器
        最近看到看到spring的配置文件放在了resources/config/application.yal文件内部,第一次见。就想的研究下,springboot启动读取配置文件的过程。1.启动过程org.spring......
  • javaclient操作kafka&springboot整合kafka&kafka分区
    1.javaclient测试kafka1.配置kafka允许远程推送修改config/Kraft/server.properties文件,,将地址变为服务器公网IP地址。advertised.listeners=PLAINTEXT://localhost:90......
  • Eclipse常用开发配置
    1.编码配置有些导入Eclipse项目之后,会出现源码里面中文乱码问题,这时候需要我们修改一下Eclipse的编码设置。1.1输出中文乱码问题点击Windows→General→Workspace选......
  • 1、在自己的腾讯云Linux服务器上配置Sonic
    配置DOCKER配置Docker-compose   ......
  • 关于app.comfig配置文件
    尝试手动创建一个和应用同名的.exe.config文件,右键属性设置为copy到可执行属输出目录下。使用ConfigurationManager.GetSection("")总也读取不到。然后删除执行目录下的......