本系列文章<memcached的演练>,这是第6篇,前面文章,已经阐述了,memcached的安装,访问,session管理,存储管理。从本篇开始,就分开有几篇演练下memcached的高可用相关。
memcached的安全,承载着后台数据库的巨大访问,意义重大。
简单介绍下HA的梗概,如果时间允许尽量都演练下个方案,然后横向比较下各方案的优缺点。
本篇主要内容
伪集群方案的测试
明确memcached的高可用方案
通过比较memcached和redis两种NOSQL的方案,很容易发现,memcached的集群方案设计很简单,主要在client实现。
伪集群方案测试步骤
准备演练环境
主机 |
memcached主目录 |
端口 |
192.168.163.146 (hadoop1) |
/usr/local/memcached/ |
11211 |
192.168.163.156 (hadoop2) | /usr/local/memcached/ | 11211 |
192.168.163.166 (hadoop3) | /usr/local/memcached/ | 11211 |
安装不久罗列了,请参照《memcached演练(1) 搭建memcached服务》
2.准备测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
void
testSet()
throws
ExecutionException, InterruptedException, IOException {
final
MemcachedClient mcc =
new
MemcachedClient(
new
BinaryConnectionFactory(),AddrUtil.getAddresses(
"192.168.163.146:11211 192.168.163.156:11211 192.168.163.166:11211"
));
for
(
int
i=
0
;i<
50
;i++){
mcc.set(
"clusterKey_"
+StringUtils.leftPad(
""
+i,
4
,
"0"
),
19000
,
"clusterValue_"
+i);
}
System.out.println(
"set ok"
);
for
(
int
i=
0
;i<
50
;i++){
Object o = mcc.get(
"clusterKey_"
+ StringUtils.leftPad(
""
+i,
4
,
"0"
));
System.out.println(o);
}
// mcc.shutdown();
}
|
3.测试结果分析
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
|
[root@hadoop3 scripts]
# ./memcached-tool hadoop1:11211 dump |sort
Dumping memcache contents
Number of buckets: 1
Number of items : 16
Dumping bucket 1 - 16 total items
add clusterKey_0002 0 1471374793 14
add clusterKey_0005 0 1471374793 14
add clusterKey_0008 0 1471374793 14
add clusterKey_0011 0 1471374793 15
add clusterKey_0014 0 1471374793 15
add clusterKey_0017 0 1471374793 15
add clusterKey_0020 0 1471374793 15
add clusterKey_0023 0 1471374793 15
add clusterKey_0026 0 1471374793 15
add clusterKey_0029 0 1471374793 15
add clusterKey_0032 0 1471374793 15
add clusterKey_0035 0 1471374793 15
add clusterKey_0038 0 1471374793 15
add clusterKey_0041 0 1471374793 15
add clusterKey_0044 0 1471374793 15
add clusterKey_0047 0 1471374793 15
[root@hadoop3 scripts]
# ./memcached-tool hadoop2:11211 dump |sort
Dumping memcache contents
Number of buckets: 1
Number of items : 17
Dumping bucket 1 - 17 total items
add clusterKey_0000 0 1471374795 14
add clusterKey_0003 0 1471374795 14
add clusterKey_0006 0 1471374795 14
add clusterKey_0009 0 1471374795 14
add clusterKey_0012 0 1471374795 15
add clusterKey_0015 0 1471374795 15
add clusterKey_0018 0 1471374795 15
add clusterKey_0021 0 1471374795 15
add clusterKey_0024 0 1471374795 15
add clusterKey_0027 0 1471374795 15
add clusterKey_0030 0 1471374795 15
add clusterKey_0033 0 1471374795 15
add clusterKey_0036 0 1471374795 15
add clusterKey_0039 0 1471374795 15
add clusterKey_0042 0 1471374795 15
add clusterKey_0045 0 1471374795 15
add clusterKey_0048 0 1471374795 15
[root@hadoop3 scripts]
# ./memcached-tool hadoop3:11211 dump |sort
Dumping memcache contents
Number of buckets: 1
Number of items : 17
Dumping bucket 1 - 17 total items
add clusterKey_0001 0 1471374794 14
add clusterKey_0004 0 1471374794 14
add clusterKey_0007 0 1471374794 14
add clusterKey_0010 0 1471374794 15
add clusterKey_0013 0 1471374794 15
add clusterKey_0016 0 1471374794 15
add clusterKey_0019 0 1471374794 15
add clusterKey_0022 0 1471374794 15
add clusterKey_0025 0 1471374794 15
add clusterKey_0028 0 1471374794 15
add clusterKey_0031 0 1471374794 15
add clusterKey_0034 0 1471374794 15
add clusterKey_0037 0 1471374794 15
add clusterKey_0040 0 1471374794 15
add clusterKey_0043 0 1471374794 15
add clusterKey_0046 0 1471374794 15
add clusterKey_0049 0 1471374794 15
|
上面数据,简单整理成图,很直观,数据分布很分散的。这样的好处,可以减少热点。
值得注意的是
1
|
final
MemcachedClient mcc =
new
MemcachedClient(
new
BinaryConnectionFactory(),AddrUtil.getAddresses(
"192.168.163.146:11211 192.168.163.156:11211 192.168.163.166:11211"
));
|
AddrUtil.getAddresses参数,可以重复添加节点信息,这就相当于机器有权重的感觉了,明显会造成数据分布不均匀。
4.测试下权重情况下的数据分布
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public
class
SpyMemcachedClusterTest
extends
TestCase
{
public
void
testSet()
throws
ExecutionException, InterruptedException, IOException {
final
MemcachedClient mcc =
new
MemcachedClient(
new
BinaryConnectionFactory(),AddrUtil.getAddresses(
"192.168.163.146:11211 192.168.163.146:11211 192.168.163.146:11211 192.168.163.156:11211 192.168.163.156:11211 192.168.163.166:11211"
));
for
(
int
i=
0
;i<
150
;i++){
mcc.set(
"clusterKey_"
+StringUtils.leftPad(
""
+i,
4
,
"0"
),
19000
,
"clusterValue_"
+i);
}
System.out.println(
"ok"
);
// mcc.shutdown();
for
(
int
i=
0
;i<
150
;i++){
Object o = mcc.get(
"clusterKey_"
+ StringUtils.leftPad(
""
+i,
4
,
"0"
));
System.out.println(o);
}
}
}
|
测试结果截图
两图比较:是不是很容易,就看出来了。一致性可以从数据分布方面考虑,还是非常平均的,但数据量的分布,就和权重有关系。基本就是 3:2:1。和上面程序代码中配置的权重基本一致。
继续做实验。
5.剔出一个节点,看看命中率
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
void
testGets()
throws
ExecutionException, InterruptedException, IOException{
final
MemcachedClient mcc =
new
MemcachedClient(
new
BinaryConnectionFactory(),AddrUtil.getAddresses(
" 192.168.163.146:11211 192.168.163.146:11211 192.168.163.156:11211 192.168.163.156:11211 192.168.163.166:11211"
));
int
nohit=
0
;
for
(
int
i=
0
;i<
150
;i++){
String key =
"clusterKey_"
+ StringUtils.leftPad(
""
+ i,
4
,
"0"
);
Object o = mcc.get(key);
if
(o==
null
){
System.out.println(key+
"未命中"
);
nohit++;
}
}
System.out.println(
"丢失:"
+nohit);
}
|
修改内容,hadoop1:hadoop2:hadoop3,有原来的 3:2:1分布,修改成2:2:1分布。
测试结果
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
|
clusterKey_0001未命中
clusterKey_0002未命中
clusterKey_0010未命中
clusterKey_0011未命中
clusterKey_0019未命中
clusterKey_0020未命中
clusterKey_0028未命中
clusterKey_0037未命中
clusterKey_0039未命中
clusterKey_0046未命中
clusterKey_0048未命中
clusterKey_0049未命中
clusterKey_0055未命中
clusterKey_0057未命中
clusterKey_0058未命中
clusterKey_0064未命中
clusterKey_0066未命中
clusterKey_0067未命中
clusterKey_0069未命中
clusterKey_0073未命中
clusterKey_0075未命中
clusterKey_0076未命中
clusterKey_0078未命中
clusterKey_0079未命中
clusterKey_0082未命中
clusterKey_0084未命中
clusterKey_0085未命中
clusterKey_0087未命中
clusterKey_0088未命中
clusterKey_0089未命中
clusterKey_0091未命中
clusterKey_0093未命中
clusterKey_0094未命中
clusterKey_0096未命中
clusterKey_0097未命中
clusterKey_0098未命中
clusterKey_0099未命中
clusterKey_0100未命中
clusterKey_0101未命中
clusterKey_0109未命中
clusterKey_0110未命中
clusterKey_0118未命中
clusterKey_0127未命中
clusterKey_0129未命中
clusterKey_0136未命中
clusterKey_0138未命中
clusterKey_0139未命中
clusterKey_0145未命中
clusterKey_0147未命中
clusterKey_0148未命中
丢失:
50
|
丢失了1/3。
即使改成 3:2,丢失也基本上到达1/3。 这个问题需要重视,否则非常容易产生缓存穿透情况。
在以后,会研究下spymemcached的一致性算法。
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1839379,如需转载请自行联系原作者