首先:Jedis是redis的java版本的客户端。
public class JedisTest {
//单机版测试Jedis,不使用连接池
@Test
public void testJedis(){
//创建Jedis对象
Jedis jedis=new Jedis("192.168.146.128", 6379);
//设置对象
jedis.set("key1", "jedis test");
String string=jedis.get("key1");
System.out.println(string);
//关闭jedis
jedis.close();
}
//单机版测试Jedis,这是使用连接池的方式来获取redis的资源
@Test
public void testJedispool(){
JedisPool pool=new JedisPool("192.168.146.128", 6379);
Jedis resource=pool.getResource();
String string=resource.get("key1");
System.out.println(string);
//不要忘记关闭连接池了
pool.close();
resource.close();
}
//集群版,测试redis集群环境
@Test
public void testJiQun(){
HashSet<HostAndPort> nodes=new HashSet<HostAndPort>();
//这里的ip和后面的端口是,在linux系统的ip和配置的不同的redis的端口
nodes.add(new HostAndPort("192.168.146.128",7001));
nodes.add(new HostAndPort("192.168.146.128",7002));
nodes.add(new HostAndPort("192.168.146.128",7003));
nodes.add(new HostAndPort("192.168.146.128",7004));
nodes.add(new HostAndPort("192.168.146.128",7005));
nodes.add(new HostAndPort("192.168.146.128",7006));
JedisCluster cluster=new JedisCluster(nodes);
cluster.set("key4", "hello world");
String string=cluster.get("key4");
System.out.println(string);
cluster.close();
}