java redis集群连接池_(08)redis之使用java客户端、spring连接redis、redis集群示例...
一、java代碼連接
1、新建工程,并引入以下包:
jedis-2.7.0.jar、commons-pool2-2.3.jar、junit-4.10.jar
2、單實(shí)例連接
/*** 單實(shí)例連接*/@Testpublic voidjedisClient(){//創(chuàng)建一個Jedis的連接
Jedis jedis=new Jedis("192.168.7.151",6379);//可以選擇庫
jedis.select(2);//寫入值
jedis.set("jediskey", "哈哈哈哈");//獲取值
String jedisStr=jedis.get("jediskey");
System.out.println(jedisStr);
Assert.assertEquals("哈哈哈哈",jedisStr);//關(guān)閉連接
jedis.close();
}
2、連接池連接
/*** 連接池連接*/@Testpublic voidjedisPool(){//創(chuàng)建一連接池對象
JedisPool pool=new JedisPool("192.168.7.151",6379);//從連接池中獲得連接
Jedis jedis=pool.getResource();
jedis.select(3);
jedis.set("jeds", "jedis連接池");
String getStr=jedis.get("jeds");
System.out.println(getStr);//關(guān)閉連接
jedis.close();//關(guān)閉連接池
pool.close();
Assert.assertEquals("jedis連接池", getStr);
}
3、JedisCluster連接集群
/*** JedisCluster 連接集群
*@throwsException*/@Testpublic void testJedisCluster() throwsException {//創(chuàng)建一連接,JedisCluster對象,在系統(tǒng)中是單例存在
Set nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.7.151", 7001));
nodes.add(new HostAndPort("192.168.7.151", 7002));
nodes.add(new HostAndPort("192.168.7.151", 7003));
nodes.add(new HostAndPort("192.168.7.151", 7004));
nodes.add(new HostAndPort("192.168.7.151", 7005));
nodes.add(new HostAndPort("192.168.7.151", 7006));
nodes.add(new HostAndPort("192.168.7.151", 7007));
nodes.add(new HostAndPort("192.168.7.151", 7008));
JedisCluster cluster= newJedisCluster(nodes);//執(zhí)行JedisCluster對象中的方法,方法和redis一一對應(yīng)。
cluster.set("cluster-test", "my jedis cluster test");
String result= cluster.get("cluster-test");
System.out.println(result);//程序結(jié)束時需要關(guān)閉JedisCluster對象
cluster.close();
}
二、使用spring配置單機(jī)redis
1、新建工程,并引入以下包:
commons-pool2-2.3.jar
jedis-2.7.0.jar
junit-4.10.jar
spring-expression-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
commons-logging-1.1.1.jar
2、工程中新建Source Folder目錄:config,config里面添加applicationContext.xml,如下:
3、測試方法如下:
/*** spring配置單機(jī)redis*/@Testpublic voidgetJedisPool(){
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
JedisPool pool= (JedisPool) applicationContext.getBean("jedisPool");
Jedis jedis=pool.getResource();
jedis.set("spring","spring測試redis");
String spring= jedis.get("spring");
System.out.println(spring);
jedis.close();
pool.close();
applicationContext.close();
}
三、使用spring配置redis集群
1、工程與引入的包如上
2、新建applicationContext2.xml如下:
3、測試方法如下:
/*** spring配置redis集群*/@Testpublic voidgetJedisPool(){
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext2.xml");
JedisCluster jedisCluster= (JedisCluster) applicationContext.getBean("jedisCluster");
jedisCluster.set("jedisCluster","spring測試jedisCluster");
String spring= jedisCluster.get("jedisCluster");
System.out.println(spring);
applicationContext.close();
}
總結(jié)
以上是生活随笔為你收集整理的java redis集群连接池_(08)redis之使用java客户端、spring连接redis、redis集群示例...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有c基础学java多久_有c十十基础的自
- 下一篇: 可爱英文网名122个