![](http://static.javashuo.com/static/loading.gif)
package com.taotao.rest.jedis; import java.util.HashSet; import org.junit.Test; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPool; public class JedisTest { @Test public void testJedisSingle() { //建立一個jedis的對象 Jedis jedis = new Jedis("192.168.179.128",6379); jedis.auth("12345678"); //調用jedis對象的方法,方法名稱和redis的命令一致 jedis.set("key1", "testJedisSingle"); String string = jedis.get("key1"); System.out.println(string); //關閉jedis jedis.close(); } /** * 使用鏈接池 */ @Test public void testJedisPool() { //建立jedis鏈接池 JedisPool pool = new JedisPool("192.168.179.128", 6379); //從鏈接池中得到Jedis對象 Jedis jedis = pool.getResource(); jedis.auth("12345678"); String string = jedis.get("key1"); System.out.println(string); //關閉jedis對象 jedis.close(); pool.close(); } //測試集羣redis @Test public void testJedisCluster() throws Exception { HashSet<HostAndPort> nodes = new HashSet<>(); nodes.add(new HostAndPort("192.168.179.128", 7001)); nodes.add(new HostAndPort("192.168.179.128", 7002)); nodes.add(new HostAndPort("192.168.179.128", 7003)); nodes.add(new HostAndPort("192.168.179.128", 7004)); nodes.add(new HostAndPort("192.168.179.128", 7005)); nodes.add(new HostAndPort("192.168.179.128", 7006)); JedisCluster cluster = new JedisCluster(nodes); cluster.set("key2", "kang"); String string = cluster.get("key2"); System.out.println(string); cluster.close(); } }
利用spring的IOC自動注入jedis的bean(參考資料:redis設置密碼以及jedisPool設置密碼)
redis.properties
![](http://static.javashuo.com/static/loading.gif)
#redis單機版JedisPool配置信息 redis.hostName=192.168.179.128 redis.port=6379 redis.timeout=60 redis.password=12345678
application-jedis.xml
![](http://static.javashuo.com/static/loading.gif)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加載屬性文件 --> <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" /> <!-- 鏈接池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大鏈接數 --> <property name="maxTotal" value="30" /> <!-- 最大空閒鏈接數 --> <property name="maxIdle" value="10" /> <!-- 每次釋放鏈接的最大數目 --> <property name="numTestsPerEvictionRun" value="1024" /> <!-- 釋放鏈接的掃描間隔(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <!-- 鏈接最小空閒時間 --> <property name="minEvictableIdleTimeMillis" value="1800000" /> <!-- 鏈接空閒多久後釋放, 當空閒時間>該值 且 空閒鏈接>最大空閒鏈接數 時直接釋放 --> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <!-- 獲取鏈接時的最大等待毫秒數,小於零:阻塞不肯定的時間,默認-1 --> <property name="maxWaitMillis" value="1500" /> <!-- 在獲取鏈接的時候檢查有效性, 默認false --> <property name="testOnBorrow" value="true" /> <!-- 在空閒時檢查有效性, 默認false --> <property name="testWhileIdle" value="true" /> <!-- 鏈接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true --> <property name="blockWhenExhausted" value="false" /> </bean> <!-- jedis客戶端單機版配置 --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg> <constructor-arg name="host" value="${redis.hostName}"></constructor-arg> <constructor-arg name="port" value="${redis.port}"></constructor-arg> <constructor-arg name="timeout" value="${redis.timeout}" type="int"/> <constructor-arg name="password" value="${redis.password}"></constructor-arg> </bean> <bean id="jedisClientSingle" class="com.taotao.rest.dao.impl.JedisClientSingle"/> <!-- jedis集羣版配置 --> <!-- <bean id="redisClient" class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.179.128"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.179.128"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.179.128"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.179.128"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.179.128"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.179.128"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </constructor-arg> <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg> </bean> <bean id="jedisClientCluster" class="com.taotao.rest.dao.impl.JedisClientCluster" /> --> </beans>
測試類的test方法
@Test public void testSpringJedisCluster() throws IOException { //1.獲取spring的IOC容器對象 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext-*.xml"); //2.從IOC容器對象中取出bean的id爲jedisCluster的那個 JedisCluster jedisCluster = (JedisCluster) applicationContext.getBean("redisClient"); jedisCluster.set("name", "kang"); String value = jedisCluster.get("name"); System.out.println(value); //console打印出kang就測試經過 /* 最後要關閉jedis客戶端 */ jedisCluster.close(); }
/taotao-rest/src/main/java/com/taotao/rest/dao/JedisClient.java