Spring集成jedis的配置與使用簡(jiǎn)單實(shí)例
jedis是redis的java客戶(hù)端,spring將redis連接池作為一個(gè)bean配置。
redis連接池分為兩種,一種是“redis.clients.jedis.ShardedJedisPool”,這是基于hash算法的一種分布式集群redis客戶(hù)端連接池。
另一種是“redis.clients.jedis.JedisPool”,這是單機(jī)環(huán)境適用的redis連接池。
maven導(dǎo)入相關(guān)包:
<!-- redis依賴(lài)包 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
ShardedJedisPool是redis集群客戶(hù)端的對(duì)象池,可以通過(guò)他來(lái)操作ShardedJedis,下面是ShardedJedisPool的xml配置,spring-jedis.xml:
<?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配置文件 -->
<!--如果你有多個(gè)數(shù)據(jù)源需要通過(guò)<context:property-placeholder管理,且不愿意放在一個(gè)配置文件里,那么一定要加上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>
下面是單機(jī)環(huán)境下redis連接池的配置:
<?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配置文件 -->
<!--如果你有多個(gè)數(shù)據(jù)源需要通過(guò)<context:property-placeholder管理,且不愿意放在一個(gè)配置文件里,那么一定要加上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>
對(duì)應(yīng)的classpath:properties/redis.properties.xml為:
#最大分配的對(duì)象數(shù) redis.pool.maxActive=200 #最大能夠保持idel狀態(tài)的對(duì)象數(shù) redis.pool.maxIdle=50 redis.pool.minIdle=10 redis.pool.maxWaitMillis=20000 #當(dāng)池內(nèi)沒(méi)有返回對(duì)象時(shí),最大等待時(shí)間 redis.pool.maxWait=300 #格式:redis://:[密碼]@[服務(wù)器地址]:[端口]/[db index] redis.uri = redis://:12345@127.0.0.1:6379/0 redis.host = 127.0.0.1 redis.port = 6379 redis.timeout=30000 redis.password = 12345 redis.database = 0
二者操作代碼類(lèi)似,都是先注入連接池,然后通過(guò)連接池獲得jedis實(shí)例,通過(guò)實(shí)例對(duì)象操作redis。
ShardedJedis操作:
@Autowired
private ShardedJedisPool shardedJedisPool;//注入ShardedJedisPool
@RequestMapping(value = "/demo_set",method = RequestMethod.GET)
@ResponseBody
public String demo_set(){
//獲取ShardedJedis對(duì)象
ShardedJedis shardJedis = shardedJedisPool.getResource();
//存入鍵值對(duì)
shardJedis.set("key1","hello jedis");
//回收ShardedJedis實(shí)例
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操作:
@Autowired
private JedisPool jedisPool;//注入JedisPool
@RequestMapping(value = "/demo_set",method = RequestMethod.GET)
@ResponseBody
public String demo_set(){
//獲取ShardedJedis對(duì)象
Jedis jedis = jedisPool.getResource();
//存入鍵值對(duì)
jedis.set("key2","hello jedis one");
//回收ShardedJedis實(shí)例
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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Mybatis-Plus主鍵插入null值報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了Mybatis-Plus主鍵插入null值報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
為什么mybatis中的SqlSession一定要關(guān)閉
這篇文章主要介紹了為什么mybatis中的SqlSession一定要關(guān)閉,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
詳解Spring Boot讀取配置文件與配置文件優(yōu)先級(jí)
這篇文章主要介紹了詳解Spring Boot讀取配置文件與配置文件優(yōu)先級(jí),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Spring?BeanDefinition收集過(guò)程示例詳解
這篇文章主要為大家介紹了Spring?BeanDefinition收集過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Spring?@Scheduled定時(shí)器注解使用方式
這篇文章主要介紹了Spring?@Scheduled定時(shí)器注解使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
idea中acitviti使用acitBPM插件出現(xiàn)亂碼問(wèn)題及解決方法
這篇文章主要介紹了idea中acitviti使用acitBPM插件出現(xiàn)亂碼問(wèn)題及解決方法,通過(guò)將File Encodings內(nèi)容設(shè)置為UTF-8,本文通過(guò)圖文展示,需要的朋友可以參考下2021-06-06
解決@ResponseBody作用在返回類(lèi)型為String的方法時(shí)的坑
這篇文章主要介紹了解決@ResponseBody作用在返回類(lèi)型為String的方法時(shí)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
兩個(gè)小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作
這篇文章主要介紹了兩個(gè)小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09

