详解spring boot starter redis配置文件

2025-05-29 0 78

spring-boot-starterRedis主要是通过配置RedisConnectionFactory中的相关参数去实现连接redis service。

RedisConnectionFactory是一个接口,有如下4个具体的实现类,我们通常使用的是JedisConnectionFactory。

在spring boot的配置文件中redis的基本配置如下:

?

1

2

3

4

5

6

7

8
# 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共三个参数的配置,其中还设置了默认的参数。

?

1

2

3

4

5

6
# 连接池最大连接数(使用负值表示没有限制,对应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.;:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41
@Configuration

@EnableCaching

public class RedisConfig extends CachingConfigurerSupport{

@Bean

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(method.getName());

for (Object obj : params) {

sb.append(obj.toString());

}

return sb.toString();

}

};

}

@SuppressWarnings("rawtypes")

@Bean

public CacheManager cacheManager(RedisTemplate redisTemplate) {

RedisCacheManager rcm = new RedisCacheManager(redisTemplate);

//设置缓存过期时间

//rcm.setDefaultExpiration(60);//秒

return rcm;

}

@Bean

public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {

StringRedisTemplate template = new StringRedisTemplate(factory);

@SuppressWarnings({ "rawtypes", "unchecked" })

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;

}

}

如果你还配置了如连接池之类的参数,在上边配置类中加入:

?

1

2

3

4

5

6

7

8

9
@Bean

public JedisConnectionFactory redisConnectionFactory() {

JedisConnectionFactory factory = new JedisConnectionFactory();

factory.setHostName(host);

factory.setPort(port);

factory.setPassword(password);

factory.setTimeout(timeout); //设置连接超时时间

return factory;

}

使用factory进行set你所配置的值即可。

附带解释一点就是在配置类中注入配置文件中的属性方案有多种,如需了解可参考下文:

点击打开链接

以上所述是小编给大家介绍的详解spring boot starter redis配置文件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:http://blog.csdn.net/h363659487/article/details/74955543

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解spring boot starter redis配置文件 https://www.kuaiidc.com/115542.html

相关文章

发表评论
暂无评论