Memcached无单点解决方案
一.Repcached (memcached同步补丁)
下载地址:http://sourceforge.net/projects/repcached/files/repcached/2.2.1-1.2.8/
版本号:memcached 1.2.8 repcached 2.2.1
1、repcached介绍repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步,如果 master坏掉, slave侦测到连接断了,它会自动 listen而成为 master;而如果 slave坏掉, master也会侦测到连接断,它就会重新 listen等待新的 slave加入
2、安装
在两台服务器上分别安装memcached服务,另注本文libevent的版本为:libevent-1.4.13,下载地址:http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz(安装省略)
repcached有两种方式:
方式一、下载对应的repcached版本
#wgethttp://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
#tarzxfmemcached-1.2.8-repcached-2.2.tar.gz
#cd memcached-1.2.8-repcached-2.2【方式二、下载对应patch版本
#wgethttp://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz
#gzip-cd../repcached-2.2-1.2.8.patch.gz|patch-p1】
#./configure–enable-replication
#make
# make install3、启动:
启动master
# ./memcached -v -u root -d -l 127.0.0.1 -p 11211 -x 127.0.0.1 -X 11111 (11211:master端口;11111:同步端口)启动salve# ./memcached -v -u root -d -l 127.0.0.1 -p 11212 -x 127.0.0.1 -X 11111 (11212:slave端口;11111:同步端口)
4、测试:操作master
查看slave
注意:如果master down机,slave接管并成为master,这时down机的master只能启用slave,他们之间互换角色,才能保持复制功能。换句话说,master没有抢占功能。而且,同步间隔时间未知。
优点:
- 数据冗余
- 两台memcached都可以进行读写操作
缺点:
- 只支持单对单
- 只支持memcached 1.2.x版本
二.Magent (memcached的代理)
原文地址:http://blog.s135.com/post/393/
magent是一款开源的Memcached代理服务器软件,其项目网址为:
http://code.google.com/p/memagent/
一、安装步骤:1、编译安装libevent:
wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
tarzxvflibevent-1.4.9-stable.tar.gz
cdlibevent-1.4.9-stable/
./configure–prefix=/usr
make&&makeinstall
cd ../2、编译安装Memcached:
wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
tarzxvfmemcached-1.2.6.tar.gz
cdmemcached-1.2.6/
./configure–with-libevent=/usr
make&&makeinstall
cd ../3、编译安装magent:
mkdir magent
cdmagent/
wgethttp://memagent.googlecode.com/files/magent-0.5.tar.gz
tarzxvfmagent-0.5.tar.gz
/sbin/ldconfig
sed-i“s#LIBS=-levent#LIBS=-levent-lm#g”Makefile
make
cpmagent/usr/bin/magent
cd ../二、使用实例:
memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
memcached-m1-uroot-d-l127.0.0.1-p11212
memcached-m1-uroot-d-l127.0.0.1-p11213
magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 127.0.0.1:11211 -s 127.0.0.1:11212 -b 127.0.0.1:112131、分别在11211、11212、11213端口启动3个Memcached进程,在12000端口开启magent代理程序;
2、11211、11212端口为主Memcached,11213端口为备份Memcached;
3、连接上12000的magent,setkey1和setkey2,根据哈希算法,key1被写入11212和11213端口的Memcached,key2被写入11212和11213端口的Memcached;
4、当11211、11212端口的Memcached死掉,连接到12000端口的magent取数据,数据会从11213端口的Memcached取出;
5、当11211、11212端口的Memcached重启复活,连接到12000端口,magent会从11211或11212端口的Memcached取数据,由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管11213端口的Memcached还有数据(此问题尚待改进)。三、整个测试流程:
[root@centos52 ~]# telnet 127.0.0.1 12000
Trying127.0.0.1…
Connectedto127.0.0.1.
Escapecharacteris‘^]’.
stats
memcachedagentv0.4
matrix1->127.0.0.1:11211,poolsize0
matrix2->127.0.0.1:11212,poolsize0
END
setkey1008
zhangyan
STORED
setkey2008
zhangyan
STORED
quit
Connection closed by foreign host.[root@centos52 ~]# telnet 127.0.0.1 11211
Trying127.0.0.1…
Connectedto127.0.0.1.
Escapecharacteris‘^]’.
getkey1
END
getkey2
VALUEkey208
zhangyan
END
quit
Connection closed by foreign host.[root@centos52 ~]# telnet 127.0.0.1 11212
Trying127.0.0.1…
Connectedto127.0.0.1.
Escapecharacteris‘^]’.
getkey1
VALUEkey108
zhangyan
END
getkey2
END
quit
Connection closed by foreign host.[root@centos52 ~]# telnet 127.0.0.1 11213
Trying127.0.0.1…
Connectedto127.0.0.1.
Escapecharacteris‘^]’.
getkey1
VALUEkey108
zhangyan
END
getkey2
VALUEkey208
zhangyan
END
quit
Connection closed by foreign host.模拟11211、11212端口的Memcached死掉
[root@centos52~]#ps-ef|grepmemcached
root65891001:25?00:00:00memcached-m1-uroot-d-l127.0.0.1-p11211
root65911001:25?00:00:00memcached-m1-uroot-d-l127.0.0.1-p11212
root65931001:25?00:00:00memcached-m1-uroot-d-l127.0.0.1-p11213
root66096509001:44pts/000:00:00grepmemcached
[root@centos52~]#kill-96589
[root@centos52~]#kill-96591
[root@centos52~]#telnet127.0.0.112000
Trying127.0.0.1…
Connectedto127.0.0.1.
Escapecharacteris‘^]’.
getkey1
VALUEkey108
zhangyan
END
getkey2
VALUEkey208
zhangyan
END
quit
Connection closed by foreign host.模拟11211、11212端口的Memcached重启复活
[root@centos52~]#memcached-m1-uroot-d-l127.0.0.1-p11211
[root@centos52~]#memcached-m1-uroot-d-l127.0.0.1-p11212
[root@centos52~]#telnet127.0.0.112000
Trying127.0.0.1…
Connectedto127.0.0.1.
Escapecharacteris‘^]’.
getkey1
END
getkey2
END
quit
Connection closed by foreign host.优点:
可以做多主或多从
缺点:
当master宕掉又恢复后,取不到数据。
三.Repcached和Magent结合
安装memcached 1.2.8、repcached 2.2.1、magent 0.5
大体架构:
测试流程:
- 启动memcached和magent
开启代理,代理端口为12000,设置127.0.0.1:11211为主,127.0.0.1:11212为从。
magent -u root -p 12000 -s 127.0.0.1:11211 -b 127.0.0.1:11212
启动两个memcached进程,做数据同步,同步端口为11111。
./memcached -u root -d -l 127.0.0.1 -p 11211 -x 127.0.0.1 -X 11111
./memcached -u root -d -l 127.0.0.1 -p 11212 -x 127.0.0.1 -X 11111
- 从代理写入数据,在两台memcached上查看。
2. 模拟主或从memcached宕机后,数据是否存在。
3. 模拟主或从memcached恢复后,数据是否存在/同步。
优点:
数据冗余
两台memcached都可进行读写操作
可以做多主或多从(未测试)
缺点:
只支持memcached 1.2.x版本