memcache+magent的高可用
一、安装步骤: 1、编译安装libevent:wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gztar zxvf libevent-1.4.9-stable.tar.gzcd libevent-1.4.9-stable/./configure --prefix=/usrmake && make installcd ../ 2、编译安装Memcached:wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gztar zxvf memcached-1.2.6.tar.gzcd memcached-1.2.6/./configure --with-libevent=/usrmake && make installcd ../ 3、编译安装magent:mkdir magentcd magent/wget http://memagent.googlecode.com/files/magent-0.5.tar.gz(墙外下载)tar zxvf magent-0.5.tar.gz/sbin/ldconfigsed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefilemakecp magent /usr/bin/magentcd ../报错处理:make时报错 magent.c: In function ‘writev_list’:magent.c:729: error: ‘SSIZE_MAX’ undeclared (first use in this function)magent.c:729: error: (Each undeclared identifier is reported only oncemagent.c:729: error: for each function it appears in.)make: *** [magent.o] Error 1解决: vim ketama.h在开头加入 #ifndef SSIZE_MAX#define SSIZE_MAX 32767#endif再次make报错: gcc: /usr/lib64/libevent.a: No such file or directorygcc: /usr/lib64/libm.a: No such file or directory解决: vi Makefile找到 LIBS = /usr/lib64/libevent.a /usr/lib64/libm.a这一行将libevent 的安装路径修改为/usr/local/lib/ libevent.a将 CFLAGS = -Wall -g -O2 -I/usr/local/include $(M64)改为: CFLAGS = -lrt -Wall -g -O2 -I/usr/local/include $(M64)特别注意的,如果没有lib64的话,可以自己mkdir一个,然后把需要的类库软链接到lib64
ln -s /usr/lib/x86_64-linux-gnu/libm.a /usr/lib64
二、使用实例:memcached -m 1 -u root -d -l 127.0.0.1 -p 11211memcached -m 1 -u root -d -l 127.0.0.1 -p 11212memcached -m 1 -u root -d -l 127.0.0.1 -p 11213magent -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:11213 1、分别在11211、11212、11213端口启动3个Memcached进程,在12000端口开启magent代理程序; 2、11211、11212端口为主Memcached,11213端口为备份Memcached; 3、连接上12000的magent,set key1和set key2,根据哈希算法,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 12000Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.statsmemcached agent v0.4matrix 1 -> 127.0.0.1:11211, pool size 0matrix 2 -> 127.0.0.1:11212, pool size 0ENDset key1 0 0 8zhangyanSTOREDset key2 0 0 8zhangyanSTOREDquitConnection closed by foreign host.[root@centos52 ~]# telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get key1ENDget key2VALUE key2 0 8zhangyanENDquitConnection closed by foreign host.[root@centos52 ~]# telnet 127.0.0.1 11212Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get key1VALUE key1 0 8zhangyanENDget key2ENDquitConnection closed by foreign host.[root@centos52 ~]# telnet 127.0.0.1 11213Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get key1VALUE key1 0 8zhangyanENDget key2VALUE key2 0 8zhangyanENDquitConnection closed by foreign host.模拟11211、11212端口的Memcached死掉[root@centos52 ~]# ps -ef | grep memcachedroot 6589 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211root 6591 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212root 6593 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11213root 6609 6509 0 01:44 pts/0 00:00:00 grep memcached[root@centos52 ~]# kill -9 6589[root@centos52 ~]# kill -9 6591[root@centos52 ~]# telnet 127.0.0.1 12000Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get key1 VALUE key1 0 8zhangyanENDget key2VALUE key2 0 8zhangyanENDquitConnection closed by foreign host.模拟11211、11212端口的Memcached重启复活[root@centos52 ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11211[root@centos52 ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212[root@centos52 ~]# telnet 127.0.0.1 12000Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get key1ENDget key2ENDquitConnection closed by foreign host.查看memcached的数据用stats items