redis.clients.jedis.ShardedJedisPool Java Examples

The following examples show how to use redis.clients.jedis.ShardedJedisPool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JedisShartClient.java    From howsun-javaee-framework with Apache License 2.0 6 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
	JedisShardInfo jedisShardInfo1 = new JedisShardInfo(ip1, port);
	jedisShardInfo1.setPassword(JedisConstant.password);
	list.add(jedisShardInfo1);
	JedisShardInfo jedisShardInfo2 = new JedisShardInfo(ip2, port);
	jedisShardInfo2.setPassword(JedisConstant.password);
	list.add(jedisShardInfo2);
	ShardedJedisPool pool = new ShardedJedisPool(config, list);
	for (int i = 0; i < 2000; i++) {
		ShardedJedis jedis = pool.getResource();
		String key = "howsun_" + i;
		//jedis.set(key, UUID.randomUUID().toString());
		System.out.println(key + "\t" + jedis.get(key) + "\t" + jedis.toString());
		pool.returnResource(jedis);
	}
}
 
Example #2
Source File: RedisClient.java    From Redis_Learning with Apache License 2.0 6 votes vote down vote up
/**
 * ��ʼ����Ƭ��
 */
private void initialShardedPool() {
	// �ػ�������
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxActive(20);
	config.setMaxIdle(5);
	config.setMaxWait(1000l);
	config.setTestOnBorrow(false);

	List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
	JedisShardInfo infoA = new JedisShardInfo(ADDR, PORT);
	infoA.setPassword("redis");
	shards.add(infoA);
	// ���Dz��� ��ʱ���������ķ������������ӷ���������
	// JedisShardInfo infoB = new JedisShardInfo(SUB_ADDR, PORT2);
	// infoB.setPassword("redis");
	// shards.add(infoB);
	// shards = Arrays.asList(infoA,infoB);
	shardedJedisPool = new ShardedJedisPool(config, shards,
			Hashing.MURMUR_HASH, ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
}
 
Example #3
Source File: TestShardedRedis.java    From craft-atom with MIT License 6 votes vote down vote up
private void init() {
	redis1.slaveofnoone();
	redis2.slaveofnoone();
	redis3.slaveofnoone();
	List<Redis> shards = new ArrayList<Redis>(3);
	shards.add(redis1);
	shards.add(redis2);
	shards.add(redis3);
	shardedRedis = RedisFactory.newShardedRedis(shards);
	
	List<JedisShardInfo> shardinfos = new ArrayList<JedisShardInfo>();
	shardinfos.add(new JedisShardInfo(HOST, PORT1));
	shardinfos.add(new JedisShardInfo(HOST, PORT2));
	shardinfos.add(new JedisShardInfo(HOST, PORT3));
	shardedPool = new ShardedJedisPool(new JedisPoolConfig(), shardinfos);
}
 
Example #4
Source File: JedisApiTest.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 分布式连接池异步调用
 * 0.452 seconds
 * 0.43 seconds
 */
@Test
@Ignore
public void testShardPipelinedPool() {
    List<JedisShardInfo> shards = Arrays.asList(
            new JedisShardInfo("localhost",6379),
            new JedisShardInfo("localhost",6379));

    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
    ShardedJedis one = pool.getResource();
    ShardedJedisPipeline pipeline = one.pipelined();
    
    long start = System.currentTimeMillis();
    for (int i = 0; i < COUNTER; i++) {
        pipeline.set("sppn" + i, "n" + i);
    }
    List<Object> results = pipeline.syncAndReturnAll();
    long end = System.currentTimeMillis();
    pool.returnResource(one);
    logger.info("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds");
    pool.destroy();
}
 
Example #5
Source File: JedisApiTest.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 分布式连接池同步调用
 * 1.288 seconds
 * 1.291 seconds
 */
@Test
public void testShardSimplePool() {
	List<JedisShardInfo> shards = Arrays.asList(new JedisShardInfo(
			"localhost", 6379), new JedisShardInfo("localhost", 6379));

	ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
	ShardedJedis one = pool.getResource();

	long start = System.currentTimeMillis();
	for (int i = 0; i < COUNTER; i++) {
		String result = one.set("spn" + i, "n" + i);
	}
	long end = System.currentTimeMillis();
	pool.returnResource(one);
	logger.info("Simple@Pool SET: " + ((end - start) / 1000.0) + " seconds");

	pool.destroy();
}
 
Example #6
Source File: RedisServiceFactory.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	final JedisPoolConfig config = JedisPoolConfigFactory.createJedisPoolConfig();

	final Set<JedisShardInfo> shardInfos = new HashSet<JedisShardInfo>();
	final HostInfo[] hostInfos = HostInfoFactory.split(hosts);
	for (final HostInfo hostInfo : hostInfos) {
		shardInfos.add(hostInfo.createJedisShardInfo());
	}
	if (redisService == null) {
		final ShardedJedisPool jedisPool = new ShardedJedisPool(config, new ArrayList<JedisShardInfo>(shardInfos));
		redisService = new RedisServiceImpl(jedisPool);
	}

	final RedisServiceProxy redisServiceProxy = new RedisServiceProxy();
	this.redisService = redisServiceProxy.bind(redisService);
}
 
