redis.clients.util.Hashing Java Examples

The following examples show how to use redis.clients.util.Hashing. 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: RedisShardedPool.java    From mmall-kay-Java with Apache License 2.0 6 votes vote down vote up
private static void initPool() {
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(maxTotal);
    config.setMaxIdle(maxIdle);
    config.setMinIdle(minIdle);
    config.setBlockWhenExhausted(true); //资源耗尽时是否阻塞
    config.setTestOnBorrow(testOnborrow);
    config.setTestOnReturn(testOnReturn);

    JedisShardInfo info1 = new JedisShardInfo(host1, port1,1000*2,2);  //超时时间默认是2s
    JedisShardInfo info2 = new JedisShardInfo(host2, port2,1000*2,1);

    List<JedisShardInfo> jedisShardInfoList = new ArrayList<>(2);
    jedisShardInfoList.add(info1);
    jedisShardInfoList.add(info2);

    //Hashing.MURMUR_HASH 一致性算法分片,Sharded中有默认分配虚拟节点策略
    pool = new ShardedJedisPool(config, jedisShardInfoList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);
}
 
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: RedisShardedPool.java    From mmall20180107 with Apache License 2.0 6 votes vote down vote up
private static void initPool(){

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);
        jedisPoolConfig.setTestOnReturn(testOnReturn);
        jedisPoolConfig.setBlockWhenExhausted(true);//连接耗尽的时候,是否阻塞,false会抛出异常,true阻塞直到超时。默认为true。

        JedisShardInfo shardInfo1 = new JedisShardInfo(redis1Ip,redis1Port,2000);
        JedisShardInfo shardInfo2 = new JedisShardInfo(redis2Ip,redis2Port,2000);

        List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>(2);
        jedisShardInfoList.add(shardInfo1);
        jedisShardInfoList.add(shardInfo2);

        shardedJedisPool = new ShardedJedisPool(jedisPoolConfig,jedisShardInfoList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);


    }
 
Example #4
Source File: ShardedJedisTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testMasterSlaveShardingConsistencyWithShardNaming() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1, "HOST2:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234"));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234"));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName());
  }
}
 
Example #5
Source File: ShardedJedisTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testMasterSlaveShardingConsistency() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(shards.indexOf(jedisShardInfo), otherShards.indexOf(jedisShardInfo2));
  }

}
 
Example #6
Source File: RedisCacheClient.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
public void init() {

		try {
			String[] hosts = servers.trim().split("\\|");

			List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();

			for (String host : hosts) {
				String[] ss = host.split(":");
				//升级 redis  构造变化
				JedisShardInfo shard = new JedisShardInfo(ss[0], Integer.parseInt(ss[1]), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, 1);
				shards.add(shard);
			}

			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxIdle(MAX_ACTIVE);
			config.setMinIdle(MAX_IDLE);
			config.setMaxWaitMillis(MAX_WAIT);
			config.setMaxTotal(MAX_TOTAL);

			pool = new ShardedJedisPool(config, shards, Hashing.MURMUR_HASH);
		} catch (NumberFormatException e) {
			System.out.println("redis客户端初始化连接异常!!!!!!!!!  链接参数:" + servers
					+ " " + DEFAULT_TIMEOUT + " " + app);
			logger.error("redis:{},exception:{}.", servers + " "
					+ DEFAULT_TIMEOUT + " " + app, e.getMessage());
		}
	}
 
Example #7
Source File: RedisShardedCacheClient.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
@Override
public Pool<ShardedJedis> initPool() throws Exception {
    if (Check.isNullOrEmpty(getServers())) {
        throw new IllegalArgumentException("未指定redis服务器地址");
    }
    String[] hosts = getServers().trim().split("\\|");
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    for (String host : hosts) {
        String[] ss = host.split(":");
        //升级 redis  构造变化
        JedisShardInfo shard = new JedisShardInfo(ss[0], Integer.parseInt(ss[1]), connectTimeout, socketTimeout, 1);
        shards.add(shard);
    }
    return new ShardedJedisPool(getPoolConfig(), shards, Hashing.MURMUR_HASH);
}
 
