1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import
redis.clients.jedis.*;
import
redis.clients.util.JedisClusterCRC16;
import
java.util.ArrayList;
import
java.util.Date;
import
java.util.List;
public
class
ShardingTest {
private
static
String ip =
"127.0.0.1"
;
private
static
int
timeout =
10000
;
public
static
void
main(String[] args) {
JedisPoolConfig poolConfig = getPoolConfig();
List<JedisShardInfo> listRedis = getListSherdInfo();
ShardedJedisPool jedisPool =
new
ShardedJedisPool(poolConfig, listRedis);
ShardedJedis jedis = jedisPool.getResource();
testData(jedis);
//initString(jedis);
//initHash(jedis); //17秒
//initHashPipeline(jedis); //0秒(是的,0秒)
System.out.println(jedis.get(
"k1"
));
System.out.println(jedis.hgetAll(
"product2"
));
//initHashPipeline100000(jedis); //100000 总时间 63秒
jedis.close();
jedisPool.destroy();
}
private
static
JedisPoolConfig getPoolConfig() {
JedisPoolConfig poolConfig =
new
JedisPoolConfig();
poolConfig.setMaxTotal(
200
);
poolConfig.setMaxIdle(
30
);
//就是在进行borrowObject进行处理时,对拿到的connection进行validateObject校验
//poolConfig.setTestOnBorrow(true);
//就是在进行returnObject对返回的connection进行validateObject校验,个人觉得对数据库连接池的管理意义不大
//poolConfig.setTestOnReturn(true);
//GenericObjectPool中针对pool管理,起了一个Evict的TimerTask定时线程进行控制(可通过设置参数
//poolConfig.setTestWhileIdle(true);
return
poolConfig;
}
private
static
List<JedisShardInfo> getListSherdInfo() {
JedisShardInfo redis1 =
new
JedisShardInfo(ip,
9111
);
redis1.setConnectionTimeout(timeout);
JedisShardInfo redis2 =
new
JedisShardInfo(ip,
9112
);
redis2.setConnectionTimeout(timeout);
JedisShardInfo redis3 =
new
JedisShardInfo(ip,
9113
);
redis3.setConnectionTimeout(timeout);
List<JedisShardInfo> listRedis =
new
ArrayList<JedisShardInfo>();
listRedis.add(redis1);
listRedis.add(redis2);
listRedis.add(redis3);
return
listRedis;
}
private
static
void
testData(ShardedJedis jedis) {
String key =
"k5"
;
String value =
"v5"
;
jedis.set(key, value);
System.out.println(
"key:"
+ jedis.get(key));
System.out.println(
"key:"
+ JedisClusterCRC16.getSlot(key));
System.out.println(jedis.type(key));
}
}
|