Example #7
Source File: RedisClientPool.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
public ShardedJedisPool appendJedis(final RedisConfig conf) {
    Assert.notNull(conf);
    Assert.hasLength(conf.getRedisType());

    if(conf.getCluster() == null || !conf.getCluster()) {
        if (!jedisPool.containsKey(conf.getRedisType())) {
            redisConfigs.put(conf.getRedisType(), conf);
            final ShardedJedisPool pool;
            jedisPool.put(conf.getRedisType(), pool = createJedisPool(conf));
            bindGlobal(conf);
            return pool;
        }
        
        return jedisPool.get(conf.getRedisType());
    }

    throw new RedisClientException("Can't append ShardedJedis, this is a redis cluster config");
}
 
Example #8
Source File: RedisClientPool.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
public ShardedJedis getJedis(final String poolName) {
    Assert.hasText(poolName);
    ShardedJedis shardedJedis = null;
    try {
        final ShardedJedisPool pool = jedisPool.get(poolName);
        if (pool != null) {
            shardedJedis = pool.getResource();
        }

        Assert.notNull(shardedJedis, "Not found ShardedJedis.");
        return shardedJedis;
    } catch (final Throwable e) {
        close(shardedJedis);
        throw new RedisClientException(e.getMessage(), e);
    }
}
 
Example #9
Source File: JedisManagerFactory.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 根据配置创建
 * 
 * @return
 */
private static JedisManager createJedisManager() {

    // 池基本配置
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(Conf.getInt("redis.maxIdle"));
    config.setMinIdle(Conf.getInt("redis.minIdle"));
    config.setMaxWaitMillis(Conf.getLong("redis.maxWaitMillis"));
    config.setTestOnBorrow(Conf.getBoolean("redis.testOnBorrow"));
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    // 链接
    shards.add(new JedisShardInfo(Conf.getString("redis.host"), Conf.getInt("redis.port"), Conf
            .getInt("redis.timeout")));

    // 构造池
    ShardedJedisPool shardedJedisPool = new ShardedJedisPool(config, shards);

    return new JedisManager(shardedJedisPool);
}
 
Example #10
Source File: ShardedJedisPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void checkResourceIsCloseable() throws URISyntaxException {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(1);
  config.setBlockWhenExhausted(false);

  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
  shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380")));
  shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379")));

  ShardedJedisPool pool = new ShardedJedisPool(config, shards);

  ShardedJedis jedis = pool.getResource();
  try {
    jedis.set("hello", "jedis");
  } finally {
    jedis.close();
  }

  ShardedJedis jedis2 = pool.getResource();
  try {
    assertEquals(jedis, jedis2);
  } finally {
    jedis2.close();
  }
}
 
Example #11
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static void freeRedisResource(ShardedJedisPool jedisPool, ShardedJedis shardedJedis, boolean isCommited){
	if (shardedJedis != null && jedisPool != null) {			
		if (isCommited) {				
			jedisPool.returnResource(shardedJedis);
		} else {
			jedisPool.returnBrokenResource(shardedJedis);
		}
	}
}
 
Example #12
Source File: ShardedJedisClientFactoryBean.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	notNull(connectionString, "Property 'connectionString' is required");
	List<HostAndPort> haps = HostAndPort.fromStringArray(connectionString);
	List<JedisShardInfo> jsi = new ArrayList<>();
	for (HostAndPort hap : haps) {
		jsi.add(new JedisShardInfo(hap.getHost(), hap.getPort()));
	}
	shardedJedisPool = new ShardedJedisPool(this, jsi);
}
 
