近期因为生产环境需要调整memcached的缓存模式,之前也考虑换redis,因为可能会涉及到应用程序的调整,所以暂时先考虑上memcached repcached模式。
主节点:10.10.10.164 CentOS6.7_x64
从节点:10.10.10.165 CentOS6.7_x64
1)安装依赖关系(时间服务器也要检查下)
1
|
[root@tomcat01 src] # yum -y install install g++ make libevent-devel
|
2)配置memcached(主从节点,同样的操作)
1
2
3
4
5
6
|
[root@tomcat01 ~] # cd /usr/local/src
[root@tomcat01 src] # tar -xf memcached-1.4.24.tar.gz
[root@tomcat01 memcached-1.4.24] # cd ..
[root@tomcat01 src] # cd memcached-1.4.24
[root@tomcat01 memcached-1.4.24] # make
[root@tomcat01 memcached-1.4.24] # make install
|
3)配置memcached-repcached(主从节点,同样的操作)
上传memcached-repached包到/usr/local/src下
1
2
3
4
5
|
[root@tomcat01 src] # tar xf memcached-1.2.8-repcached-2.2.tar.gz
[root@tomcat01 src] # cd memcached-1.2.8-repcached-2.2
[root@tomcat01 memcached-1.2.8-repcached-2.2] # ./configure --enable-replication
[root@tomcat01 memcached-1.2.8-repcached-2.2] # make
[root@tomcat01 memcached-1.2.8-repcached-2.2] # make install
|
4)启动主节点
1
2
3
|
[root@tomcat01 ~] # memcached -v -d -p 11211 -l 10.10.10.164 -u root -P /tmp/memcached1.pid
[root@tomcat01 ~] # replication: listen
说明:accept为启动正常的状态 |
5)启动从节点
1
2
3
4
5
|
[root@tomcat02 ~] # memcached -v -d -p 11211 -l 10.10.10.165 -u root -x 10.10.10.164 -P
/tmp/memcached1 .pid
[root@tomcat02 ~] # replication: connect (peer=10.10.10.164:11212)
replication: marugoto copying replication: start |
6)telnet进行验证
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
|
[root@tomcat01 ~] # telnet 10.10.10.164 11211
Trying 10.10.10.164... Connected to 10.10.10.164. Escape character is '^]' .
set userId 0 0 5
12345 STORED [root@tomcat01 ~] # telnet 10.10.10.164 11211
Trying 10.10.10.164... Connected to 10.10.10.164. Escape character is '^]' .
get userId VALUE userId 0 5 12345 END [root@tomcat02 src] # replication: connect (peer=10.10.10.164:11212)
replication: marugoto copying replication: start replication: close replication: listen replication: accept replication: marugoto start replication: marugoto 1 replication: marugoto owari replication: close replication: listen 说明:以上为连接状态 [root@tomcat02 ~] # telnet 10.10.10.165 11211
Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is '^]' .
get userId VALUE userId 0 5 12345 END 说明:根据上面的输出结果,可以判断userId的键值为12345 |
7)模拟故障(如主memcached服务挂掉了)
a、检查从节点上的,memcached储值状态
1
2
3
4
5
6
7
8
9
|
[root@tomcat02 ~] # telnet 10.10.10.165 11211
Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is '^]' .
get userId VALUE userId 0 5 12345 END 说明:从节点的键值仍旧存在 |
b、在从节点上添加新键值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@tomcat02 ~] # telnet 10.10.10.165 11211
Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is '^]' .
get userId VALUE userId 0 5 12345 END set userId2 0 0 6
123456 STORED get userId2 VALUE userId2 0 6 123456 END 说明:添加userId2的新键值,userId2的值为123456 |
c、恢复主有的节点
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
|
[root@tomcat01 ~] # memcached -v -d -p 11211 -l 10.10.10.164 -u root -x 10.10.10.165 -P
/tmp/memcached .pid
[root@tomcat01 ~] # replication: connect (peer=10.10.10.165:11212)
replication: marugoto copying replication: start [root@tomcat01 ~] # telnet 10.10.10.165 11211
Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is '^]' .
get userId VALUE userId 0 5 12345 END get userId2 VALUE userId2 0 6 123456 END # 然后我们在此节点上添加一个新的键值userId3 set userId3 0 0 7
7654321 STORED get userId3 VALUE userId3 0 7 7654321 END |
d、在另外一台节点上进行验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@tomcat02 ~] # telnet 10.10.10.165 11211
Trying 10.10.10.165... Connected to 10.10.10.165. Escape character is '^]' .
get userId VALUE userId 0 5 12345 END set userId2 0 0 6
123456 STORED get userId2 VALUE userId2 0 6 123456 END get userId3 VALUE userId3 0 7 7654321 END 说明:新添加的键值在此节点上,仍旧存在 |
e、在10.10.10.165上,设置新键值wanwan
1
2
3
4
5
6
7
|
set wanwan 0 0 8
wan08wan STORED get wanwan VALUE wanwan 0 8 wan08wan END |
f、在10.10.10.164上进行查看
1
2
3
4
5
6
7
8
|
[root@tomcat01 ~] # telnet 10.10.10.164 11211
Trying 10.10.10.164... Connected to 10.10.10.164. Escape character is '^]' .
get wanwan VALUE wanwan 0 8 wan08wan END |
8)memcached小结:
1
2
|
a、Repcached 的 Memcached 主从,主从之间可以相互读写 b、由于memcached的主/从没有抢占功能,因此主恢复之后,只能作为现有主节点的从节点 |