Memcached学习笔记(四)——repcached高可用方案
repcached:全称 replication cached是由日本人发明的memcached的高可用性技术,简称复制缓冲区技术。
使用场景:它是一个单master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步,如果 master 宕机, slave侦测到连接断了,它会自动 listen而成为 master;而如果 slave坏掉, master也会侦测到连接断,它就会重新 listen等待新的 slave加入。
(一)安装相关依赖(编译repcached程序所依赖的包):
[root@localhost memcached-1.2.8-repcached-2.2]# yum install libevent-devel Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * addons: mirrors.yun-idc.com * base: mirrors.yun-idc.com * epel: mirrors.yun-idc.com * extras: mirrors.yun-idc.com * updates: mirrors.yun-idc.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package libevent-devel.i386 0:1.4.13-1 set to be updated --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: libevent-devel i386 1.4.13-1 base 373 k Transaction Summary ================================================================================ Install 1 Package(s) Upgrade 0 Package(s) Total download size: 373 k Is this ok [y/N]: y Downloading Packages: libevent-devel-1.4.13-1.i386.rpm | 373 kB 00:04 Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing : libevent-devel 1/1 Installed: libevent-devel.i386 0:1.4.13-1 Complete! [root@localhost memcached-1.2.8-repcached-2.2]#
(二)下载并安装:memcached-1.2.8-repcached-2.2.tar.gz
[root@localhost ~]# wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz --2013-03-23 13:35:56-- http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz Resolving downloads.sourceforge.net... 216.34.181.59 Connecting to downloads.sourceforge.net|216.34.181.59|:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: http://downloads.sourceforge.net/project/repcached/repcached/2.2-1.2.8/memcached-1.2.8-repcached-2.2.tar.gz [following] --2013-03-23 13:35:57-- http://downloads.sourceforge.net/project/repcached/repcached/2.2-1.2.8/memcached-1.2.8-repcached-2.2.tar.gz Reusing existing connection to downloads.sourceforge.net:80. HTTP request sent, awaiting response... 302 Found Location: http://nchc.dl.sourceforge.net/project/repcached/repcached/2.2-1.2.8/memcached-1.2.8-repcached-2.2.tar.gz [following] --2013-03-23 13:35:59-- http://nchc.dl.sourceforge.net/project/repcached/repcached/2.2-1.2.8/memcached-1.2.8-repcached-2.2.tar.gz Resolving nchc.dl.sourceforge.net... 211.79.60.17, 2001:e10:ffff:1f02::17 Connecting to nchc.dl.sourceforge.net|211.79.60.17|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 227510 (222K) [application/x-gzip] Saving to: `memcached-1.2.8-repcached-2.2.tar.gz' 100%[======================================>] 227,510 240K/s in 0.9s 2013-03-23 13:36:01 (240 KB/s) - `memcached-1.2.8-repcached-2.2.tar.gz' saved [227510/227510] [root@localhost ~]# tar zxvf memcached-1.2.8-repcached-2.2.tar.gz [root@localhost ~]# ./configure --enable-replication --program-transform-name=s/memcached/repcached/(三)启动repcached (用非root操作):
[hadoop@localhost ~]$ /usr/local/bin/repcached -p 11211 -v -d replication: listen [hadoop@localhost ~]$ /usr/local/bin/repcached -p 11212 -x localhost -v -d replication: connect (peer=127.0.0.1:11212) replication: marugoto copying replication: accept [hadoop@localhost ~]$ replication: start参数解释:
-d : 后台运行memcached进程
-p : 默认监听端口11211 11212
-x : 监听高可用机器,如果是监听默认端口不用写端口号,localhost就是监听本机
作用:第二个repcached监听第一个repcached如果有问题就接管
(四)复制实验:
先连接11211机器,设置name 值为“zhangsan”
[hadoop@localhost ~]$ telnet localhost 11211 Trying 127.0.0.1... Connected to localhost (127.0.0.1). Escape character is '^]'. set name 0 0 8 zhangsan STORED get name VALUE name 0 8 zhangsan END quit Connection closed by foreign host.连接11212机器,读取name值:
[hadoop@localhost ~]$ telnet localhost 11212 Trying 127.0.0.1... Connected to localhost (127.0.0.1). Escape character is '^]'. get name VALUE name 0 8 zhangsan END(五)反向复制实验
连接11211,设置age值为23
[hadoop@localhost ~]$ telnet localhost 11212 Trying 127.0.0.1... Connected to localhost (127.0.0.1). Escape character is '^]'. get name VALUE name 0 8 zhangsan END set age 0 0 2 23 STORED get age VALUE age 0 2 23 END连接11211,读取age值:
[hadoop@localhost ~]$ telnet localhost 11211 Trying 127.0.0.1... Connected to localhost (127.0.0.1). Escape character is '^]'. get age VALUE age 0 2 23 END get name VALUE name 0 8 zhangsan END quit Connection closed by foreign host.(六)监控memcached:
[hadoop@localhost ~]$ stats cachedump 1 10 -bash: stats: command not found [hadoop@localhost ~]$ telnet localhost 11211 Trying 127.0.0.1... Connected to localhost (127.0.0.1). Escape character is '^]'. stats cachedump 1 10 ITEM name [8 b; 1365421489 s] ITEM age [2 b; 1365421489 s] END stats items STAT items:1:number 2 STAT items:1:age 119 STAT items:1:evicted 0 STAT items:1:evicted_time 0 STAT items:1:outofmemory 0 STAT items:1:tailrepairs 0 END
(7)实验完成,祝您成功!