天天看點

Memcached的代理伺服器軟體:magent使用小記

 [文章作者:張宴 本文版本:v1.0 最後修改:2009.01.19 轉載請注明原文連結: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

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還有數 據(此問題尚待改進)。

  三、整個測試流程:

[[email protected] ~]# 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.

[[email protected] ~]# 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.

[[email protected] ~]# 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.

[[email protected] ~]# 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死掉

[[email protected] ~]# 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

[[email protected] ~]# kill -9 6589

[[email protected] ~]# kill -9 6591

[[email protected] ~]# 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重新開機複活

[[email protected] ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11211

[[email protected] ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212

[[email protected] ~]# 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
    1. get gets
    2. delete
    3. incr decr
    4. add set replace prepend append
    5. 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

  1. add connection keepalive handler
  2. 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:

  1. current magent's speed is about 80% of memcached's. finding way to speed it up(not good)
  2. adding backup server does no harm to magent's performance. (good)
  3. magent's cpu usage is less than memcached's and has very little memory usage. (good)

繼續閱讀