[文章作者:張宴 本文版本:v1.0 最後修改:2009.01.19 轉載請註明原文連結:http://blog.s135.com/post/393/
]
magent是一款開源的MemcachedProxy 伺服器軟體,其項目網址為:
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 ../
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 ../
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.
附用法介紹
:
magent
is a simple but useful proxy program for memcached
servers.
It features:
- keeps connections to memcached servers
- supports following memcached commands
- get gets
- delete
- incr decr
- add set replace prepend append
- cas
- event-driven by using libevent
library
- supports ketama algorithm
- backup servers farm
- unix domain socket
Usage:
-h this message
-u uid
-g gid
-p port, default is 11211. (0 to disable tcp support)
-s ip:port, set memcached server ip and port
-b ip:port, set backup memcached server ip and port
-l ip, local bind ip address, default is 0.0.0.0
-n number, set max connections, default is 4096
-D do not go to background
-k use ketama key allocation algorithm
-f file, unix socket path to listen on. default is off
-i number, max keep alive connections for one memcached server, default is 20
-v verbose
Changelog:
2010/4/14: memcached agent 0.6
- add connection keepalive handler
- bug fix, more robust, more debug messages
Examples:
magent -s 10.1.2.1 -s 10.1.2.2:11211 -b 10.1.2.3:14000 -v
Notes:
- current magent's speed is about 80% of memcached's. finding way to speed it up(not good)
- adding backup server does no harm to magent's performance. (good)
- magent's cpu usage is less than memcached's and has very little memory usage. (good)