Springboot中配置RedisTemplate
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.2.6.RELEASE</version>
<!-- 不使用lettuce -->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- redis锁 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.7.5</version>
</dependency>
连接配置
spring:
redis:
database: 8 # redis数据库索引 # redis服务器地址
port: 6379 # redis端口
#port: 6006
timeout: 30000ms # redis连接超时时间
jedis:
pool: # redis连接池配置
max-active: 20 # 最大可用连接数(默认为8,负数表示无限)
min-idle: 0 # 最小空闲连接数(默认为0,该值只有为正数才有用)
max-idle: 10 # 最大空闲连接数(默认为8,负数表示无限)
max-wait: -1ms # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)
host: ENC(lcYXtUT5BAoX/FS1TTz5V5zdp2tzE2MQ) # redis地址
password: ENC(0QXVaUl0lP30DEQu33sLTXJFuJoppGta) # 密码,在redis配置中设置
Redis配置
在Springboot启动文件同文件层级下新建config目录,在config中新建RedisConfig.java文件
使用Jackson替换默认的序列化工具
package cn.youcats.youcatsmgt.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @program: YouCats
* @description: Redis Config.
* @author: **
* @create: 2022-11-30 11:55:06
**/
@Configuration
public class RedisConfig extends RedisAutoConfiguration {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 将对象序列化
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
// 使用Jackson2JsonRedisSerialize替换默认序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer<>(Object.class);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置key和value的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}