public class RedisShardPoolTest {
static ShardedJedisPool 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 jdsInfoList = new ArrayList(2);
JedisShardInfo infoA = new JedisShardInfo(hostA, portA);
infoA.setPassword("testpass");
JedisShardInfo infoB = new JedisShardInfo(hostB, portB);
infoB.setPassword("testpass");
jdsInfoList.add(infoA);
jdsInfoList.add(infoB);
pool = new ShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH,
Sharded.DEFAULT_KEY_TAG_PATTERN);
最终部署的情况会是,分布式的每台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