spring-boot-starter-Redis主要是通過配置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配置文件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/h363659487/article/details/74955543