Redis的分片技术一般是通过客户端或代理来实现的
1、用jedis实现分片的时候,服务端不需要做任何配置即可
package com.djhu.redis.test; import java.util.ArrayList; import java.util.List; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; public class JedisShardTest { public static void main(String[] args) { List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>(); jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6379)); jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6380)); ShardedJedis sharded = new ShardedJedis(jedisShardInfoList); sharded.set("key01", "a"); sharded.set("key02", "b"); sharded.set("key03", "c"); sharded.set("key04", "d"); sharded.set("key05", "e"); System.out.println(sharded.get("key03")); } }
2、用Jedis连接池实现分片
package com.djhu.redis.test; import java.util.ArrayList; import java.util.List; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.util.Hashing; import redis.clients.util.Sharded; public class JedisSharedFactory { // 最大可用连接数,默认值为8,如果赋值为-1则表示不限制 private static int MAX_TOTAL = 256; // 最大空闲连接数,默认值为8 private static int MAX_IDLE = 32; // 最小空闲连接数 private static int MIN_IDLE = 4; // 最大等待连接毫秒数,默认值为-1表示永不超时 private static int MAX_WAIT = 3000; // 连接redis超时时间 private static int TIMEOUT = 3000; // true表示验证连接 private static boolean TEST_ON_BORROW = true; //连接池 private static ShardedJedisPool jedisPool = null; public static void initJedisPool() { try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(MAX_TOTAL); config.setMaxIdle(MAX_IDLE); config.setMinIdle(MIN_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>(); jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6379)); jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6380)); jedisPool = new ShardedJedisPool(config, jedisShardInfoList,Hashing.MURMUR_HASH,Sharded.DEFAULT_KEY_TAG_PATTERN); } catch (Exception e) { e.printStackTrace(); } } public synchronized static ShardedJedis getConnection() { try { if (jedisPool != null) { ShardedJedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } public static void returnResource(final ShardedJedis jedis) { if (jedis != null) { jedis.close(); } } public static void main(String[] args) { initJedisPool(); ShardedJedis redis = getConnection(); redis.set("key10", "j"); redis.set("key11", "k"); redis.set("key12", "l"); redis.set("key13", "m"); redis.set("key14", "n"); System.out.print(redis.get("key12")); returnResource(redis); } }