jedis是redis的java客戶端,spring將redis連接池作為一個bean配置。
redis連接池分為兩種,一種是“redis.clients.jedis.shardedjedispool
”,這是基于hash算法的一種分布式集群redis客戶端連接池。
另一種是“redis.clients.jedis.jedispool
”,這是單機環(huán)境適用的redis連接池。
maven導(dǎo)入相關(guān)包:
1
2
3
4
5
6
|
<!-- redis依賴包 --> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version> 2.9 . 0 </version> </dependency> |
shardedjedispool是redis集群客戶端的對象池,可以通過他來操作shardedjedis,下面是shardedjedispool的xml配置,spring-jedis.xml
:
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 引入jedis的properties配置文件 --> <!--如果你有多個數(shù)據(jù)源需要通過<context:property-placeholder管理,且不愿意放在一個配置文件里,那么一定要加上ignore-unresolvable=“ true "--> <context:property-placeholder location= "classpath:properties/redis.properties" ignore-unresolvable= "true" /> <!--shardedjedispool的相關(guān)配置--> <bean id= "jedispoolconfig" class = "redis.clients.jedis.jedispoolconfig" > <!--新版是maxtotal,舊版是maxactive--> <property name= "maxtotal" > <value>${redis.pool.maxactive}</value> </property> <property name= "maxidle" > <value>${redis.pool.maxidle}</value> </property> <property name= "testonborrow" value= "true" /> <property name= "testonreturn" value= "true" /> </bean> <bean id= "shardedjedispool" class = "redis.clients.jedis.shardedjedispool" scope= "singleton" > <constructor-arg index= "0" ref= "jedispoolconfig" /> <constructor-arg index= "1" > <list> <bean class = "redis.clients.jedis.jedisshardinfo" > <constructor-arg name= "host" value= "${redis.uri}" /> </bean> </list> </constructor-arg> </bean> </beans> |
下面是單機環(huán)境下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
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 引入jedis的properties配置文件 --> <!--如果你有多個數(shù)據(jù)源需要通過<context:property-placeholder管理,且不愿意放在一個配置文件里,那么一定要加上ignore-unresolvable=“ true "--> <context:property-placeholder location= "classpath:properties/redis.properties" ignore-unresolvable= "true" /> <!--jedis連接池的相關(guān)配置--> <bean id= "jedispoolconfig" class = "redis.clients.jedis.jedispoolconfig" > <!--新版是maxtotal,舊版是maxactive--> <property name= "maxtotal" > <value>${redis.pool.maxactive}</value> </property> <property name= "maxidle" > <value>${redis.pool.maxidle}</value> </property> <property name= "testonborrow" value= "true" /> <property name= "testonreturn" value= "true" /> </bean> <bean id= "jedispool" class = "redis.clients.jedis.jedispool" > <constructor-arg name= "poolconfig" ref= "jedispoolconfig" /> <constructor-arg name= "host" value= "${redis.host}" /> <constructor-arg name= "port" value= "${redis.port}" type= "int" /> <constructor-arg name= "timeout" value= "${redis.timeout}" type= "int" /> <constructor-arg name= "password" value= "${redis.password}" /> <constructor-arg name= "database" value= "${redis.database}" type= "int" /> </bean> </beans> |
對應(yīng)的classpath:properties/redis.properties.xml為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#最大分配的對象數(shù) redis.pool.maxactive= 200 #最大能夠保持idel狀態(tài)的對象數(shù) redis.pool.maxidle= 50 redis.pool.minidle= 10 redis.pool.maxwaitmillis= 20000 #當池內(nèi)沒有返回對象時,最大等待時間 redis.pool.maxwait= 300 #格式:redis: //:[密碼]@[服務(wù)器地址]:[端口]/[db index] redis.uri = redis: //:[email protected]:6379/0 redis.host = 127.0 . 0.1 redis.port = 6379 redis.timeout= 30000 redis.password = 12345 redis.database = 0 |
二者操作代碼類似,都是先注入連接池,然后通過連接池獲得jedis實例,通過實例對象操作redis。
shardedjedis操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@autowired private shardedjedispool shardedjedispool; //注入shardedjedispool @requestmapping (value = "/demo_set" ,method = requestmethod.get) @responsebody public string demo_set(){ //獲取shardedjedis對象 shardedjedis shardjedis = shardedjedispool.getresource(); //存入鍵值對 shardjedis.set( "key1" , "hello jedis" ); //回收shardedjedis實例 shardjedis.close(); return "set" ; } @requestmapping (value = "/demo_get" ,method = requestmethod.get) @responsebody public string demo_get(){ shardedjedis shardedjedis = shardedjedispool.getresource(); //根據(jù)鍵值獲得數(shù)據(jù) string result = shardedjedis.get( "key1" ); shardedjedis.close(); return result; } |
jedis操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@autowired private jedispool jedispool; //注入jedispool @requestmapping (value = "/demo_set" ,method = requestmethod.get) @responsebody public string demo_set(){ //獲取shardedjedis對象 jedis jedis = jedispool.getresource(); //存入鍵值對 jedis.set( "key2" , "hello jedis one" ); //回收shardedjedis實例 jedis.close(); return "set" ; } @requestmapping (value = "/demo_get" ,method = requestmethod.get) @responsebody public string demo_get(){ jedis jedis = jedispool.getresource(); //根據(jù)鍵值獲得數(shù)據(jù) string result = jedis.get( "key2" ); jedis.close(); return result; } |
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/u014320421/article/details/80576232