[文章作者:张宴 本文版本:v1.0 最后修改:2009.01.19 转载请注明原文链接:http://blog.zyan.cc/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还有数据(此问题尚待改进)。



  三、整个测试流程:
[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.






技术大类 » Cache与存储 | 评论(97) | 引用(8) | 阅读(105994)
dengjiuhong Homepage
2009-1-19 09:36
3、连接上12000的magent,set key1和set key2,根据哈希算法,key1被写入11212和11213端口的Memcached,key2被写入11212和11213端口的Memcached;貌似key1是写入11211和11213?
xi2008wang
2009-1-19 09:44
已阅zan
稳压电源 Email Homepage
2009-1-19 14:23
很详细 谢谢了  学习中。。。


稳压电源
Tim Homepage
2009-1-20 10:56
感觉主要是容错功能吧,象 java memcached client 默认带了类似功能。

http://www.whalin.com/memcached/HOWTO.txt

By default the java client will failover to a new server when a server
dies.  It will also failback to the original if it detects that the
server comes back (it checks the server in a falling off pattern).

If you want to disable this (useful if you have flapping servers),
there are two settings to handle this.

  pool.setFailover( false );
  pool.setFailback( false );
dd_macle
2009-1-21 13:06
拜读了
沙加
2009-2-1 18:18
为什么不补充一点为什么需要使用这个代理的理由呢,也照顾一下俺们这些初级读者嘛~~
evil_knight
2009-2-5 23:02
感觉在应用里面直接实现会少一个proxy层,效率会好,当然想应用无关的思想最好!呵呵!
todayboy
2009-3-16 10:49
我的网站是php + oracle 的 ,我现在在phg上加了memcache ,也开启了memcached 进程,但感觉网站没有任何性情的改变,不知我那里错,张兄能不能写一篇 memcache比较完整的文章??????谢谢
向你学习
2009-3-24 11:06
发现 你一处手误

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

打下划线的部分应该 改成  -s 吧?
张宴 回复于 2009-3-24 11:32
-b 表示127.0.0.1:11213是 -s 两个Memcached的备份,此处没有错误。
瞎玩
2009-9-2 11:36
proxy 宕掉了呢?
变压器 Homepage
2009-10-10 10:17
这个东西很详细 我在学习


变压器
天下小飞~
2009-11-3 01:33
哥们,你老牛了,改天去北京看看你吧
奇迹花园
2010-2-5 16:50
5、当11211、11212端口的Memcached重启复活,连接到12000端口,magent会从11211或11212端口的Memcached取数据,由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管11213端口的Memcached还有数据(此问题尚待改进)。
老兄,我想知道这个问题怎么解决。这个问题比较棘手。
jelly
2010-7-14 10:22
重新从db中读取放入mc中呗
勇气的力量 Email
2010-7-15 20:02
5、当11211、11212端口的Memcached重启复活,连接到12000端口,magent会从11211或11212端口的Memcached取数据,由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管11213端口的Memcached还有数据(此问题尚待改进)。
老兄,我想知道这个问题怎么解决。这个问题比较棘手。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这个是可以解决的。
在get查询时,magent只在返回字节数小于0的情况下才查询备用服务器,对应主服务器down机。所以你要做的,就是在这个点判断返回的字节数,如果为5,'END\r\n',则主服务器没有数据返回,这时需要强制它查询备用服务器。
John
2010-8-3 15:41
你好
备份服务器可否有多台?

magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s A机器:11211 -s B机器:11212 -b A机器:11213 -b B机器:11214
a12333a
2011-3-24 18:40
这些操作只能在同一台服务器上面操作么,局域网内的不同机器也能实现memcache的数据同步么,? 主要是怕单一机器宕机的话比较麻烦。
Allice
2011-11-2 10:12
使用 magent 后,程序中如何调用 magent
louis vuitton uk Email Homepage
2011-11-23 09:07
This louis vuitton uk for sale belongs to the sounding just what are termed as Louis Vuitton vintage best sellers, many other products and services for the reason that range appearing companies.You will easily notice the unfold zippers of this coach outlet store online. That is the decoration. There are some inside pockets for you as well. They are easy to match your clothes and to carry.Let us inspire your inner beauty with fine christian louboutin sale. Purse the elegance in bridal wedding. Enjoy the fashion.
hani
2012-2-1 14:51
set key1 0 0 8
kkkkk
CLIENT_ERROR bad data chunk   为什么会有这个错误?
分页: 1/6 第一页 1 2 3 4 5 6 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]