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<JedisShardInfo> jdsInfoList = new ArrayList<JedisShardInfo>(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);
}
/**
*
* @param args
*
*/
public static void main(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 static String generateKey() {
return String.valueOf(Thread.currentThread().getId()) + "_" + (index++);
}
}
从运行结果中能够看到,不一样的key被分配到不一样的Redis-Server上去了。web
上面的集群模式还存在两个问题:
1.
扩容问题:
由于使用了一致性哈稀进行分片,那么不一样的key
分布到不一样的Redis-Server
上,当咱们须要扩容时,须要增长机器到分片列表中,这时候会使得一样的key
算出来落到跟原来不一样的机器上,这样若是要取某一个值,会出现取不到的状况,对于这种状况,Redis
的做者提出了一种名为Pre-Sharding
的方式:
Pre-Sharding
方法是将每个台物理机上,运行多个不一样断口的Redis
实例,假若有三个物理机,每一个物理机运行三个Redis
实际,那么咱们的分片列表中实际有9
个Redis
实例,当咱们须要扩容时,增长一台物理机,步骤以下:
A.
在新的物理机上运行Redis-Server
;
B.
该Redis-Server
从属于(slaveof)
分片列表中的某一Redis-Server
(假设叫RedisA
);
C.
等主从复制(Replication)
完成后,将客户端分片列表中RedisA
的IP
和端口改成新物理机上Redis-Server
的IP
和端口;
D.
中止RedisA
。
这样至关于将某一Redis-Server
转移到了一台新机器上。Prd-Sharding
其实是一种在线扩容的办法,但仍是很依赖Redis
自己的复制功能的,若是主库快照数据文件过大,这个复制的过程也会好久,同时会给主库带来压力。因此作这个拆分的过程最好选择为业务访问低峰时段进行。
2.
单点故障问题:
仍是用到Redis
主从复制的功能,两台物理主机上分别都运行有Redis-Server
,其中一个Redis-Server
是另外一个的从库,采用双机热备技术,客户端经过虚拟IP
访问主库的物理IP
,当主库宕机时,切换到从库的物理IP
。只是过后修复主库时,应该将以前的从库改成主库(使用命令slaveofno one
),主库变为其从库(使命令slaveofIP PORT
),这样才能保证修复期间新增数据的一致性
最终部署的状况会是,分布式的每台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/partitioningredis