1.pom文件添加依赖:
2.创建配置文件
<!-- jedis集群版配置(JedisCluster通过构造传参(2个参数)) -->
<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.203.137"></constructor-arg>
<constructor-arg name="port" value="6001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.203.137"></constructor-arg>
<constructor-arg name="port" value="6002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.203.137"></constructor-arg>
<constructor-arg name="port" value="6003"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.203.137"></constructor-arg>
<constructor-arg name="port" value="6004"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.203.137"></constructor-arg>
<constructor-arg name="port" value="6005"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.203.137"></constructor-arg>
<constructor-arg name="port" value="6006"></constructor-arg>
</bean>
</set>
</constructor-arg> <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean id="jedisClientCluster" class="com.skymall.rest.dao.imp.JedisClientCluster"></bean>
jedis客户端:
代码:
package com.skymall.rest.dao.imp; import org.springframework.beans.factory.annotation.Autowired; import com.skymall.rest.dao.JedisClient; import redis.clients.jedis.JedisCluster;
/**
* jedis集群客户端dao
* @ClassName: JedisClientCluster
* @Description: TODO
* @author
* @date 2018年3月22日 下午1:44:54
* @version V1.0
*/
public class JedisClientCluster implements JedisClient { @Autowired
private JedisCluster jedisCluster;
@Override
public String get(String key) { return jedisCluster.get(key);
} @Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
} @Override
public String hget(String hkey, String key) {
return jedisCluster.hget(hkey, key);
} @Override
public long hset(String hkey, String key, String value) {
return jedisCluster.hset(hkey,key, value);
} @Override
public long incr(String key) {
return jedisCluster.incr(key);
} @Override
public long expire(String key, int second) {
return jedisCluster.expire(key, second);
} @Override
public long ttl(String key) {
return jedisCluster.ttl(key);
} @Override
public long del(String key) { return jedisCluster.del(key);
} @Override
public long hdel(String hkey, String key) { return jedisCluster.hdel(hkey, key);
} }
测试:
package com.skymall.rest.jedis; import java.util.HashSet; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool; public class TestJedis { //测试jedis
// @Test
// public void testJedis(){
// //创建jedis对象
// Jedis jedis=new Jedis("192.168.203.137",6379);
// //与reids指令操作一至
// jedis.set("key1","88888888888888" );
// //删除
// Long del = jedis.del("cccc");
// //添加
//// String result=jedis.get("cccc");
// System.err.println(del);
// //关闭jedis对象
// jedis.close();
// } // 测试jedis连接池
// @Test
// public void testJedisPool(){
// //创建连接池
// JedisPool jedisPool=new JedisPool("192.168.203.137",6379);
// //从连接池里取jedis对象
// Jedis jedis = jedisPool.getResource();
// //一下操作都一样
// String result=jedis.get("ddd");
// System.err.println(result);
// //关闭jedis
// jedis.close();
// //关闭连接池
// jedisPool.close();
// }
//
//
// //测试redis集群(自带连接池)不需要关闭否则会报错
// @Test
// public void testJedisCluster(){
// HashSet<HostAndPort> nodes=new HashSet<>();
// nodes.add(new HostAndPort("192.168.203.137", 7001));
// nodes.add(new HostAndPort("192.168.203.137", 7002));
// nodes.add(new HostAndPort("192.168.203.137", 7003));
// nodes.add(new HostAndPort("192.168.203.137", 7004));
// nodes.add(new HostAndPort("192.168.203.137", 7005));
// nodes.add(new HostAndPort("192.168.203.137", 7006));
// JedisCluster cluster=new JedisCluster(nodes);
// cluster.set("key2","成功了");
// System.out.println(cluster.get("key2"));
//
//
// }
// //测试单机版jedis与spring整合
// @Test
// public void testJedisAndSpring(){
//
// ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
// JedisPool jedisPool=(JedisPool) applicationContext.getBean("redisClient");
// Jedis jedis=jedisPool.getResource();
// jedis.set("gggg", "09090900");
// String str=jedis.get("gggg");
// System.out.println(str);
//
// jedis.close();
// jedisPool.close();
// }
//
// //测试jedis集群与spring整合
// @Test
// public void JedisClusterAndSpring(){
// ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
// JedisCluster jedisCluster=(JedisCluster) applicationContext.getBean("redisClient");
// jedisCluster.set("name","8822288");
// String str=jedisCluster.get("name");
// System.out.println(str);
//
//
// }
}