Example #8
Source File: ShardedJedisSentinelPool.java    From sharded-jedis-sentinel-pool with Apache License 2.0 5 votes vote down vote up
private void initPool(List<HostAndPort> masters) {
	if (!equals(currentHostMasters, masters)) {
		StringBuffer sb = new StringBuffer();
		for (HostAndPort master : masters) {
			sb.append(master.toString());
			sb.append(" ");
		}
		log.info("Created ShardedJedisPool to master at [" + sb.toString() + "]");
		List<JedisShardInfo> shardMasters = makeShardInfoList(masters);
		initPool(poolConfig, new ShardedJedisFactory(shardMasters, Hashing.MURMUR_HASH, null));
		currentHostMasters = masters;
	}
}
 
Example #9
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 #10
Source File: ShardedJedisTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testMurmurSharding() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);
  int shard_6379 = 0;
  int shard_6380 = 0;
  int shard_6381 = 0;
  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    switch (jedisShardInfo.getPort()) {
    case 6379:
      shard_6379++;
      break;
    case 6380:
      shard_6380++;
      break;
    case 6381:
      shard_6381++;
      break;
    default:
      fail("Attempting to use a non-defined shard!!:" + jedisShardInfo);
      break;
    }
  }
  assertTrue(shard_6379 > 300 && shard_6379 < 400);
  assertTrue(shard_6380 > 300 && shard_6380 < 400);
  assertTrue(shard_6381 > 300 && shard_6381 < 400);
}
 
Example #11
Source File: ShardedJedisTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testMD5Sharding() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards, Hashing.MD5);
  int shard_6379 = 0;
  int shard_6380 = 0;
  int shard_6381 = 0;
  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    switch (jedisShardInfo.getPort()) {
    case 6379:
      shard_6379++;
      break;
    case 6380:
      shard_6380++;
      break;
    case 6381:
      shard_6381++;
      break;
    default:
      fail("Attempting to use a non-defined shard!!:" + jedisShardInfo);
      break;
    }
  }
  assertTrue(shard_6379 > 300 && shard_6379 < 400);
  assertTrue(shard_6380 > 300 && shard_6380 < 400);
  assertTrue(shard_6381 > 300 && shard_6381 < 400);
}
 
Example #12
Source File: ShardedCacheFactory.java    From howsun-javaee-framework with Apache License 2.0 4 votes vote down vote up
public ShardedJedisFactory(List<JedisShardInfo> shards, Hashing algo,
		Pattern keyTagPattern) {
	this.shards = shards;
	this.algo = algo;
	this.keyTagPattern = keyTagPattern;
}
 
Example #13
Source File: ShardedCacheFactory.java    From howsun-javaee-framework with Apache License 2.0 4 votes vote down vote up
public ShardedCacheFactory(final CacheFactoryConfig connectionConfig) {
	super(connectionConfig, new ShardedJedisFactory(connectionConfig.getJedisShardInfos(), Hashing.MURMUR_HASH, null));
}
 
Example #14
Source File: ShardedJedisSentinelPool.java    From sharded-jedis-sentinel-pool with Apache License 2.0 4 votes vote down vote up
public ShardedJedisFactory(List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
    this.shards = shards;
    this.algo = algo;
    this.keyTagPattern = keyTagPattern;
}
 
Example #15
Source File: ShardedJedisPool.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public ShardedJedisFactory(List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
  this.shards = shards;
  this.algo = algo;
  this.keyTagPattern = keyTagPattern;
}
 
Example #16
Source File: ShardedJedisPool.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards,
    Hashing algo, Pattern keyTagPattern) {
  super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern));
}
 
Example #17
Source File: ShardedJedisPool.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards,
    Pattern keyTagPattern) {
  this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern);
}
 
Example #18
Source File: ShardedJedisPool.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards,
    Hashing algo) {
  this(poolConfig, shards, algo, null);
}
 
Example #19
Source File: ShardedJedisPool.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards) {
  this(poolConfig, shards, Hashing.MURMUR_HASH);
}
 
Example #20
Source File: BinaryShardedJedis.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public BinaryShardedJedis(List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
  super(shards, algo, keyTagPattern);
}
 
Example #21
Source File: BinaryShardedJedis.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public BinaryShardedJedis(List<JedisShardInfo> shards, Hashing algo) {
  super(shards, algo);
}
 
Example #22
Source File: ShardedJedis.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public ShardedJedis(List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
  super(shards, algo, keyTagPattern);
}
 
Example #23
Source File: ShardedJedis.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
public ShardedJedis(List<JedisShardInfo> shards, Hashing algo) {
  super(shards, algo);
}