對(duì)于單實(shí)例的Redis的使用椿猎,我們可以用Jedis,并發(fā)環(huán)境下我們可以用JedisPool寿弱。但是這兩種方法否是針對(duì)于單實(shí)例的Redis的情況下使用的犯眠,但是有時(shí)候我們的業(yè)務(wù)可能不是單實(shí)例Redis能支撐的,那么我們這時(shí)候需要引入多個(gè)實(shí)例進(jìn)行“數(shù)據(jù)分區(qū)”症革。其實(shí)好多人都說(shuō)筐咧,用Redis集群不就搞定了嗎?但是Redis集群無(wú)論部署還是維護(hù)成本都比較高噪矛,對(duì)于一些業(yè)務(wù)來(lái)說(shuō)量蕊,使用起來(lái)還是成本很高。所以艇挨,對(duì)我們來(lái)說(shuō)更好的方案可能是在客戶端實(shí)現(xiàn)對(duì)數(shù)據(jù)的手動(dòng)分區(qū).
對(duì)于分區(qū)的方案残炮,我感覺大多數(shù)人都會(huì)想到Hash,的確Hash是最簡(jiǎn)單最有效的方式缩滨。但是Hash的問(wèn)題是:“單節(jié)點(diǎn)掛掉不可用势就,數(shù)據(jù)量大了不好擴(kuò)容”。對(duì)于如果業(yè)務(wù)的可靠性要求不高同時(shí)數(shù)據(jù)可控的情況下可以考慮數(shù)據(jù)分區(qū)的方式脉漏。
其實(shí)數(shù)據(jù)分區(qū)就是Shard苞冯,其實(shí)Redis已經(jīng)對(duì)Shard有很好的支持了,接下來(lái)簡(jiǎn)單的搞一下數(shù)據(jù)分片:
package redis.clients.jedis.tests;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.*;
import java.util.ArrayList;
import java.util.List;
/**
* ShardJedis的測(cè)試類
*/
public class ShardJedisTest {
private ShardedJedisPool sharedPool;
@Before
public void initJedis(){
JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
config.setTestOnBorrow(true);
String hostA = "127.0.0.1";
int portA = 6381;
String hostB = "127.0.0.1";
int portB = 6382;
List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(2);
JedisShardInfo infoA = new JedisShardInfo(hostA, portA);
JedisShardInfo infoB = new JedisShardInfo(hostB, portB);
jdsInfoList.add(infoA);
jdsInfoList.add(infoB);
sharedPool =new ShardedJedisPool(config, jdsInfoList);
}
@Test
public void testSetKV() throws InterruptedException {
try {
for (int i=0;i<50;i++){
String key = "test"+i;
ShardedJedis jedisClient = sharedPool.getResource();
System.out.println(key+":"+jedisClient.getShard(key).getClient().getHost()+":"+jedisClient.getShard(key).getClient().getPort());
System.out.println(jedisClient.set(key,Math.random()+""));
jedisClient.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
這里我是用JUnit做的測(cè)試侧巨,我在本機(jī)開了兩個(gè)Redis實(shí)例:
端口號(hào)分別是6381和6382舅锄。然后用ShardedJedisPool實(shí)現(xiàn)了一個(gè)Shard,主要是生成了50個(gè)Key司忱,分別存到Redis中巧娱。運(yùn)行結(jié)果如下:
test0:127.0.0.1:6382
OK
test1:127.0.0.1:6382
OK
test2:127.0.0.1:6381
OK
test3:127.0.0.1:6382
OK
test4:127.0.0.1:6382
OK
test5:127.0.0.1:6382
OK
test6:127.0.0.1:6382
OK
test7:127.0.0.1:6382
OK
test8:127.0.0.1:6381
OK
test9:127.0.0.1:6381
可以看到碉怔,KV分別分發(fā)到了不同的Redis實(shí)例,這種Shard的方式需要我們提前計(jì)算好數(shù)據(jù)量的大小禁添,便于決定實(shí)例的個(gè)數(shù)撮胧。同時(shí)這種shard的可靠性不是很好,如果單個(gè)Redis實(shí)例掛掉了老翘,那么這個(gè)實(shí)例便不可用了芹啥。
其實(shí)Shard使用起來(lái)很簡(jiǎn)單,接下來(lái)我們看看ShardedJedisPool的具體的實(shí)現(xiàn):
首先在初始化ShardedJedisPool的時(shí)候我們需要?jiǎng)?chuàng)建一個(gè)JedisShardInfo實(shí)例铺峭,JedisShardInfo主要是對(duì)單個(gè)連接的相關(guān)配置:
public class JedisShardInfo extends ShardInfo<Jedis> {
private static final String REDISS = "rediss";
private int connectionTimeout;
private int soTimeout;
private String host;
private int port;
private String password = null;
private String name = null;
// Default Redis DB
private int db = 0;
private boolean ssl;
private SSLSocketFactory sslSocketFactory;
private SSLParameters sslParameters;
private HostnameVerifier hostnameVerifier;
像連接超時(shí)時(shí)間墓怀、發(fā)送超時(shí)時(shí)間、Host和port等卫键。這些都是之前我們實(shí)例化Jedis用到的傀履。
同時(shí)還需要進(jìn)行JedisPoolConfig的設(shè)置,可以猜到ShardedJedisPool也是基于JedisPool來(lái)實(shí)現(xiàn)的莉炉。
看看ShardedJedisPool的構(gòu)造:
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards) {
this(poolConfig, shards, Hashing.MURMUR_HASH);
}
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards,
Hashing algo) {
this(poolConfig, shards, algo, null);
}
public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards,
Hashing algo, Pattern keyTagPattern) {
super(poolConfig, new ShardedJedisFactory(shards, algo, keyTagPattern));
}
public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
initPool(poolConfig, factory);
}
public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
if (this.internalPool != null) {
try {
closeInternalPool();
} catch (Exception e) {
}
}
this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
}
構(gòu)造方法很長(zhǎng)钓账,但是很清晰,關(guān)鍵點(diǎn)在ShardedJedisFactory的構(gòu)建絮宁,因?yàn)檫@是使用commons-pool的必要工廠類梆暮。同時(shí)我們可以看到,這里分分片策略使用的確實(shí)是Hash绍昂,而且還是沖突率很低的MURMUR_HASH啦粹。這里不了解commons-pool的可以看一下之前的Commons-pool源碼分析[http://www.reibang.com/p/b49452fb3a67]
那么我們直接看ShardedJedisFactory類就好了,因?yàn)閏ommons-pool就是基于這個(gè)工廠類來(lái)管理相關(guān)的對(duì)象的窘游,這里緩存的對(duì)象是ShardedJedis
我們先看一下ShardedJedisFactory:
public ShardedJedisFactory(List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
this.shards = shards;
this.algo = algo;
this.keyTagPattern = keyTagPattern;
}
@Override
public PooledObject<ShardedJedis> makeObject() throws Exception {
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
return new DefaultPooledObject<ShardedJedis>(jedis);
}
@Override
public void destroyObject(PooledObject<ShardedJedis> pooledShardedJedis) throws Exception {
final ShardedJedis shardedJedis = pooledShardedJedis.getObject();
for (Jedis jedis : shardedJedis.getAllShards()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {
}
}
}
@Override
public boolean validateObject(PooledObject<ShardedJedis> pooledShardedJedis) {
try {
ShardedJedis jedis = pooledShardedJedis.getObject();
for (Jedis shard : jedis.getAllShards()) {
if (!shard.ping().equals("PONG")) {
return false;
}
}
return true;
} catch (Exception ex) {
return false;
}
}
其實(shí)這里makeObject是創(chuàng)建一個(gè)ShardedJedis唠椭,同時(shí)ShardedJedis也是連接池里保存的對(duì)象。
可以看到destroyObject和validateObject都是將ShardedJedis里的redis實(shí)例當(dāng)做了一個(gè)整體去對(duì)待忍饰,一個(gè)失敗贪嫂,全部失敗。
接下來(lái)看下ShardedJedis的實(shí)現(xiàn)喘批,這個(gè)里面主要做了Hash的處理和各個(gè)Shard的Client的緩存。
public class ShardedJedis extends BinaryShardedJedis implements JedisCommands, Closeable {
protected ShardedJedisPool dataSource = null;
public ShardedJedis(List<JedisShardInfo> shards) {
super(shards);
}
public ShardedJedis(List<JedisShardInfo> shards, Hashing algo) {
super(shards, algo);
}
public ShardedJedis(List<JedisShardInfo> shards, Pattern keyTagPattern) {
super(shards, keyTagPattern);
}
public ShardedJedis(List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
super(shards, algo, keyTagPattern);
}
這里的dataSource是對(duì)連接池的引用铣揉,用于在Close的時(shí)候資源返還饶深。和JedisPool的思想差不多。
由于ShardedJedis是BinaryShardedJedis的子類逛拱,所以構(gòu)造函數(shù)會(huì)一直向上調(diào)用敌厘,在Shard中:
public Sharded(List<S> shards, Hashing algo, Pattern tagPattern) {
this.algo = algo;
this.tagPattern = tagPattern;
initialize(shards);
}
private void initialize(List<S> shards) {
nodes = new TreeMap<Long, S>();
for (int i = 0; i != shards.size(); ++i) {
final S shardInfo = shards.get(i);
if (shardInfo.getName() == null) for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo);
}
else for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
nodes.put(this.algo.hash(shardInfo.getName() + "*" + shardInfo.getWeight() + n), shardInfo);
}
resources.put(shardInfo, shardInfo.createResource());
}
}
這里主要做整個(gè)ShardedJedis中Jedis緩存池的初始化和分片的實(shí)現(xiàn),可以看到首先獲取shardInfo就是之前的JedisShardInfo朽合,根據(jù)shardInfo生成多個(gè)槽位俱两,將這些槽位存到TreeMap中饱狂,同時(shí)將shardInfo和Jedis的映射存到resources中。當(dāng)我們做Client的獲取的時(shí)候:
首先調(diào)用ShardedJedisPool的getResource方法宪彩,從對(duì)象池中獲取一個(gè)ShardedJedis:
ShardedJedis jedisClient = sharedPool.getResource();
調(diào)用ShardedJedis的getShard方法獲取一個(gè)Jedis實(shí)例——一個(gè)shard休讳。
public R getShard(String key) {
return resources.get(getShardInfo(key));
}
public S getShardInfo(String key) {
return getShardInfo(SafeEncoder.encode(getKeyTag(key)));
}
public S getShardInfo(byte[] key) {
SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key));
if (tail.isEmpty()) {
return nodes.get(nodes.firstKey());
}
return tail.get(tail.firstKey());
}
這里主要是對(duì)key做hash,然后去TreeMap中判斷尿孔,當(dāng)前的key落在哪個(gè)區(qū)間上俊柔,再通過(guò)這個(gè)區(qū)間上的ShardInfo從resources的Map中獲取對(duì)應(yīng)的Jedis實(shí)例。
這也就是說(shuō)活合,每一個(gè)ShardedJedis都維護(hù)了所有的分片雏婶,將多個(gè)實(shí)例當(dāng)成一個(gè)整體去使用,這也就導(dǎo)致白指,只要集群中一個(gè)實(shí)例不可用留晚,整個(gè)ShardedJedis就不可用了。同時(shí)對(duì)于hash的分片方式告嘲,是不可擴(kuò)容的错维,擴(kuò)容之后原本應(yīng)該存儲(chǔ)在一起的數(shù)據(jù)就分離了。
其實(shí)這種是Jedis默認(rèn)提供的分片方式状蜗,其實(shí)針對(duì)我們自己的場(chǎng)景我們也可以嘗試自己做一個(gè)路由機(jī)制需五,例如根據(jù)不同年份、月份的數(shù)據(jù)落到一個(gè)實(shí)例上轧坎。
上面就是所有的數(shù)據(jù)分片的jedis實(shí)現(xiàn)的分析宏邮,我們線上的業(yè)務(wù)也是基于ShardedJedis來(lái)實(shí)現(xiàn)的,由于線上業(yè)務(wù)的QPS不高缸血,量也不是很大蜜氨,所以運(yùn)行還算平穩(wěn)。