public classRedisShardPoolTest { staticShardedJedisPool pool; static{ JedisPoolConfig config = new JedisPoolConfig();//Jedis池配置 config.setMaxActive(500);//最大活动的对象个数 config.setMaxIdle(1000 * 60);//对象最大空闲时间 config.setMaxWait(1000 * 10);//获取对象时最大等待时间 config.setTestOnBorrow(true); String hostA = "127.0.0.1"; int portA = 6378; String hostB = "127.0.0.1"; int portB = 6379; List<JedisShardInfo> jdsInfoList = new ArrayList<JedisShardInfo>(2); JedisShardInfo infoA = newJedisShardInfo(hostA, portA); infoA.setPassword("testpass"); JedisShardInfo infoB = newJedisShardInfo(hostB, portB); infoB.setPassword("testpass"); jdsInfoList.add(infoA); jdsInfoList.add(infoB); pool = newShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN); } /** * * @param args * */ public static voidmain(String[] args) { for (int i = 0; i < 100; i++) { String key =generateKey(); ShardedJedis jds = null; try{ jds =pool.getResource(); System.out.println(key + ":" +jds.getShard(key).getClient().getHost()); System.out.println(key + ":" +jds.getShard(key).getClient().getPort()); System.out.println(jds.get(key)); System.out.println(jds.set(key,"1111111111111111111111111111111")); } catch(Exception e) { e.printStackTrace(); } finally{ pool.returnResource(jds); } } } private static int index = 1; public staticString generateKey() { return String.valueOf(Thread.currentThread().getId()) + "_" + (index++); } }
从运行结果中可以看到,不同的key被分配到不同的Redis-Server上去了。
附件是一份windows下redis服务端程序和客户端需要的jar包,附件下载后后缀改为.rar后解压缩
可以自己配置分布式和主从测试,
配置文件着重关注以下配置
服务端口和 绑定网卡IP
# Accept connections on the specified port, default is 6379
port 6178
# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for connections.
#
# bind 127.0.0.1
是否为备机(IP:端口)
################################# REPLICATION #################################
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the slave
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
#
slaveof 127.0.0.1 6378
认证密码
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
最好深读官方的解决方案http://redis.io/topics/partitioning