spring-boot-starter-redis配置详解
spring-boot-starter-redis主要是通过配置RedisConnectionFactory中的相关参数去实现连接redis service。RedisConnectionFactory是一个接口,有如下4个具体的实现类,我们通常使用的是JedisConnectionFactory。
在spring boot的配置文件中redis的基本配置如下:
# Redis服务器地址
spring.redis.host=192.168.0.58
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空,如果redis服务端配置文件开启了requirepass 密码,此处就应该填写相应的配置密码)
spring.redis.password=
# 连接超时时间(毫秒)
spring.redis.timeout=0
上边这4项是在JedisConnectionFactory类中的基本配置项,里边其实还包含了一些比如连接池,集群,主从,哨兵等的配置,这里先简单介绍下连接池(JedisPoolConfig),需要了解其它配置了可以看下源码。GenericObjectPoolConfig是JedisPoolConfig的父类,主要提供了maxTotal、maxIdle、maxIdle共三个参数的配置,其中还设置了默认的参数。
# 连接池最大连接数(使用负值表示没有限制,对应maxTotal)
spring.redis.pool.max-active=8
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
配置文件配置好后,还需要建立一个redis的配置类,主要用来配置key和value的序列化及加载配置文件中的相关参数
如果你只需要使用基本的redis配置,那么使用如下配置类即可,spring boot会自动扫描redis的基本配置,但是有一项要注意那就是password,如果你在配置文件中设置了password,那么就必须在配置类中手工注入JedisConnectionFactory中,否则会在启动过程中报NOAUTH Authentication required.;:
-
-
-
public class RedisConfig extends CachingConfigurerSupport{
-
-
-
public KeyGenerator keyGenerator() {
-
return new KeyGenerator() {
-
-
public Object generate(Object target, Method method, Object... params) {
-
StringBuilder sb = new StringBuilder();
-
sb.append(target.getClass().getName());
-
sb.append("_").append(method.getName());
-
for (Object obj : params) {
-
sb.append("_").append(obj.toString());
-
}
-
return sb.toString();
-
}
-
};
-
}
-
-
-
-
public CacheManager cacheManager(RedisTemplate redisTemplate) {
-
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
-
//设置缓存过期时间
-
//rcm.setDefaultExpiration(60);//秒
-
return rcm;
-
}
-
-
-
-
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
-
StringRedisTemplate template = new StringRedisTemplate(factory);
-
-
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
-
ObjectMapper om = new ObjectMapper();
-
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
-
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
-
jackson2JsonRedisSerializer.setObjectMapper(om);
-
template.setValueSerializer(jackson2JsonRedisSerializer);
-
template.afterPropertiesSet();
-
JedisConnectionFactory jc = (JedisConnectionFactory) factory;
-
System.out.println(jc.getHostName());
-
return template;
-
}
-
-
}
-
-
public JedisConnectionFactory redisConnectionFactory() {
-
JedisConnectionFactory factory = new JedisConnectionFactory();
-
factory.setHostName(host);
-
factory.setPort(port);
-
factory.setPassword(password);
-
factory.setTimeout(timeout); //设置连接超时时间
-
return factory;
-
}
使用factory进行set你所配置的值即可。
附带解释一点就是在配置类中注入配置文件中的属性方案有多种,如需了解可参考博客:
StringRedisTemplate与RedisTemplate使用时的注意事项:
1、StringRedisTemplate是RedisTemplate的唯一子类
2、StringRedisTemplate默认采用的key序列化方式为setKeySerializer(stringSerializer);此时在使用Spring的缓存注解如@Cacheable的key属性设置值时,就需
-
要注意如果参数类型为Long那么会出不能进行String类型转换异常。
-
3、RedisTemplate默认使用的序列化方式为JdkSerializationRedisSerializer,它就没有上边的问题。因为它的序列化方法为serialize(Object object)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2016-07-12 实战mysql分区(PARTITION)
2013-07-12 linux 自定义信号