Reis的客户端连接方式有如下几种:
1.基本方式
/** * 简单基本方式调用 */ @Test public void test1JedisStandardClient() { Jedis jedis = new Jedis("192.168.56.101", 6379); jedis.set("123", "first line is null"); String valueString = jedis.get("123"); System.out.println("the result of redis statement is :"+valueString); System.out.println("the result of redis statement is :"+jedis.get("111")); jedis.close(); }
2.事务方式(基于乐观锁的事务,事务内的指令即使有执行失败的也不会回滚已经执行的,也不会影响事务内后续的指令的执行)
主要有:watch,multi,exec,unwatch,discard等指令
/** * 事务连接2 redis的事务一般与watch组合使用 */ @Test public void test3JredisTransactionAndWatch(){ JedisPool jedisPool = new JedisPool("192.168.56.101", 6379); Jedis jedis = jedisPool.getResource(); //开始观察“test2” String watch = jedis.watch("test2"); System.out.println(Thread.currentThread().getName()+"--"+watch); //开启事务 Transaction tx = jedis.multi(); tx.set("test2", "I am is tiger"); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //执行事务 List list =tx.exec(); System.out.println(Thread.currentThread().getName()+"exe list :"+list); //结束观察 jedis.unwatch(); } /** * 事务连接2 redis的事务一般与watch组合使用 */ @Test public void test3JedisTransactionAndWatch1(){ JedisPool jedisPool = new JedisPool("192.168.56.101", 6379); Jedis jedis = jedisPool.getResource(); //开始观察“test2” String watch = jedis.watch("test2"); System.out.println(Thread.currentThread().getName()+"--"+watch); //开启事务 Transaction tx = jedis.multi(); tx.set("test2", "I am is snake"); tx.set("test3", "I am is rocker"); //事务执行 List list= tx.exec(); System.out.println(Thread.currentThread().getName()+"exe list :"+list); //结束观察 jedis.unwatch(); }
3.集群方式(实际生产环境中推荐使用,高可用,数据分片,缺点集群不支持事务操作)
/** * 配置文件方式的主从集群模式 */ @Test public void test4JedisClusterBySpring(){ JedisCluster cluster = (JedisCluster) applicationContext.getBean("jedisCluster"); cluster.set("s5", "555"); String result = cluster.get("s5"); System.out.println("the s5 is :"+result); System.out.println("the s4 is :"+cluster.get("s4")); System.out.println("the s3 is :"+cluster.get("s3")); System.out.println("the s2 is :"+cluster.get("s2")); cluster.close(); }
/** * 主从集群模式 */ @Test public void test4JedisCluster(){ //创建cluster Set<HostAndPort> nodeSet = new HashSet<HostAndPort>(); nodeSet.add(new HostAndPort("192.168.56.101", 7001)); nodeSet.add(new HostAndPort("192.168.56.101", 7002)); nodeSet.add(new HostAndPort("192.168.56.101", 7003)); nodeSet.add(new HostAndPort("192.168.56.101", 7004)); nodeSet.add(new HostAndPort("192.168.56.101", 7005)); nodeSet.add(new HostAndPort("192.168.56.101", 7006)); nodeSet.add(new HostAndPort("192.168.56.101", 7007)); JedisCluster cluster = new JedisCluster(nodeSet); cluster.set("s4", "444"); cluster.set("111","光辉岁月"); cluster.set("8090","org.springwork.context.support.AbstractApplicationContext prepareRefresh"); Map<String, JedisPool> nodes = cluster.getClusterNodes(); String result = cluster.get("s4"); System.out.println("the s4 is :"+result); System.out.println("the s4 is :"+cluster.get("111")); cluster.close(); }
4.哨兵方式(防止单节点宕机故障)
/** * 主从的哨兵方式(方便主从架构的故障转移) * master下线后,不需要客户端改变ip和port,应该这种模式是连接哨兵,通过哨兵发现master(推荐) */ @Test public void test2JedisClient1(){ Set<String> sentinels = new HashSet<String>(16); sentinels.add("192.168.56.101:26379");//集群中所有sentinels的地址 sentinels.add("192.168.56.101:26380"); sentinels.add("192.168.56.101:26381"); JedisPoolConfig config = new JedisPoolConfig(); JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,config); Jedis jedis1 = pool.getResource(); try{ // jedis1.set("key","value"); String valueString =jedis1.get("key"); System.out.println("the result of redis statement is :"+valueString); System.out.println("the result of redis statement is :"+jedis1.get("111")); }catch (Exception e) { System.out.println("the system is broken"); } finally { pool.returnResourceObject(jedis1); } pool.close(); }
5.管道方式(执行效率高)
/** * 管道方式的连接 * 我们需要采用异步方式,一次发送很多指令,不同步等待其返回结果。这样可以取得很好的执行效率。 */ @Test public void test5JedisPipeline() { Jedis jedis = new Jedis("192.168.56.101", 6379); //创建管道 Pipeline pipeline = jedis.pipelined(); long start =System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("p"+i, "p"+i); } List list = pipeline.syncAndReturnAll(); long end =System.currentTimeMillis(); System.out.println("PipeLined Set :"+((end - start)/1000.0)+"seconds"); jedis.close(); } /** * 管道中事务方式的连接 */ @Test public void test6JedisPipeline() { Jedis jedis = new Jedis("192.168.56.101", 6379); //创建管道 Pipeline pipeline = jedis.pipelined(); long start =System.currentTimeMillis(); //管道开启事务 pipeline.multi(); for (int i = 0; i < 100000; i++) { pipeline.set("p"+i, "p"+i); } //事务执行 pipeline.exec(); //管道同步 List list = pipeline.syncAndReturnAll(); long end =System.currentTimeMillis(); System.out.println("PipeLined Set :"+((end - start)/1000.0)+"seconds"); jedis.close(); }
其他
appliactioncontext.xml如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd "> <!-- 连接池配置 --> <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> <!-- redis集群 --> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg index="0"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.56.101"></constructor-arg> <constructor-arg index="1" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.56.101"></constructor-arg> <constructor-arg index="1" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.56.101"></constructor-arg> <constructor-arg index="1" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.56.101"></constructor-arg> <constructor-arg index="1" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.56.101"></constructor-arg> <constructor-arg index="1" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.56.101"></constructor-arg> <constructor-arg index="1" value="7006"></constructor-arg> </bean> </set> </constructor-arg> <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg> </bean> </beans>