jedis源码分析(四)-ShardedJedisPool连接池实现

多实例ShardedJedisPool连接池实现:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class ShardedJedisPoolUtil {
        private ShardedJedisPool shardedJedisPool ;
        private ShardedJedis shardedJedis ;
        private String addresss ;
        private String password ;
        public ShardedJedisPoolUtil( final String addresss ) {
               this . addresss = addresss ;
              initPool();
       }
        public ShardedJedisPoolUtil( final String addresss , final String password ) {
               this . addresss = addresss ;
               this . password = password ;
              initPool();
       }
        public void initPool() {
               if ( shardedJedisPool == null ) {
                     JedisPoolConfig config = new JedisPoolConfig();
                      config .setMaxIdle(100);
                      config .setMaxWaitMillis(1000);
                      // 设置空间连接
                      config .setMaxIdle(10);
                     List<JedisShardInfo> shards = new ArrayList<>();
                     JedisShardInfo info = null ;
                      for (String address : addresss .split( "," )) {
                           String[] hostAndPort = address .split( ":" );
                            info = new JedisShardInfo( hostAndPort [0], Integer. valueOf ( hostAndPort [1]));
                            if ( password != null ) {
                                   info .setPassword( password );
                           }
                            shards .add( info );
                     }
                      shardedJedisPool = new ShardedJedisPool( config , shards );
                     System. out .println( "连接池创建成功" );
              }
       }
        public void initJedis() {
               if ( shardedJedis == null ) {
                      shardedJedis = shardedJedisPool .getResource();
              }
              System. out .println( "shardedJedis创建成功" );
       }
        public String set(String key , String value ) {
               if ( shardedJedis == null ) {
                     initJedis();
              }
              String result = null ;
               try {
                      result = shardedJedis .set( key , value );
              } catch (Exception e ) {
                     System. out .println( e .getMessage());
              } finally {
                      shardedJedis .close();
              }
               return result ;
       }
        public static void main(String[] args ) {
               /*
               * for ( int i = 0; i < 100; i++) { ShardedJedisPoolUtil shardedJedisPoolUtil =
               * new ShardedJedisPoolUtil("127.0.0.1:4100,127.0.0.1:4101");
               * System.out.println(shardedJedisPoolUtil.set("test" + i, "value" + i)); }
               */
               // 测试多线程访问 jedis 对象操作 redis
              Thread14 thread1 = new Thread14();
               thread1 .start();
              Thread14 thread2 = new Thread14();
               thread2 .start();
              Thread14 thread3 = new Thread14();
               thread3 .start();
              Thread14 thread4 = new Thread14();
               thread4 .start();
              Thread14 thread5 = new Thread14();
               thread5 .start();
              Thread14 thread6 = new Thread14();
               thread6 .start();
              Thread14 thread7 = new Thread14();
               thread7 .start();
              Thread14 thread8 = new Thread14();
               thread8 .start();
              Thread14 thread9 = new Thread14();
               thread9 .start();
              Thread14 thread10 = new Thread14();
               thread10 .start();
       }
}
class Thread14 extends Thread {
        public void run() {
               while ( true ) {
                     String ts = new SimpleDateFormat( "hh:mm:ss" ).format( new Date());
                     ShardedJedisPoolUtil shardedJedisPoolUtil = new ShardedJedisPoolUtil( "127.0.0.1:4100,127.0.0.1:4101" );
                      try {
                           String key = "hello" + String. valueOf ( new Random().nextInt());
                           String value = String. valueOf ( new Random().nextInt());
                           System. out .println( "线程id::" + Thread. currentThread ().getId() + ": " + ts + ": hello=" + shardedJedisPoolUtil .set( key , value ));
                     } catch (Exception e ) {
                           System. out .println( "线程id::" + Thread. currentThread ().getId() + ": " + ts + ": Cannot connect to Redis  " + e .getMessage());
                     } finally {
                            // shardedJedisPoolUtil.close();
                     }
                      try {
                           Thread. sleep (500L);
                     } catch (InterruptedException e ) {
                            e .printStackTrace();
                     }
              }
       }
}

示例代码中构造方法调用方法initPool()初始化ShardedJedisPool连接池,方法中创建了一个继承 org.apache.commons.pool2.impl.GenericObjectPoolConfig配置对象类的JedisPoolConfig连接池配置类config和一个JedisShardInfo的list集合对象shards,通过config和shards参数构造ShardedJedisPool连接池对象,查看ShardedJedisPool连接池类的构造方法:
 public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards) {
    this ( poolConfig , shards , Hashing. MURMUR_HASH );
  }
  public ShardedJedisPool( final GenericObjectPoolConfig poolConfig , List<JedisShardInfo> shards ,
      Hashing algo ) {
    this ( poolConfig , shards , algo , null );
  }
  public ShardedJedisPool( final GenericObjectPoolConfig poolConfig , List<JedisShardInfo> shards ,
      Pattern keyTagPattern ) {
    this ( poolConfig , shards , Hashing. MURMUR_HASH , keyTagPattern );
  }
  public ShardedJedisPool( final GenericObjectPoolConfig poolConfig , List<JedisShardInfo> shards ,
      Hashing algo , Pattern keyTagPattern ) {
    super ( poolConfig , new ShardedJedisFactory( shards , algo , keyTagPattern ));
  }
ShardedJedisPool继承了实现了Closeable接口的Pool<Jedis>,
public Pool( final GenericObjectPoolConfig poolConfig , PooledObjectFactory<T> factory ) {
    initPool ( poolConfig , factory );
  }
public void initPool ( final GenericObjectPoolConfig poolConfig , PooledObjectFactory<T> factory ) {
    if ( this . internalPool != null ) {
      try {
        closeInternalPool();
      } catch (Exception e ) {
      }
    }
    this . internalPool = new GenericObjectPool<T>( factory , poolConfig );
  }
在Pool的构造方法中调用了initPool方法,在initPool中new了一个org.apache.commons.pool2.impl.GenericObjectPool连接池对象。获取到连接池之后通过shardedJedisPool.getResource();获取到ShardedJedis对象。可以根据ShardedJedis类中方法对redis进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值