memcached+magent实现负载

环境:

CentOS 5.4 x64

这里只用1台虚拟机的3个不同端口模拟,可以进一步模拟3台虚拟机的同一个端口实验

 

yum -y install gcc

mkdir -p /opt/{lnmp,packet_src}/

cd /opt/packet_src/

wget https://memcached.googlecode.com/files/memcached-1.4.15.tar.gz

wget http://sourceforge.net/projects/levent/files/libevent/libevent-1.4/libevent-1.4.13-stable.tar.gz/download

wget http://memagent.googlecode.com/files/magent-0.6.tar.gz

 

1、编译安装libevent:

tar zxvf libevent-1.4.13-stable.tar.gz

cd libevent-1.4.13-stable/

./configure --prefix=/usr

make && make install

cd ../

 

2、编译安装Memcached

tar zxvf memcached-1.4.15.tar.gz

cd memcached-1.4.15/

./configure --prefix=/opt/lnmp/memcached/ --with-libevent=/usr

make && make install

cd ../

 

参数手册

启动过程中所用选项说明如下:

-p,使用的TCP端口。默认为11211。

-m,最大内存大小。默认为64MB。

-vv,以very vrebose模式启动,将调试信息和错误输出到控制台。

-d,作为守护进程在后台运行。

-c,最大运行的并发连接数,默认是1024,按照服务器的负载量来设定。

-P,设置保存Memcache的pid文件。

-l,监听的服务器IP地址,如果有多个地址。

-u,运行Memcached的用户,默认不能由root用户启动,所以当前用户为root用户时,需要利用-u参数来指定。

例:/opt/lnmp/memcached/bin/memcached -m 32m -d -l 192.168.4.222 -p 11211 -u root -P /opt/lnmp/memcached/memcached_11211.pid -c 256

 

3、编译安装magent:

mkdir -p magent

tar zxvf magent-0.6.tar.gz -C magent

/sbin/ldconfig

sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile  //版本小于0.6需要执行这一句

make

cp magent /usr/bin/magent

 

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

 

问题:

gcc: /usr/lib64/libevent.a: No such file or directory  

解决办法:

ln -s /usr/lib/libevent.a /usr/lib64/libevent.a   

 

问题:

gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib64/libevent.a /usr/lib64/libm.a   

/usr/lib64/libevent.a(event.o): In function `gettime':  

/home/shuzhi/libevent-1.4.5-stable/event.c:150: undefined reference to `clock_gettime'  

/usr/lib64/libevent.a(event.o): In function `detect_monotonic':  

/home/shuzhi/libevent-1.4.5-stable/event.c:133: undefined reference to `clock_gettime' 

解决办法:

gcc -lrt -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib64/libevent.a /usr/lib64/libm.a

 

实例:

/opt/lnmp/memcached/bin/memcached -m 32m -d -l 192.168.4.222 -p 11211 -u root -P /opt/lnmp/memcached/memcached_11211.pid -c 256

/opt/lnmp/memcached/bin/memcached -m 32m -d -l 192.168.4.222 -p 11212 -u root -P /opt/lnmp/memcached/memcached_11212.pid -c 256

/opt/lnmp/memcached/bin/memcached -m 32m -d -l 192.168.4.222 -p 11213 -u root -P /opt/lnmp/memcached/memcached_11213.pid -c 256

magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 192.168.4.222:11211 -s 192.168.4.222:11212 -b 192.168.4.222: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还有数 据(此问题尚待改进)。

 

 

测试:

# telnet 127.0.0.1 12000

Trying 127.0.0.1...

Connected to Monitor (127.0.0.1).

Escape character is '^]'.

stats

memcached agent v0.6

matrix 1 -> 192.168.4.222:11211, pool size 0

matrix 2 -> 192.168.4.222:11212, pool size 0

END

set key1 0 0 9

chinatree

STORED

set key2 0 0 9

chinatree

STORED

quit

Connection closed by foreign host.

 

# telnet 192.168.4.222 11211

Trying 192.168.4.222...

Connected to 192.168.4.222.

Escape character is '^]'.

get key1

VALUE key1 0 9

chinatree

END

get key2

END

quit

Connection closed by foreign host.

 

# telnet 192.168.4.222 11212

Trying 192.168.4.222...

Connected to 192.168.4.222.

Escape character is '^]'.

get key1

END

get key2

VALUE key2 0 9

chinatree

END

quit

Connection closed by foreign host.

 

# telnet 192.168.4.222 11213

Trying 192.168.4.222...

Connected to 192.168.4.222.

Escape character is '^]'.

get key1

VALUE key1 0 9

chinatree

END

get key2

VALUE key2 0 9

chinatree

END

quit

Connection closed by foreign host.

 

模拟11211和11212端口全down掉

kill `cat /opt/lnmp/memcached/memcached_11211.pid` `cat /opt/lnmp/memcached/memcached_11212.pid`

 

# telnet 127.0.0.1 12000

Trying 127.0.0.1...

Connected to Monitor (127.0.0.1).

Escape character is '^]'.

get key1

VALUE key1 0 9

chinatree

END

get key2

VALUE key2 0 9

chinatree

END

quit

Connection closed by foreign host.

 

恢复11211和11212端口:

/opt/lnmp/memcached/bin/memcached -m 32m -d -l 192.168.4.222 -p 11211 -u root -P /opt/lnmp/memcached/memcached_11211.pid -c 256

/opt/lnmp/memcached/bin/memcached -m 32m -d -l 192.168.4.222 -p 11212 -u root -P /opt/lnmp/memcached/memcached_11212.pid -c 256

 

# telnet 127.0.0.1 12000

Trying 127.0.0.1...

Connected to Monitor (127.0.0.1).

Escape character is '^]'.

get key1

END

get key2

END

quit

Connection closed by foreign host.

 

参考网址:

https://code.google.com/p/memagent/

http://simblog.vicp.net/?p=105

你可能感兴趣的:(集群,memcached,magent,memcache代理软件)