Example #13
Source File: ShardedJedisOperation.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	try{
		this.shardedJedisPool =  beanFactory.getBean(shardedJedisFactoryBeanKey, ShardedJedisPool.class);
	}catch(BeansException be){
		logger.error("["+ shardedJedisFactoryBeanKey +"] not in the spring container.");
		throw be;
	}
}
 
Example #14
Source File: JedisDemo.java    From JavaTutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 多机分布式+连接池。
 */
private static void shardPool() {
    // 生成多机连接信息列表
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add( new JedisShardInfo("127.0.0.1", 6379) );
    shards.add( new JedisShardInfo("192.168.56.102", 6379) );
    
    // 生成连接池配置信息
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxIdle(10);
    config.setMaxTotal(30);
    config.setMaxWaitMillis(3*1000);
    
    // 在应用初始化的时候生成连接池
    ShardedJedisPool pool = new ShardedJedisPool(config, shards);
    
    // 在业务操作时,从连接池获取连接
    ShardedJedis client = pool.getResource();
    try {
        // 执行指令
        String result = client.set("key-string", "Hello, Redis!");
        System.out.println( String.format("set指令执行结果:%s", result) );
        String value = client.get("key-string");
        System.out.println( String.format("get指令执行结果:%s", value) );
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        // 业务操作完成,将连接返回给连接池
        if (null != client) {
            pool.returnResource(client);
        }
    } // end of try block
    
    // 应用关闭时,释放连接池资源
    pool.destroy();
}
 
Example #15
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public RedisCommand(ShardedJedisPool jedisPool) {
	super();
	if (jedisPool == null) {
		throw new IllegalArgumentException("jedisPool is NULL!");
	}
	this.jedisPool = jedisPool;		
}
 
Example #16
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public RedisCommand(ShardedJedisPool jedisPool) {
	super();
	if (jedisPool == null) {
		throw new IllegalArgumentException("jedisPool is NULL!");
	}
	this.jedisPool = jedisPool;		
}
 
Example #17
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public RedisCommand(ShardedJedisPool jedisPool) {
	super();
	if (jedisPool == null) {
		throw new IllegalArgumentException("jedisPool is NULL!");
	}
	this.jedisPool = jedisPool;		
}
 
Example #18
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static void freeRedisResource(ShardedJedisPool jedisPool, ShardedJedis shardedJedis, boolean isCommited){
	if (shardedJedis != null && jedisPool != null) {			
		if (isCommited) {				
			jedisPool.returnResource(shardedJedis);
		} else {
			jedisPool.returnBrokenResource(shardedJedis);
		}
	}
}
 
Example #19
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static void freeRedisResource(ShardedJedisPool jedisPool, ShardedJedis shardedJedis, boolean isCommited){
	if (shardedJedis != null && jedisPool != null) {			
		if (isCommited) {				
			jedisPool.returnResource(shardedJedis);
		} else {
			jedisPool.returnBrokenResource(shardedJedis);
		}
	}
}
 
Example #20
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public RedisCommand(ShardedJedisPool jedisPool) {
	super();
	if (jedisPool == null) {
		throw new IllegalArgumentException("jedisPool is NULL!");
	}
	this.jedisPool = jedisPool;		
}
 
Example #21
Source File: SpringDataRedis.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	JedisShardInfo jedisShardInfo1 = new JedisShardInfo(ip1);
	jedisShardInfo1.setPassword(JedisConstant.password);
	JedisShardInfo jedisShardInfo2 = new JedisShardInfo(ip2);
	jedisShardInfo2.setPassword(JedisConstant.password);

	List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
	jedisShardInfos.add(jedisShardInfo1);
	jedisShardInfos.add(jedisShardInfo2);

	JedisPoolConfig poolConfig = new JedisPoolConfig();
	poolConfig.setMaxActive(JedisConstant.maxActive);
	poolConfig.setMaxIdle(JedisConstant.maxIdle);
	poolConfig.setMaxWait(JedisConstant.maxWait);
	poolConfig.setTestOnBorrow(JedisConstant.testOnBorrow);
	poolConfig.setTestOnReturn(JedisConstant.testOnReturn);


	ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig, jedisShardInfos);

	JedisConnectionFactory factory = new JedisConnectionFactory(jedisShardInfo1);
	StringRedisTemplate template = new StringRedisTemplate(factory);
	for (int i = 0; i < 2000; i++) {
		String key = "howsun_" + i;
		BoundValueOperations<String, String> v = template.boundValueOps(key);
		//jedis.set(key, UUID.randomUUID().toString());
		System.out.println(key + "\t" + v.get() + "\t" + factory.getHostName());
	}

}
 
