magent解决memcached集群问题
?
magent解决memcached集群问题
?
magent是一款开源的Memcached代理服务器软件,其项目网址为:
http://code.google.com/p/memagent/
?
一、安装步骤:
1、编译安装libevent:
wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
tar zxvf libevent-1.4.9-stable.tar.gz
cd libevent-1.4.9-stable/
./configure --prefix=/usr
make && make install
cd ../
?
测试libevent库是否安装成功:
ls -al /usr/lib |grep libevent 显示如下: ? lrwxrwxrwx ?1 root root ? ? 21 Dec 12 20:28 libevent-2.0.so.5 -> libevent-2.0.so.5.1.4 -rwxr-xr-x ?1 root root 762242 Dec 12 20:28 libevent-2.0.so.5.1.4
?
2、编译安装Memcached:
wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
tar zxvf memcached-1.2.6.tar.gz
cd memcached-1.2.6/
./configure --with-libevent=/usr
make && make install
cd ../
?
?
测试memcached服务端是否安装成功:
ls -al /usr/local/bin/memcached
显示如下:
-rwxr-xr-x 1 root root 222925 Dec 12 20:39 /usr/local/bin/memcached
这样就OK了,memcached服务端就安装成功了
?
把Memcached服务加载到Linux的启动项中.万一机器断电系统重启.那么Memcached就会自动启动了.
假如启动Memcache的服务器端的命令为:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.141.64 -p 12000 -c 256 -P /tmp/memcached.pid容来自17jquery
想开机自动启动的话,只需在/etc/rc.d/rc.local中加入一行,下面命令
/usr/local/memcached/bin/memcached -d -m 10 -p 12000 -u apache -c 256
上面有些东西可以参考一下:即,ip不指定时,默认是本机,用户:最好选择是:apache 或 deamon
这样,也就是属于哪个用户的服务,由哪个用户启动。
3、编译安装magent:
mkdir magent
cd magent/
wget http://memagent.googlecode.com/files/magent-0.5.tar.gz
tar zxvf magent-0.5.tar.gz
/sbin/ldconfig
sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
make
cp magent /usr/bin/magent
cd ../
--------------------------------------------
二、使用实例:
memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
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: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 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
memcached agent v0.4
matrix 1 -> 127.0.0.1:11211, pool size 0
matrix 2 -> 127.0.0.1:11212, pool size 0
END
set key1 0 0 8
zhangyan
STORED
set key2 0 0 8
zhangyan
STORED
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11212
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
END
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11213
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
模拟11211、11212端口的Memcached死掉
[root@centos52 ~]# ps -ef | grep memcached
root ? ? ? 6589 ? ? 1 ? 0 01:25 ? ? ? ? ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
root ? ? ? 6591 ? ? 1 ? 0 01:25 ? ? ? ? ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
root ? ? ? 6593 ? ? 1 ? 0 01:25 ? ? ? ? ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
root ? ? ? 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 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1?
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection 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 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
END
quit
Connection closed by foreign host
注意: 服务器上的GCC是否安装
rpm -q gcc