jedis 連線redis
阿新 • • 發佈:2018-12-12
一, 單機版連線
@Test public void testJedis() { //1. 建立jedis 物件 Jedis jedis = new Jedis("192.168.88.142", 6379); //2. 直接呼叫 jedis物件的方法, 方法名稱和 redis的命令一致 jedis.set("k1", "v1"); System.out.println(jedis.get("k1")); //3.關閉jedis jedis.close(); }
/** * 使用連線池 */ public void TestJedisPool() { //建立連線池 JedisPool pool = new JedisPool("192.168.88.142", 6379); //從連線池中獲得jedis物件 Jedis jedis = pool.getResource(); System.out.println(jedis.get("k1")); //關閉jedis jedis.close();//關閉連線池 pool.close(); }
二, 連線叢集
/** * 連結叢集 */ @Test public void testJedisCluster() { HashSet<HostAndPort> node = new HashSet(); node.add(new HostAndPort("192.168.88.142",7001)); node.add(new HostAndPort("192.168.88.142",7002)); node.add(new HostAndPort("192.168.88.142",7003)); node.add(new HostAndPort("192.168.88.142",7004)); node.add(new HostAndPort("192.168.88.142",7005)); node.add(new HostAndPort("192.168.88.142",7006)); JedisCluster cluster = new JedisCluster(node); System.out.println(cluster.get("k1")); cluster.close(); }