Example #22
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static void freeRedisResource(ShardedJedisPool jedisPool, ShardedJedis shardedJedis, boolean isCommited){
	if (shardedJedis != null && jedisPool != null) {			
		if (isCommited) {				
			jedisPool.returnResource(shardedJedis);
		} else {
			jedisPool.returnBrokenResource(shardedJedis);
		}
	}
}
 
Example #23
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public RedisCommand(ShardedJedisPool jedisPool) {
	super();
	if (jedisPool == null) {
		throw new IllegalArgumentException("jedisPool is NULL!");
	}
	this.jedisPool = jedisPool;		
}
 
Example #24
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static void freeRedisResource(ShardedJedisPool jedisPool, ShardedJedis shardedJedis, boolean isCommited){
	if (shardedJedis != null && jedisPool != null) {			
		if (isCommited) {				
			jedisPool.returnResource(shardedJedis);
		} else {
			jedisPool.returnBrokenResource(shardedJedis);
		}
	}
}
 
Example #25
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public RedisCommand(ShardedJedisPool jedisPool) {
	super();
	if (jedisPool == null) {
		throw new IllegalArgumentException("jedisPool is NULL!");
	}
	this.jedisPool = jedisPool;		
}
 
Example #26
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static void freeRedisResource(ShardedJedisPool jedisPool, ShardedJedis shardedJedis, boolean isCommited){
	if (shardedJedis != null && jedisPool != null) {			
		if (isCommited) {				
			jedisPool.returnResource(shardedJedis);
		} else {
			jedisPool.returnBrokenResource(shardedJedis);
		}
	}
}
 
Example #27
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public RedisCommand(ShardedJedisPool jedisPool) {
	super();
	if (jedisPool == null) {
		throw new IllegalArgumentException("jedisPool is NULL!");
	}
	this.jedisPool = jedisPool;		
}
 
Example #28
Source File: RedisCommand.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static void freeRedisResource(ShardedJedisPool jedisPool, ShardedJedis shardedJedis, boolean isCommited){
	if (shardedJedis != null && jedisPool != null) {			
		if (isCommited) {				
			jedisPool.returnResource(shardedJedis);
		} else {
			jedisPool.returnBrokenResource(shardedJedis);
		}
	}
}
 
Example #29
Source File: RedisClientPool.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
private ShardedJedisPool createJedisPool(final RedisConfig conf) {
    Assert.notNull(conf);
    try {
        final String[] hostAndports = conf.getHostNames().split(";");
        final List<String> redisHosts = Lists.newArrayList();
        final List<Integer> redisPorts = Lists.newArrayList();
        for (int i = 0; i < hostAndports.length; i++) {
            final String[] hostPort = hostAndports[i].split(":");
            redisHosts.add(hostPort[0]);
            redisPorts.add(Integer.valueOf(hostPort[1]));
        }

        final List<JedisShardInfo> shards = Lists.newArrayList();
        for (int i = 0; i < redisHosts.size(); i++) {
            final String host = (String) redisHosts.get(i);
            final Integer port = (Integer) redisPorts.get(i);
            Integer timeout = conf.getTimeOut();
            if (timeout == null || timeout < 0) {
                timeout = DEFAULT_TIMEOUT;
            }
            
            JedisShardInfo si = new JedisShardInfo(host, port.intValue(), timeout);
            shards.add(si);
        }

        return new ShardedJedisPool(getJedisPoolConfig(conf), shards, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    }
}
 
Example #30
Source File: ShardedRedisCache.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * ShardedRedisCache
 */
public ShardedRedisCache() {
    String cacheModel = PropertyHolder.getProperty("cache.model");
    if (CACHE_MODEL.equals(cacheModel)) {
        Address[] addresses = getAddresses();
        String passwd = CommonUtil.isNotEmpty(addresses) ? addresses[0].getPassword() : null;
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(addresses.length);
        for (Address addr : addresses) {
            JedisShardInfo jedisShardInfo = new JedisShardInfo(addr.getHost(), addr.getPort());
            jedisShardInfo.setPassword(passwd);
            shards.add(jedisShardInfo);
        }
        shardedPool = new ShardedJedisPool(getConfig(), shards);
    }
}