需要的jar包:spring版本:4.3.6.release,jedis版本:2.9.0,spring-data-redis:1.8.0.release;如果使用jackson序列化的話還額外需要:jackson-annotations和jackson-databind包
spring集成redis單機版:
1.配置redistemplate
1
2
3
4
5
6
|
<bean id= "redistemplate" class = "org.springframework.data.redis.core.redistemplate" > <property name= "connectionfactory" ref= "connectionfactory" /> <property name= "defaultserializer" ref= "stringredisserializer" /> <property name= "keyserializer" ref= "stringredisserializer" /> <property name= "valueserializer" ref= "valueserializer" /> </bean> |
2.配置connectionfactory
1
2
3
4
5
6
7
8
9
10
|
<bean id= "connectionfactory" class = "org.springframework.data.redis.connection.jedis.jedisconnectionfactory" > <!-- 配置ip --> <property name= "hostname" value= "${redis.host}" /> <!-- 配置port --> <property name= "port" value= "${redis.port}" /> <!-- 是否使用連接池--> <property name= "usepool" value= "${redis.usepool}" /> <!-- 配置redis連接池--> <property name= "poolconfig" ref= "poolconfig" /> </bean> |
3.配置連接池
1
2
3
4
5
6
7
8
9
10
|
<bean id= "poolconfig" class = "redis.clients.jedis.jedispoolconfig" > <!--最大空閑實例數--> <property name= "maxidle" value= "${redis.maxidle}" /> <!--最大活躍實例數--> <property name= "maxtotal" value= "${redis.maxtotal}" /> <!--創建實例時最長等待時間--> <property name= "maxwaitmillis" value= "${redis.maxwaitmillis}" /> <!--創建實例時是否驗證--> <property name= "testonborrow" value= "${redis.testonborrow}" /> </bean> |
spring集成redis集群
1.配置redistemplate步驟與單機版一致
2.配置connectionfactory
1
2
3
4
5
6
7
8
|
<bean id= "connectionfactory" class = "org.springframework.data.redis.connection.jedis.jedisconnectionfactory" > <!-- 配置redis連接池--> <constructor-arg ref= "poolconfig" ></constructor-arg> <!-- 配置redis集群--> <constructor-arg ref= "clusterconfig" ></constructor-arg> <!-- 是否使用連接池--> <property name= "usepool" value= "${redis.usepool}" /> </bean> |
3.配置連接池步驟與單機版一致
4.配置redis集群
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<bean id= "clusterconfig" class = "org.springframework.data.redis.connection.redisclusterconfiguration" > <property name= "maxredirects" value= "3" ></property> <property name= "clusternodes" > <set> <bean class = "org.springframework.data.redis.connection.redisclusternode" > <constructor-arg value= "${redis.host1}" ></constructor-arg> <constructor-arg value= "${redis.port1}" ></constructor-arg> </bean> <bean class = "org.springframework.data.redis.connection.redisclusternode" > <constructor-arg value= "${redis.host2}" ></constructor-arg> <constructor-arg value= "${redis.port2}" ></constructor-arg> </bean> ...... </set> </property> </bean |
或者
1
2
3
4
5
6
|
<bean name= "propertysource" class = "org.springframework.core.io.support.resourcepropertysource" > <constructor-arg name= "location" value= "classpath:properties/spring-redis-cluster.properties" /> </bean> <bean id= "clusterconfig" class = "org.springframework.data.redis.connection.redisclusterconfiguration" > <constructor-arg name= "propertysource" ref= "propertysource" /> </bean> |
序列化配置簡述:
1.stringredisserializer:由于redis的key是string類型所以一般使用stringredisserializer
2.valueserializer:對于redis的value序列化,spring-data-redis提供了許多序列化類,這里建議使用jackson2jsonredisserializer,默認為jdkserializationredisserializer
3.jdkserializationredisserializer: 使用jdk提供的序列化功能。 優點是反序列化時不需要提供類型信息(class),但缺點是序列化后的結果非常龐大,是json格式的5倍左右,這樣就會消耗redis服務器的大量內存。
4.jackson2jsonredisserializer:使用jackson庫將對象序列化為json字符串。優點是速度快,序列化后的字符串短小精悍。但缺點也非常致命,那就是此類的構造函數中有一個類型參數,必須提供要序列化對象的類型信息(.class對象)。
使用spring注解式來配置redis,這里只配置集群樣例
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
@configuration @enablecaching public class redisconfig extends cachingconfigurersupport { //spring3支持注解方式獲取value 在application里配置配置文件路徑即可獲取 @value ( "${spring.redis.cluster.nodes}" ) private string clusternodes; @value ( "${spring.redis.cluster.timeout}" ) private long timeout; @value ( "${spring.redis.cluster.max-redirects}" ) private int redirects; @value ( "${redis.maxidle}" ) private int maxidle; @value ( "${redis.maxtotal}" ) private int maxtotal; @value ( "${redis.maxwaitmillis}" ) private long maxwaitmillis; @value ( "${redis.testonborrow}" ) private boolean testonborrow; /** * 選擇redis作為默認緩存工具 * @param redistemplate * @return */ @bean public cachemanager cachemanager(redistemplate redistemplate) { rediscachemanager cachemanager = new rediscachemanager(redistemplate); //cachemanager.setdefaultexpiration(60); //map<string,long> expiresmap=new hashmap<>(); //expiresmap.put("rediscache",5l); //cachemanager.setexpires(expiresmap); return cachemanager; } @bean public redisclusterconfiguration redisclusterconfiguration(){ map<string, object> source = new hashmap<>(); source.put( "spring.redis.cluster.nodes" , clusternodes); source.put( "spring.redis.cluster.timeout" , timeout); source.put( "spring.redis.cluster.max-redirects" , redirects); return new redisclusterconfiguration( new mappropertysource( "redisclusterconfiguration" , source)); } @bean public jedisconnectionfactory redisconnectionfactory(redisclusterconfiguration configuration){ jedispoolconfig poolconfig = new jedispoolconfig(); poolconfig.setmaxidle(maxidle); poolconfig.setmaxtotal(maxtotal); poolconfig.setmaxwaitmillis(maxwaitmillis); poolconfig.settestonborrow(testonborrow); return new jedisconnectionfactory(configuration,poolconfig); } /** * retemplate相關配置 * @param factory * @return */ @bean public redistemplate<string, object> redistemplate(jedisconnectionfactory factory) { redistemplate<string, object> template = new redistemplate<>(); // 配置連接工廠 template.setconnectionfactory(factory); //使用jackson2jsonredisserializer來序列化和反序列化redis的value值(默認使用jdk的序列化方式) jackson2jsonredisserializer jacksonseial = new jackson2jsonredisserializer(object. class ); objectmapper om = new objectmapper(); // 指定要序列化的域,field,get和set,以及修飾符范圍,any是都有包括private和public om.setvisibility(propertyaccessor.all, jsonautodetect.visibility.any); // 指定序列化輸入的類型,類必須是非final修飾的,final修飾的類,比如string,integer等會跑出異常 //om.enabledefaulttyping(objectmapper.defaulttyping.non_final); jacksonseial.setobjectmapper(om); // 值采用json序列化 template.setvalueserializer(jacksonseial); //使用stringredisserializer來序列化和反序列化redis的key值 template.setkeyserializer( new stringredisserializer()); // 設置hash key 和value序列化模式 template.sethashkeyserializer( new stringredisserializer()); template.sethashvalueserializer(jacksonseial); template.afterpropertiesset(); return template; } } |
注意事項:
1.采用注解式配置redis或者使用@configuration需要在配置文件中指定掃描什么哪些包下的配置文件,當然如果是springboot那當我沒說過這句話...
1
|
<context:component-scan base- package = "com.*" /> |
2.spring3支持注解方式獲取value,但是需要在加載的配置文件配置文件路徑即可,具體值自己指定吧...
1
|
<value>classpath:properties/spring-redis-cluster.properties</value> |
3.由于我們公司原有的框架采用的是spring2.5.6, 而對于spring2 和spring2+主要區別(當然是我自己覺得啊)是把各模塊分成了不同的jar,而對于使用spring-data-redis模板化處理redis的話,單機情況下spring2.5.6和spring4不會沖突,而如果使用集群模式需要配置redis集群的時候就會出現jar包沖突,這個時候就看要如何取決了,可以直接使用jediscluster來連接redis集群(不過很多方法都需要自己去寫),也可以把spring2.5.6替換成高版本的spring4,只是框架替換需要注意的事情更多了啊(我們公司的直接全部替換沒啥毛病好吧,o(∩_∩)o哈哈~),至于重寫jedisconnectionfactory和redisclusterconfiguration我還未去嘗試,這個可以作為后續補充吧...
4.順便說句,spring4不在支持ibatis了,如果你需要用spring4,又需要連接ibatis的話,最粗暴的方式是把spring-orm包換成spring3版本,其他的jar還是4版本即可。(當然我這邊直接替換是沒啥問題,但同3一樣可能存在潛在問題啊,所以說嘛,公司有時候還是需要更新換代下吧...)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000018208317